Skip to content

Commit 05b501c

Browse files
committed
Update Tomcat multi-connectors sample to configure SSL declaratively
The documentation recommends configuring the HTTP connector in code and using application.properties to configure the HTTPS connector as it's easier. This commit updates the sample to follow that recommendation. Closes gh-4342
1 parent 7c1bf58 commit 05b501c

File tree

5 files changed

+17
-47
lines changed

5 files changed

+17
-47
lines changed

spring-boot-samples/spring-boot-sample-tomcat-multi-connectors/src/main/java/sample/tomcat/SampleTomcatTwoConnectorsApplication.java

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,26 +16,20 @@
1616

1717
package sample.tomcat;
1818

19-
import java.io.File;
20-
import java.io.FileOutputStream;
21-
import java.io.IOException;
22-
2319
import org.apache.catalina.connector.Connector;
24-
import org.apache.coyote.http11.Http11NioProtocol;
2520

2621
import org.springframework.boot.SpringApplication;
2722
import org.springframework.boot.autoconfigure.SpringBootApplication;
2823
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
2924
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
3025
import org.springframework.context.annotation.Bean;
31-
import org.springframework.core.io.ClassPathResource;
32-
import org.springframework.util.FileCopyUtils;
3326
import org.springframework.util.SocketUtils;
3427

3528
/**
36-
* Sample Application to show Tomcat running 2 connectors
29+
* Sample Application to show Tomcat running two connectors
3730
*
3831
* @author Brock Mills
32+
* @author Andy Wilkinson
3933
*/
4034
@SpringBootApplication
4135
public class SampleTomcatTwoConnectorsApplication {
@@ -54,37 +48,8 @@ public EmbeddedServletContainerFactory servletContainer() {
5448

5549
private Connector createSslConnector() {
5650
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
57-
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
58-
try {
59-
File keystore = getKeyStoreFile();
60-
File truststore = keystore;
61-
connector.setScheme("https");
62-
connector.setSecure(true);
63-
connector.setPort(port());
64-
protocol.setSSLEnabled(true);
65-
protocol.setKeystoreFile(keystore.getAbsolutePath());
66-
protocol.setKeystorePass("changeit");
67-
protocol.setTruststoreFile(truststore.getAbsolutePath());
68-
protocol.setTruststorePass("changeit");
69-
protocol.setKeyAlias("apitester");
70-
return connector;
71-
}
72-
catch (IOException ex) {
73-
throw new IllegalStateException("cant access keystore: [" + "keystore"
74-
+ "] or truststore: [" + "keystore" + "]", ex);
75-
}
76-
}
77-
78-
private File getKeyStoreFile() throws IOException {
79-
ClassPathResource resource = new ClassPathResource("keystore");
80-
try {
81-
return resource.getFile();
82-
}
83-
catch (Exception ex) {
84-
File temp = File.createTempFile("keystore", ".tmp");
85-
FileCopyUtils.copy(resource.getInputStream(), new FileOutputStream(temp));
86-
return temp;
87-
}
51+
connector.setPort(port());
52+
return connector;
8853
}
8954

9055
public static void main(String[] args) throws Exception {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
server.port = 8443
2+
server.ssl.key-store = classpath:sample.jks
3+
server.ssl.key-store-password = secret
4+
server.ssl.key-password = password

spring-boot-samples/spring-boot-sample-tomcat-multi-connectors/src/test/java/sample/tomcat/SampleTomcatTwoConnectorsApplicationTests.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,9 +46,10 @@
4646
import static org.junit.Assert.assertEquals;
4747

4848
/**
49-
* Basic integration tests for 2 connector demo application.
49+
* Basic integration tests for {@link SampleTomcatTwoConnectorsApplication}.
5050
*
5151
* @author Brock Mills
52+
* @author Andy Wilkinson
5253
*/
5354
@RunWith(SpringJUnit4ClassRunner.class)
5455
@SpringApplicationConfiguration(classes = SampleTomcatTwoConnectorsApplication.class)
@@ -109,14 +110,14 @@ public boolean verify(final String hostname,
109110
});
110111
template.setRequestFactory(factory);
111112

112-
ResponseEntity<String> entity = template
113-
.getForEntity("http://localhost:" + this.port + "/hello", String.class);
113+
ResponseEntity<String> entity = template.getForEntity(
114+
"http://localhost:" + this.context.getBean("port") + "/hello",
115+
String.class);
114116
assertEquals(HttpStatus.OK, entity.getStatusCode());
115117
assertEquals("hello", entity.getBody());
116118

117-
ResponseEntity<String> httpsEntity = template.getForEntity(
118-
"https://localhost:" + this.context.getBean("port") + "/hello",
119-
String.class);
119+
ResponseEntity<String> httpsEntity = template
120+
.getForEntity("https://localhost:" + this.port + "/hello", String.class);
120121
assertEquals(HttpStatus.OK, httpsEntity.getStatusCode());
121122
assertEquals("hello", httpsEntity.getBody());
122123

0 commit comments

Comments
 (0)