Browse Source
Both Tomcat and Jetty can now be configured to use SSL via the environment (typically application.properties or application.yml) Closes #1084pull/1297/head
19 changed files with 777 additions and 80 deletions
@ -0,0 +1,56 @@ |
|||||||
|
<?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.2.0.BUILD-SNAPSHOT</version> |
||||||
|
</parent> |
||||||
|
<artifactId>spring-boot-sample-tomcat-ssl</artifactId> |
||||||
|
<name>Spring Boot Tomcat Sample</name> |
||||||
|
<description>Spring Boot Tomcat 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> |
||||||
|
</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> |
||||||
Binary file not shown.
@ -0,0 +1,34 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-2014 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; |
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication; |
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
||||||
|
import org.springframework.context.annotation.ComponentScan; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
|
||||||
|
@ComponentScan |
||||||
|
@Configuration |
||||||
|
@EnableAutoConfiguration |
||||||
|
@EnableConfigurationProperties |
||||||
|
public class SampleTomcatSslApplication { |
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception { |
||||||
|
SpringApplication.run(SampleTomcatSslApplication.class, args); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,32 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-2014 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.web; |
||||||
|
|
||||||
|
import org.springframework.stereotype.Controller; |
||||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||||
|
import org.springframework.web.bind.annotation.ResponseBody; |
||||||
|
|
||||||
|
@Controller |
||||||
|
public class SampleController { |
||||||
|
|
||||||
|
@RequestMapping("/") |
||||||
|
@ResponseBody |
||||||
|
public String helloWorld() { |
||||||
|
return "Hello, world"; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,4 @@ |
|||||||
|
server.port = 8443 |
||||||
|
server.ssl.key-store = sample.jks |
||||||
|
server.ssl.key-store-password = secret |
||||||
|
server.ssl.key-password = password |
||||||
@ -0,0 +1,66 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-2014 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; |
||||||
|
|
||||||
|
import org.apache.http.client.HttpClient; |
||||||
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
||||||
|
import org.apache.http.conn.ssl.SSLContextBuilder; |
||||||
|
import org.apache.http.conn.ssl.TrustSelfSignedStrategy; |
||||||
|
import org.apache.http.impl.client.HttpClients; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.springframework.beans.factory.annotation.Value; |
||||||
|
import org.springframework.boot.test.IntegrationTest; |
||||||
|
import org.springframework.boot.test.SpringApplicationConfiguration; |
||||||
|
import org.springframework.boot.test.TestRestTemplate; |
||||||
|
import org.springframework.http.HttpStatus; |
||||||
|
import org.springframework.http.ResponseEntity; |
||||||
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
||||||
|
import org.springframework.test.annotation.DirtiesContext; |
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
||||||
|
import org.springframework.test.context.web.WebAppConfiguration; |
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals; |
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class) |
||||||
|
@SpringApplicationConfiguration(classes = SampleTomcatSslApplication.class) |
||||||
|
@WebAppConfiguration |
||||||
|
@IntegrationTest("server.port:0") |
||||||
|
@DirtiesContext |
||||||
|
public class SampleTomcatSslApplicationTests { |
||||||
|
|
||||||
|
@Value("${local.server.port}") |
||||||
|
private int port; |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testHome() throws Exception { |
||||||
|
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( |
||||||
|
new SSLContextBuilder().loadTrustMaterial(null, |
||||||
|
new TrustSelfSignedStrategy()).build()); |
||||||
|
|
||||||
|
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory) |
||||||
|
.build(); |
||||||
|
|
||||||
|
TestRestTemplate testRestTemplate = new TestRestTemplate(); |
||||||
|
((HttpComponentsClientHttpRequestFactory) testRestTemplate.getRequestFactory()) |
||||||
|
.setHttpClient(httpClient); |
||||||
|
ResponseEntity<String> entity = testRestTemplate.getForEntity( |
||||||
|
"https://localhost:" + this.port, String.class); |
||||||
|
assertEquals(HttpStatus.OK, entity.getStatusCode()); |
||||||
|
assertEquals("Hello, world", entity.getBody()); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,120 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-2014 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; |
||||||
|
|
||||||
|
/** |
||||||
|
* Simple container-independent abstraction for SSL configuration. |
||||||
|
* |
||||||
|
* @author Andy Wilkinson |
||||||
|
* @since 1.2.0 |
||||||
|
*/ |
||||||
|
public class Ssl { |
||||||
|
|
||||||
|
private ClientAuth clientAuth; |
||||||
|
|
||||||
|
private String[] ciphers; |
||||||
|
|
||||||
|
private String keyAlias; |
||||||
|
|
||||||
|
private String keyPassword; |
||||||
|
|
||||||
|
private String keyStore; |
||||||
|
|
||||||
|
private String keyStorePassword; |
||||||
|
|
||||||
|
private String trustStore; |
||||||
|
|
||||||
|
private String trustStorePassword; |
||||||
|
|
||||||
|
private String protocol = "TLS"; |
||||||
|
|
||||||
|
public ClientAuth getClientAuth() { |
||||||
|
return this.clientAuth; |
||||||
|
} |
||||||
|
|
||||||
|
public void setClientAuth(ClientAuth clientAuth) { |
||||||
|
this.clientAuth = clientAuth; |
||||||
|
} |
||||||
|
|
||||||
|
public String[] getCiphers() { |
||||||
|
return this.ciphers; |
||||||
|
} |
||||||
|
|
||||||
|
public void setCiphers(String[] ciphers) { |
||||||
|
this.ciphers = ciphers; |
||||||
|
} |
||||||
|
|
||||||
|
public String getKeyAlias() { |
||||||
|
return this.keyAlias; |
||||||
|
} |
||||||
|
|
||||||
|
public void setKeyAlias(String keyAlias) { |
||||||
|
this.keyAlias = keyAlias; |
||||||
|
} |
||||||
|
|
||||||
|
public String getKeyPassword() { |
||||||
|
return this.keyPassword; |
||||||
|
} |
||||||
|
|
||||||
|
public void setKeyPassword(String keyPassword) { |
||||||
|
this.keyPassword = keyPassword; |
||||||
|
} |
||||||
|
|
||||||
|
public String getKeyStore() { |
||||||
|
return this.keyStore; |
||||||
|
} |
||||||
|
|
||||||
|
public void setKeyStore(String keyStore) { |
||||||
|
this.keyStore = keyStore; |
||||||
|
} |
||||||
|
|
||||||
|
public String getKeyStorePassword() { |
||||||
|
return this.keyStorePassword; |
||||||
|
} |
||||||
|
|
||||||
|
public void setKeyStorePassword(String keyStorePassword) { |
||||||
|
this.keyStorePassword = keyStorePassword; |
||||||
|
} |
||||||
|
|
||||||
|
public String getTrustStore() { |
||||||
|
return this.trustStore; |
||||||
|
} |
||||||
|
|
||||||
|
public void setTrustStore(String trustStore) { |
||||||
|
this.trustStore = trustStore; |
||||||
|
} |
||||||
|
|
||||||
|
public String getTrustStorePassword() { |
||||||
|
return this.trustStorePassword; |
||||||
|
} |
||||||
|
|
||||||
|
public void setTrustStorePassword(String trustStorePassword) { |
||||||
|
this.trustStorePassword = trustStorePassword; |
||||||
|
} |
||||||
|
|
||||||
|
public String getProtocol() { |
||||||
|
return this.protocol; |
||||||
|
} |
||||||
|
|
||||||
|
public void setProtocol(String protocol) { |
||||||
|
this.protocol = protocol; |
||||||
|
} |
||||||
|
|
||||||
|
public enum ClientAuth { |
||||||
|
WANT, NEED; |
||||||
|
} |
||||||
|
} |
||||||
Binary file not shown.
Loading…
Reference in new issue