Browse Source
This commit changes the default version of Tomcat to 8.5.3 while also retaining support for Tomcat 8.0 and 7.0. The main difference in 8.5 is that the ServerSocketFactory abstraction that allowed the TrustStore and KeyStore to be configured programatically no longer exists. This logic has been replaced with the use of a custom URL protocol (springbootssl) that provides access to the key store and trust store of an SslStoreProvider. In addition to working with 8.5, this approach has the advantage of also working with 8.0 and 7.0. Closes gh-6164pull/6191/merge
28 changed files with 521 additions and 154 deletions
@ -0,0 +1,57 @@
@@ -0,0 +1,57 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<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"> |
||||
<modelVersion>4.0.0</modelVersion> |
||||
<parent> |
||||
<!-- Your own application should inherit from spring-boot-starter-parent --> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-samples</artifactId> |
||||
<version>1.4.0.BUILD-SNAPSHOT</version> |
||||
</parent> |
||||
<artifactId>spring-boot-sample-tomcat7-ssl</artifactId> |
||||
<name>Spring Boot Tomcat 7 SSL Sample</name> |
||||
<description>Spring Boot Tomcat 7 SSL Sample</description> |
||||
<url>http://projects.spring.io/spring-boot/</url> |
||||
<organization> |
||||
<name>Pivotal Software, Inc.</name> |
||||
<url>http://www.spring.io</url> |
||||
</organization> |
||||
<properties> |
||||
<main.basedir>${basedir}/../..</main.basedir> |
||||
<tomcat.version>7.0.69</tomcat.version> |
||||
</properties> |
||||
<dependencies> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-tomcat</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework</groupId> |
||||
<artifactId>spring-webmvc</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.apache.httpcomponents</groupId> |
||||
<artifactId>httpclient</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-test</artifactId> |
||||
<scope>test</scope> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.yaml</groupId> |
||||
<artifactId>snakeyaml</artifactId> |
||||
</dependency> |
||||
</dependencies> |
||||
<build> |
||||
<plugins> |
||||
<plugin> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-maven-plugin</artifactId> |
||||
</plugin> |
||||
</plugins> |
||||
</build> |
||||
</project> |
||||
@ -0,0 +1,29 @@
@@ -0,0 +1,29 @@
|
||||
/* |
||||
* Copyright 2012-2015 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package sample.tomcat.ssl; |
||||
|
||||
import org.springframework.boot.SpringApplication; |
||||
import org.springframework.boot.autoconfigure.SpringBootApplication; |
||||
|
||||
@SpringBootApplication |
||||
public class SampleTomcatSslApplication { |
||||
|
||||
public static void main(String[] args) throws Exception { |
||||
SpringApplication.run(SampleTomcatSslApplication.class, args); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,32 @@
@@ -0,0 +1,32 @@
|
||||
/* |
||||
* Copyright 2012-2016 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package sample.tomcat.ssl.web; |
||||
|
||||
import org.springframework.stereotype.Controller; |
||||
import org.springframework.web.bind.annotation.GetMapping; |
||||
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
||||
@Controller |
||||
public class SampleController { |
||||
|
||||
@GetMapping("/") |
||||
@ResponseBody |
||||
public String helloWorld() { |
||||
return "Hello, world"; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,4 @@
@@ -0,0 +1,4 @@
|
||||
server.port = 8443 |
||||
server.ssl.key-store = classpath:sample.jks |
||||
server.ssl.key-store-password = secret |
||||
server.ssl.key-password = password |
||||
Binary file not shown.
@ -0,0 +1,51 @@
@@ -0,0 +1,51 @@
|
||||
/* |
||||
* Copyright 2012-2016 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package sample.tomcat.ssl; |
||||
|
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
|
||||
import org.springframework.boot.context.embedded.LocalServerPort; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
||||
import org.springframework.boot.test.web.client.TestRestTemplate; |
||||
import org.springframework.boot.test.web.client.TestRestTemplate.HttpClientOption; |
||||
import org.springframework.http.HttpStatus; |
||||
import org.springframework.http.ResponseEntity; |
||||
import org.springframework.test.annotation.DirtiesContext; |
||||
import org.springframework.test.context.junit4.SpringRunner; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
@RunWith(SpringRunner.class) |
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) |
||||
@DirtiesContext |
||||
public class SampleTomcatSslApplicationTests { |
||||
|
||||
@LocalServerPort |
||||
private int port; |
||||
|
||||
@Test |
||||
public void testHome() throws Exception { |
||||
TestRestTemplate testRestTemplate = new TestRestTemplate(HttpClientOption.SSL); |
||||
ResponseEntity<String> entity = testRestTemplate |
||||
.getForEntity("https://localhost:" + this.port, String.class); |
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); |
||||
assertThat(entity.getBody()).isEqualTo("Hello, world"); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,57 @@
@@ -0,0 +1,57 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<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"> |
||||
<modelVersion>4.0.0</modelVersion> |
||||
<parent> |
||||
<!-- Your own application should inherit from spring-boot-starter-parent --> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-samples</artifactId> |
||||
<version>1.4.0.BUILD-SNAPSHOT</version> |
||||
</parent> |
||||
<artifactId>spring-boot-sample-tomcat-ssl</artifactId> |
||||
<name>Spring Boot Tomcat 8.0 SSL Sample</name> |
||||
<description>Spring Boot Tomcat 8.0 SSL Sample</description> |
||||
<url>http://projects.spring.io/spring-boot/</url> |
||||
<organization> |
||||
<name>Pivotal Software, Inc.</name> |
||||
<url>http://www.spring.io</url> |
||||
</organization> |
||||
<properties> |
||||
<main.basedir>${basedir}/../..</main.basedir> |
||||
<tomcat.version>8.0.33</tomcat.version> |
||||
</properties> |
||||
<dependencies> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-tomcat</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework</groupId> |
||||
<artifactId>spring-webmvc</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.apache.httpcomponents</groupId> |
||||
<artifactId>httpclient</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-test</artifactId> |
||||
<scope>test</scope> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.yaml</groupId> |
||||
<artifactId>snakeyaml</artifactId> |
||||
</dependency> |
||||
</dependencies> |
||||
<build> |
||||
<plugins> |
||||
<plugin> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-maven-plugin</artifactId> |
||||
</plugin> |
||||
</plugins> |
||||
</build> |
||||
</project> |
||||
@ -0,0 +1,29 @@
@@ -0,0 +1,29 @@
|
||||
/* |
||||
* Copyright 2012-2015 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package sample.tomcat.ssl; |
||||
|
||||
import org.springframework.boot.SpringApplication; |
||||
import org.springframework.boot.autoconfigure.SpringBootApplication; |
||||
|
||||
@SpringBootApplication |
||||
public class SampleTomcatSslApplication { |
||||
|
||||
public static void main(String[] args) throws Exception { |
||||
SpringApplication.run(SampleTomcatSslApplication.class, args); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,32 @@
@@ -0,0 +1,32 @@
|
||||
/* |
||||
* Copyright 2012-2016 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package sample.tomcat.ssl.web; |
||||
|
||||
import org.springframework.stereotype.Controller; |
||||
import org.springframework.web.bind.annotation.GetMapping; |
||||
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
||||
@Controller |
||||
public class SampleController { |
||||
|
||||
@GetMapping("/") |
||||
@ResponseBody |
||||
public String helloWorld() { |
||||
return "Hello, world"; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,4 @@
@@ -0,0 +1,4 @@
|
||||
server.port = 8443 |
||||
server.ssl.key-store = classpath:sample.jks |
||||
server.ssl.key-store-password = secret |
||||
server.ssl.key-password = password |
||||
Binary file not shown.
@ -0,0 +1,51 @@
@@ -0,0 +1,51 @@
|
||||
/* |
||||
* Copyright 2012-2016 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package sample.tomcat.ssl; |
||||
|
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
|
||||
import org.springframework.boot.context.embedded.LocalServerPort; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
||||
import org.springframework.boot.test.web.client.TestRestTemplate; |
||||
import org.springframework.boot.test.web.client.TestRestTemplate.HttpClientOption; |
||||
import org.springframework.http.HttpStatus; |
||||
import org.springframework.http.ResponseEntity; |
||||
import org.springframework.test.annotation.DirtiesContext; |
||||
import org.springframework.test.context.junit4.SpringRunner; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
@RunWith(SpringRunner.class) |
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) |
||||
@DirtiesContext |
||||
public class SampleTomcatSslApplicationTests { |
||||
|
||||
@LocalServerPort |
||||
private int port; |
||||
|
||||
@Test |
||||
public void testHome() throws Exception { |
||||
TestRestTemplate testRestTemplate = new TestRestTemplate(HttpClientOption.SSL); |
||||
ResponseEntity<String> entity = testRestTemplate |
||||
.getForEntity("https://localhost:" + this.port, String.class); |
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); |
||||
assertThat(entity.getBody()).isEqualTo("Hello, world"); |
||||
} |
||||
|
||||
} |
||||
@ -1,98 +0,0 @@
@@ -1,98 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2016 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.boot.context.embedded.tomcat; |
||||
|
||||
import java.io.IOException; |
||||
import java.security.KeyStore; |
||||
|
||||
import org.apache.tomcat.util.net.AbstractEndpoint; |
||||
import org.apache.tomcat.util.net.SSLUtil; |
||||
import org.apache.tomcat.util.net.ServerSocketFactory; |
||||
import org.apache.tomcat.util.net.jsse.JSSEImplementation; |
||||
import org.apache.tomcat.util.net.jsse.JSSESocketFactory; |
||||
|
||||
import org.springframework.boot.context.embedded.SslStoreProvider; |
||||
|
||||
/** |
||||
* {@link JSSEImplementation} for embedded Tomcat that supports {@link SslStoreProvider}. |
||||
* |
||||
* @author Phillip Webb |
||||
* @author Venil Noronha |
||||
* @since 1.4.0 |
||||
*/ |
||||
public class TomcatEmbeddedJSSEImplementation extends JSSEImplementation { |
||||
|
||||
@Override |
||||
public ServerSocketFactory getServerSocketFactory(AbstractEndpoint<?> endpoint) { |
||||
return new SocketFactory(endpoint); |
||||
} |
||||
|
||||
@Override |
||||
public SSLUtil getSSLUtil(AbstractEndpoint<?> endpoint) { |
||||
return new SocketFactory(endpoint); |
||||
} |
||||
|
||||
/** |
||||
* {@link JSSESocketFactory} that supports {@link SslStoreProvider}. |
||||
*/ |
||||
static class SocketFactory extends JSSESocketFactory { |
||||
|
||||
private final SslStoreProvider sslStoreProvider; |
||||
|
||||
SocketFactory(AbstractEndpoint<?> endpoint) { |
||||
super(endpoint); |
||||
this.sslStoreProvider = (SslStoreProvider) endpoint |
||||
.getAttribute("sslStoreProvider"); |
||||
} |
||||
|
||||
@Override |
||||
protected KeyStore getKeystore(String type, String provider, String pass) |
||||
throws IOException { |
||||
if (this.sslStoreProvider != null) { |
||||
try { |
||||
KeyStore store = this.sslStoreProvider.getKeyStore(); |
||||
if (store != null) { |
||||
return store; |
||||
} |
||||
} |
||||
catch (Exception ex) { |
||||
throw new IOException(ex); |
||||
} |
||||
} |
||||
return super.getKeystore(type, provider, pass); |
||||
} |
||||
|
||||
@Override |
||||
protected KeyStore getTrustStore(String keystoreType, String keystoreProvider) |
||||
throws IOException { |
||||
if (this.sslStoreProvider != null) { |
||||
try { |
||||
KeyStore store = this.sslStoreProvider.getTrustStore(); |
||||
if (store != null) { |
||||
return store; |
||||
} |
||||
} |
||||
catch (Exception ex) { |
||||
throw new IOException(ex); |
||||
} |
||||
} |
||||
return super.getTrustStore(keystoreType, keystoreProvider); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue