Skip to content

Commit 55ac525

Browse files
committed
First working solution with two separate HttpComponentsClientHttpRequestFactorys to switch between RestTemplate, as it doesn´t seem to be possible to do it with one SSLContext (see http://mail-archives.apache.org/mod_mbox/hc-httpclient-users/201109.mbox/%3C1315998630.3176.17.camel@ubuntu%3E)
1 parent aee2af8 commit 55ac525

19 files changed

+74
-183
lines changed

README.md

+16-18
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Open your Browser with [http:localhost:8080/swagger-ui.html] and fire up a GET-R
4343
#### 1. Private Key: aliceprivate.key
4444

4545
```
46-
openssl genrsa -des3 -out aliceprivate.key 128
46+
openssl genrsa -des3 -out aliceprivate.key 1024
4747
```
4848

4949
- passphrase `alicepassword`
@@ -82,6 +82,12 @@ openssl pkcs12 -export -in alice.crt -inkey aliceprivate.key -certfile alice.crt
8282

8383
__the same password__ `alicepassword`
8484

85+
To read in KeyStore Explorer
86+
87+
```
88+
keytool -importkeystore -srckeystore alice-keystore.p12 -srcstoretype pkcs12 -destkeystore alice-keystore.jks -deststoretype JKS
89+
```
90+
8591

8692

8793
## server-tom keys and client certificate, truststore & keystore (see /server-tom/src/main/resources)
@@ -148,25 +154,11 @@ In KeyStore Explorer this should look like this:
148154

149155
#### 2. Java Keystore, that inherits Public and Private Keys (keypair): client-keystore.p12
150156

151-
Openssl CLI sadly doesn´t support importing multiple certificate files... But we can [concatenate them](https://serverfault.com/a/483490/326340):
152-
153-
```
154-
cat alice.crt tom.crt > allcerts.pem
155-
cat aliceprivate.key tomprivate.key > allkeys.pem
156-
```
157-
158-
Then we can do:
157+
We need a way to import multiple private keys and certificates into the same `client-keystore.jks`, so that our implementation could call multiple secured endpoints. This seems to be a harder task then one could think beforehand. But luckily there´s a simple way: Just copy both `alice-keystore.p12` and `tom-keystore.p12` into __client-bob/src/main/resources__ and use keytool as follows:
159158

160159
```
161-
openssl pkcs12 -export -in allcerts.pem -inkey allkeys.pem -certfile allcerts.pem -name "alicecert" -out client-keystore.p12
162-
```
163-
164-
__the same password__ `bobpassword`
165-
166-
If you want to check everything worked fine in KeyStoreExplorer, you have to convert the `.p12` file into a `.jks`, otherwise the tool will bring up a nasty exception:
167-
168-
```
169-
keytool -importkeystore -srckeystore client-keystore.p12 -srcstoretype pkcs12 -destkeystore client-keystore.jks -deststoretype JKS
160+
keytool -importkeystore -srckeystore alice-keystore.p12 -srcstoretype pkcs12 -destkeystore client-keystore.jks -deststoretype JKS
161+
keytool -importkeystore -srckeystore tom-keystore.p12 -srcstoretype pkcs12 -destkeystore client-keystore.jks -deststoretype JKS
170162
```
171163

172164
The result should look like this:
@@ -189,3 +181,9 @@ https://stackoverflow.com/questions/21488845/how-can-i-generate-a-self-signed-ce
189181
--> this is not the only solution, see `-extfile` and `-extensions` CLI paramters!
190182

191183
https://serverfault.com/questions/779475/openssl-add-subject-alternate-name-san-when-signing-with-ca
184+
185+
#### Multiple certificates handling in Java Keystores:
186+
187+
Look into the documentation of Tomcat in section `keyAlias`: http://tomcat.apache.org/tomcat-6.0-doc/config/http.html#SSL_Support
188+
189+
https://stackoverflow.com/questions/5292074/how-to-specify-outbound-certificate-alias-for-https-calls
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
11
package de.jonashackt.client;
22

33
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
45
import org.springframework.stereotype.Component;
56
import org.springframework.web.client.RestTemplate;
67

78
@Component
89
public class ServerClientImpl implements ServerClient {
910

11+
//@Autowired
12+
private RestTemplate restTemplate = new RestTemplate();
13+
14+
@Autowired
15+
private HttpComponentsClientHttpRequestFactory serverAliceClientHttpRequestFactory;
16+
1017
@Autowired
11-
private RestTemplate restTemplate;
18+
private HttpComponentsClientHttpRequestFactory serverTomClientHttpRequestFactory;
1219

1320
@Override
1421
public String callServerAlice() {
22+
restTemplate.setRequestFactory(serverAliceClientHttpRequestFactory);
23+
1524
return restTemplate.getForObject("https://server-alice:8443/hello", String.class);
1625
}
1726

1827
@Override
1928
public String callServerTom() {
29+
restTemplate.setRequestFactory(serverTomClientHttpRequestFactory);
30+
2031
return restTemplate.getForObject("https://server-tom:8443/hello", String.class);
2132
}
2233
}

client-bob/src/main/java/de/jonashackt/configuration/RestClientCertConfiguration.java

+46-4
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,30 @@ public class RestClientCertConfiguration {
2121

2222
private char[] bobPassword = "bobpassword".toCharArray();
2323

24-
@Value("classpath:client-keystore.p12")
25-
private Resource keystoreResource;
24+
@Value("classpath:alice-keystore.p12")
25+
private Resource aliceKeystoreResource;
26+
27+
@Value("classpath:alice-truststore.jks")
28+
private Resource aliceTruststoreResource;
29+
30+
@Value("classpath:tom-keystore.p12")
31+
private Resource tomKeystoreResource;
32+
33+
@Value("classpath:tom-truststore.jks")
34+
private Resource tomTruststoreResource;
2635

2736
@Value("classpath:client-truststore.jks")
2837
private Resource truststoreResource;
2938

30-
@Bean
39+
/* @Bean
3140
public RestTemplate restTemplate(RestTemplateBuilder builder) throws Exception {
3241
42+
43+
3344
SSLContext sslContext = SSLContextBuilder
3445
.create()
35-
.loadKeyMaterial(inStream2File(keystoreResource), bobPassword, bobPassword)
46+
.loadKeyMaterial(inStream2File(aliceKeystoreResource), "alicepassword".toCharArray(), "alicepassword".toCharArray())
47+
.loadKeyMaterial(inStream2File(tomKeystoreResource), "tompassword".toCharArray(), "tompassword".toCharArray())
3648
.loadTrustMaterial(inStream2File(truststoreResource), bobPassword)
3749
.build();
3850
@@ -43,6 +55,36 @@ public RestTemplate restTemplate(RestTemplateBuilder builder) throws Exception {
4355
return builder
4456
.requestFactory(new HttpComponentsClientHttpRequestFactory(client))
4557
.build();
58+
}*/
59+
60+
@Bean
61+
public HttpComponentsClientHttpRequestFactory serverTomClientHttpRequestFactory() throws Exception {
62+
SSLContext sslContext = SSLContextBuilder
63+
.create()
64+
.loadKeyMaterial(inStream2File(tomKeystoreResource), "tompassword".toCharArray(), "tompassword".toCharArray())
65+
.loadTrustMaterial(inStream2File(tomTruststoreResource), "tompassword".toCharArray())
66+
.build();
67+
68+
HttpClient client = HttpClients.custom()
69+
.setSSLContext(sslContext)
70+
.build();
71+
72+
return new HttpComponentsClientHttpRequestFactory(client);
73+
}
74+
75+
@Bean
76+
public HttpComponentsClientHttpRequestFactory serverAliceClientHttpRequestFactory() throws Exception {
77+
SSLContext sslContext = SSLContextBuilder
78+
.create()
79+
.loadKeyMaterial(inStream2File(aliceKeystoreResource), "alicepassword".toCharArray(), "alicepassword".toCharArray())
80+
.loadTrustMaterial(inStream2File(aliceTruststoreResource), "alicepassword".toCharArray())
81+
.build();
82+
83+
HttpClient client = HttpClients.custom()
84+
.setSSLContext(sslContext)
85+
.build();
86+
87+
return new HttpComponentsClientHttpRequestFactory(client);
4688
}
4789

4890
private File inStream2File(Resource resource) {
2.45 KB
Binary file not shown.
749 Bytes
Binary file not shown.

client-bob/src/main/resources/alice.crt

-16
This file was deleted.

client-bob/src/main/resources/alice.csr

-12
This file was deleted.

client-bob/src/main/resources/aliceprivate.key

-18
This file was deleted.

client-bob/src/main/resources/allcerts.pem

-32
This file was deleted.

client-bob/src/main/resources/allkeys.pem

-36
This file was deleted.
98 Bytes
Binary file not shown.
-3.66 KB
Binary file not shown.
Binary file not shown.
2.42 KB
Binary file not shown.
737 Bytes
Binary file not shown.

client-bob/src/main/resources/tom.crt

-16
This file was deleted.

client-bob/src/main/resources/tom.csr

-12
This file was deleted.

client-bob/src/main/resources/tomprivate.key

-18
This file was deleted.
Binary file not shown.

0 commit comments

Comments
 (0)