Skip to content

Commit 19af663

Browse files
authored
Adds http-ssl example (#130)
Co-authored-by: Croway
1 parent 98298ef commit 19af663

24 files changed

+954
-1
lines changed

README.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ readme's instructions.
2727
=== Examples
2828

2929
// examples: START
30-
Number of Examples: 56 (0 deprecated)
30+
Number of Examples: 57 (0 deprecated)
3131

3232
[width="100%",cols="4,2,4",options="header"]
3333
|===
@@ -133,6 +133,8 @@ Number of Examples: 56 (0 deprecated)
133133
| link:reactive-streams/readme.adoc[Reactive Streams] (reactive-streams) | Reactive | An example that shows how Camel can exchange data using reactive streams with Spring Boot reactor
134134

135135

136+
| link:http-ssl/README.adoc[Http Ssl] (http-ssl) | Rest | An example showing the Camel HTTP component with Spring Boot and SSL
137+
136138
| link:openapi-contract-first/readme.adoc[Openapi Contract First] (openapi-contract-first) | Rest | Contract First OpenAPI example
137139

138140
| link:platform-http/README.adoc[Platform Http] (platform-http) | Rest | An example showing Camel REST DSL with platform HTTP

http-ssl/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ssl
2+
*.jks
3+
*.pem

http-ssl/README.adoc

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
== Spring Boot Example with HTTP and SSL
2+
3+
=== Introduction
4+
5+
This example shows how to configure SSL in different scenarios:
6+
7+
1. one way SSL, the server exposes REST API using SSL and the client trusts the server certificate. The SSL server configuration is managed by Spring Boot and the Camel inherit it, the SSL client configuration is managed by Camel in HTTP component
8+
2. two ways SSL, the server and the client check both certificates in a mutual trusted handshake
9+
3. same scenario as point 1 but the server configuration is managed directly in Camel (undertow component) instead of Spring Boot
10+
11+
=== Prerequisites
12+
13+
keytool installed and available on PATH
14+
15+
Generate certificates and keystores
16+
17+
$ ./generate-certificates.sh
18+
19+
=== Run using one way ssl (server validation on client side)
20+
21+
Start ssl-server in a separate terminal:
22+
23+
$ mvn spring-boot:run -f ssl-server/pom.xml
24+
25+
Start ssl-client in a separate terminal:
26+
27+
$ mvn spring-boot:run -f ssl-client/pom.xml
28+
29+
=== Run using two ways ssl (mutual validation)
30+
31+
Start ssl-server in a separate terminal:
32+
33+
$ mvn spring-boot:run -f ssl-server/pom.xml -Ptwoways
34+
35+
Start ssl-client in a separate terminal:
36+
37+
$ mvn spring-boot:run -f ssl-client/pom.xml -Ptwoways
38+
39+
=== Run using Camel component as server
40+
41+
Start ssl-camel-server in a separate terminal:
42+
43+
$ mvn spring-boot:run -f ssl-camel-server/pom.xml
44+
45+
Start ssl-client in a separate terminal:
46+
47+
$ mvn spring-boot:run -f ssl-client/pom.xml
48+
49+
=== Call service to start handshake
50+
51+
$ curl http://localhost:8080/ping
52+
53+
==== Tip
54+
55+
to show the full handshake it is possible to add `-Dspring-boot.run.jvmArguments="-Djavax.net.debug=all"` in the start command line
56+
57+
58+
=== Help and contributions
59+
60+
If you hit any problem using Camel or have some feedback, then please
61+
https://camel.apache.org/community/support/[let us know].
62+
63+
We also love contributors, so
64+
https://camel.apache.org/community/contributing/[get involved] :-)
65+
66+
The Camel riders!

http-ssl/generate-certificates.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/bash
2+
GEN_DIR="ssl"
3+
4+
K_PASS=pass123
5+
SERVER_JKS=$GEN_DIR/server.jks
6+
SERVER_CERT=$GEN_DIR/server.pem
7+
SERVER_TRUST=$GEN_DIR/server-truststore.jks
8+
CLIENT_JKS=$GEN_DIR/client.jks
9+
CLIENT_CERT=$GEN_DIR/client.pem
10+
CLIENT_TRUST=$GEN_DIR/client-truststore.jks
11+
12+
echo remove directory $GEN_DIR if exists
13+
[ -e $GEN_DIR ] && rm -rf $GEN_DIR
14+
15+
echo create directory $GEN_DIR
16+
mkdir -p $GEN_DIR
17+
18+
echo generate server certificates
19+
keytool -alias server -dname "cn=localhost, ou=ssl-server, o=csb-http-ssl, c=US" -genkeypair -storepass $K_PASS -keyalg RSA -keystore $SERVER_JKS
20+
21+
echo generate client certificates
22+
keytool -alias client -dname "cn=localhost, ou=ssl-client, o=csb-http-ssl, c=US" -genkeypair -storepass $K_PASS -keyalg RSA -keystore $CLIENT_JKS
23+
24+
echo export server certificates
25+
keytool -exportcert -alias server -storepass $K_PASS -keystore $SERVER_JKS -rfc -file $SERVER_CERT
26+
27+
echo export client certificates
28+
keytool -exportcert -alias client -storepass $K_PASS -keystore $CLIENT_JKS -rfc -file $CLIENT_CERT
29+
30+
echo import server in client truststore
31+
keytool -import -keystore $CLIENT_TRUST -storepass $K_PASS -file $SERVER_CERT -alias server -noprompt -trustcacerts
32+
33+
echo import client in server truststore
34+
keytool -import -keystore $SERVER_TRUST -storepass $K_PASS -file $CLIENT_CERT -alias client -noprompt -trustcacerts
35+
36+
echo copy $SERVER_JKS in ssl-server/src/main/resources
37+
[ -e ssl-server/src/main/resources/server.jks ] && rm ssl-server/src/main/resources/server.jks
38+
cp $SERVER_JKS ssl-server/src/main/resources/server.jks
39+
40+
echo copy $SERVER_TRUST in ssl-server/src/main/resources
41+
[ -e ssl-server/src/main/resources/server-truststore.jks ] && rm ssl-server/src/main/resources/server-truststore.jks
42+
cp $SERVER_TRUST ssl-server/src/main/resources/server-truststore.jks
43+
44+
echo copy $CLIENT_JKS in ssl-client/src/main/resources
45+
[ -e ssl-client/src/main/resources/client.jks ] && rm ssl-client/src/main/resources/client.jks
46+
cp $CLIENT_JKS ssl-client/src/main/resources/client.jks
47+
48+
echo copy $CLIENT_TRUST in ssl-client/src/main/resources
49+
[ -e ssl-client/src/main/resources/client-truststore.jks ] && rm ssl-client/src/main/resources/client-truststore.jks
50+
cp $CLIENT_TRUST ssl-client/src/main/resources/client-truststore.jks
51+
52+
echo copy $SERVER_JKS in ssl-camel-server/src/main/resources
53+
[ -e ssl-camel-server/src/main/resources/server.jks ] && rm ssl-camel-server/src/main/resources/server.jks
54+
cp $SERVER_JKS ssl-camel-server/src/main/resources/server.jks

http-ssl/pom.xml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Licensed to the Apache Software Foundation (ASF) under one or more
5+
contributor license agreements. See the NOTICE file distributed with
6+
this work for additional information regarding copyright ownership.
7+
The ASF licenses this file to You under the Apache License, Version 2.0
8+
(the "License"); you may not use this file except in compliance with
9+
the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
23+
<parent>
24+
<groupId>org.apache.camel.springboot.example</groupId>
25+
<artifactId>examples</artifactId>
26+
<version>4.7.0-SNAPSHOT</version>
27+
</parent>
28+
29+
<artifactId>camel-example-spring-boot-http-ssl</artifactId>
30+
<name>Camel SB Examples :: HTTP SSL</name>
31+
<description>An example showing the Camel HTTP component with Spring Boot and SSL</description>
32+
<packaging>pom</packaging>
33+
34+
<properties>
35+
<category>Rest</category>
36+
</properties>
37+
38+
<!-- Spring-Boot and Camel BOM -->
39+
<dependencyManagement>
40+
<dependencies>
41+
<dependency>
42+
<groupId>org.apache.camel.springboot</groupId>
43+
<artifactId>camel-spring-boot-bom</artifactId>
44+
<version>${project.version}</version>
45+
<type>pom</type>
46+
<scope>import</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-dependencies</artifactId>
51+
<version>${spring-boot-version}</version>
52+
<type>pom</type>
53+
<scope>import</scope>
54+
</dependency>
55+
</dependencies>
56+
</dependencyManagement>
57+
58+
<modules>
59+
<module>ssl-server</module>
60+
<module>ssl-client</module>
61+
<module>ssl-camel-server</module>
62+
</modules>
63+
64+
</project>

http-ssl/ssl-camel-server/pom.xml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Licensed to the Apache Software Foundation (ASF) under one or more
5+
contributor license agreements. See the NOTICE file distributed with
6+
this work for additional information regarding copyright ownership.
7+
The ASF licenses this file to You under the Apache License, Version 2.0
8+
(the "License"); you may not use this file except in compliance with
9+
the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
<project xmlns="http://maven.apache.org/POM/4.0.0"
21+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
23+
<modelVersion>4.0.0</modelVersion>
24+
25+
<parent>
26+
<groupId>org.apache.camel.springboot.example</groupId>
27+
<artifactId>camel-example-spring-boot-http-ssl</artifactId>
28+
<version>4.7.0-SNAPSHOT</version>
29+
</parent>
30+
31+
<artifactId>camel-example-spring-boot-http-ssl-camel-server</artifactId>
32+
<name>Camel SB Examples :: HTTP SSL :: SSL Camel server</name>
33+
<description>SSL Server using undertow component</description>
34+
35+
<properties>
36+
<category>Rest</category>
37+
38+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
39+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
40+
</properties>
41+
42+
<dependencies>
43+
44+
<!-- Camel -->
45+
<dependency>
46+
<groupId>org.apache.camel.springboot</groupId>
47+
<artifactId>camel-spring-boot-starter</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.apache.camel.springboot</groupId>
51+
<artifactId>camel-undertow-starter</artifactId>
52+
</dependency>
53+
54+
</dependencies>
55+
56+
<build>
57+
<plugins>
58+
<plugin>
59+
<groupId>org.springframework.boot</groupId>
60+
<artifactId>spring-boot-maven-plugin</artifactId>
61+
<version>${spring-boot-version}</version>
62+
<executions>
63+
<execution>
64+
<goals>
65+
<goal>repackage</goal>
66+
</goals>
67+
</execution>
68+
</executions>
69+
</plugin>
70+
</plugins>
71+
</build>
72+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.camel.springboot.example.httpssl;
18+
19+
import org.springframework.boot.SpringApplication;
20+
import org.springframework.boot.autoconfigure.SpringBootApplication;
21+
22+
@SpringBootApplication
23+
public class HttpSslCamelServerApplication {
24+
25+
public static void main(String[] args) {
26+
SpringApplication.run(HttpSslCamelServerApplication.class, args);
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.camel.springboot.example.httpssl;
18+
19+
import org.apache.camel.builder.RouteBuilder;
20+
21+
import org.springframework.stereotype.Component;
22+
23+
@Component
24+
public class HttpSslCamelServerRouter extends RouteBuilder {
25+
@Override
26+
public void configure() throws Exception {
27+
from("undertow:https://localhost:8443/ping")
28+
.setBody().constant("pong");
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.camel.springboot.example.httpssl;
18+
19+
import org.apache.camel.support.jsse.KeyManagersParameters;
20+
import org.apache.camel.support.jsse.KeyStoreParameters;
21+
import org.apache.camel.support.jsse.SSLContextParameters;
22+
23+
import org.springframework.beans.factory.annotation.Value;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.context.annotation.Configuration;
26+
27+
@Configuration
28+
public class SSLConfiguration {
29+
30+
@Bean("serverConfig")
31+
public SSLContextParameters sslContextParameters(@Value("${keystore-password}") final String password) {
32+
final SSLContextParameters sslContextParameters = new SSLContextParameters();
33+
34+
final KeyStoreParameters ksp = new KeyStoreParameters();
35+
ksp.setResource("classpath:server.jks");
36+
ksp.setPassword(password);
37+
ksp.setType("PKCS12");
38+
39+
KeyManagersParameters kmp = new KeyManagersParameters();
40+
kmp.setKeyPassword(password);
41+
kmp.setKeyStore(ksp);
42+
43+
sslContextParameters.setKeyManagers(kmp);
44+
45+
return sslContextParameters;
46+
}
47+
}

0 commit comments

Comments
 (0)