如今,企業(yè)級應(yīng)用程序的常見場景是同時支持HTTP和HTTPS兩種協(xié)議,這篇文章考慮如何讓Spring Boot應(yīng)用程序同時支持HTTP和HTTPS兩種協(xié)議。
準(zhǔn)備
為了使用HTTPS連接器,需要生成一份Certificate keystore,用于加密和機(jī)密瀏覽器的SSL溝通。
如果你使用Unix或者M(jìn)ac OS,可以通過下列命令:keytool -genkey -alias tomcat -keyalg RSA ,在生成過程中可能需要你填入一些自己的信息,例如我的機(jī)器上反饋如下:
 生成kestore
可以看出,執(zhí)行完上述命令后在home目錄下多了一個新的.keystore文件。
How Do
- 首先在resources目錄下新建一個配置文件tomcat.https.properties,用于存放HTTPS的配置信息;
custom.tomcat.https.port=8443
custom.tomcat.https.secure=true
custom.tomcat.https.scheme=https
custom.tomcat.https.ssl=true
custom.tomcat.https.keystore=${user.home}/.keystore
custom.tomcat.https.keystore-password=changeit
- 然后在WebConfiguration類中創(chuàng)建一個靜態(tài)類TomcatSslConnectorProperties;
@ConfigurationProperties(prefix = "custom.tomcat.https")
public static class TomcatSslConnectorProperties {
private Integer port;
private Boolean ssl = true;
private Boolean secure = true;
private String scheme = "https";
private File keystore;
private String keystorePassword;
//這里為了節(jié)省空間,省略了getters和setters,讀者在實踐的時候要加上
public void configureConnector(Connector connector) {
if (port != null) {
connector.setPort(port);
}
if (secure != null) {
connector.setSecure(secure);
}
if (scheme != null) {
connector.setScheme(scheme);
}
if (ssl != null) {
connector.setProperty("SSLEnabled", ssl.toString());
}
if (keystore != null && keystore.exists()) {
connector.setProperty("keystoreFile", keystore.getAbsolutePath());
connector.setProperty("keystorePassword", keystorePassword);
}
}
}
- 通過注解加載tomcat.https.properties配置文件,并與TomcatSslConnectorProperties綁定,用注解修飾WebConfiguration類;
@Configuration
@PropertySource("classpath:/tomcat.https.properties")
@EnableConfigurationProperties(WebConfiguration.TomcatSslConnectorProperties.class)
public class WebConfiguration extends WebMvcConfigurerAdapter {...}
- 在WebConfiguration類中創(chuàng)建EmbeddedServletContainerFactory類型的Srping bean,并用它添加之前創(chuàng)建的HTTPS連接器。
@Bean
public EmbeddedServletContainerFactory servletContainer(TomcatSslConnectorProperties properties) {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
tomcat.addAdditionalTomcatConnectors(createSslConnector(properties));
return tomcat;
}
private Connector createSslConnector(TomcatSslConnectorProperties properties) {
Connector connector = new Connector();
properties.configureConnector(connector);
return connector;
}
- 通過
mvn spring-boot:run 啟動應(yīng)用程序; - 在瀏覽器中訪問URL
https://localhost:8443/internal/tomcat.https.properties
 支持HTTPS協(xié)議
- 在瀏覽器中訪問URL
http://localhost:8080/internal/application.properties
 同時支持HTTP協(xié)議
分析
根據(jù)之前的文章和官方文檔,Spring Boot已經(jīng)對外開放了很多服務(wù)器配置,這些配置信息通過Spring Boot內(nèi)部的ServerProperties類完成綁定,若要參考Spring Boot的通用配置項,請點擊這里
Spring Boot不支持通過application.properties同時配置HTTP連接器和HTTPS連接器。在官方文檔70.8中提到一種方法,是將屬性值硬編碼在程序中。
因此我們這里新建一個配置文件tomcat.https.properties來實現(xiàn),但是這并不符合“Spring Boot風(fēng)格”,后續(xù)有可能應(yīng)該會支持“通過application.properties同時配置HTTP連接器和HTTPS連接器”。我添加的TomcatSslConnectorProperties是模仿Spring Boot中的ServerProperties的使用機(jī)制實現(xiàn)的,這里使用了自定義的屬性前綴custom.tomcat而沒有用現(xiàn)有的server.前綴,因為ServerProperties禁止在其他的配置文件中使用該命名空間。
@ConfigurationProperties(prefix = "custom.tomcat.https")這個注解會讓Spring Boot自動將custom.tomcat.https開頭的屬性綁定到TomcatSslConnectorProperties這個類的成員上(確保該類的getters和setters存在)。值得一提的是,在綁定過程中Spring Boot會自動將屬性值轉(zhuǎn)換成合適的數(shù)據(jù)類型,例如custom.tomcat.https.keystore的值會自動綁定到File對象keystore上。
使用@PropertySource("classpath:/tomcat.https.properties")來讓Spring Boot加載tomcat.https.properties文件中的屬性。
使用@EnableConfigurationProperties(WebConfiguration.TomcatSslConnectorProperties.class)讓Spring Boot自動創(chuàng)建一個屬性對象,包含上述通過@PropertySource導(dǎo)入的屬性。
在屬性值導(dǎo)入內(nèi)存,并構(gòu)建好TomcatSslConnectorProperties實例后,需要創(chuàng)建一個EmbeddedServletContainerFactory類型的Spring bean,用于創(chuàng)建EmbeddedServletContainer。
通過createSslConnector方法可以構(gòu)建一個包含了我們指定的屬性值的連接器,然后通過tomcat.addAdditionalTomcatConnectors(createSslConnector(properties));設(shè)置tomcat容器的HTTPS連接器。
參考資料
- 配置SSL
|