Spring Boot應(yīng)用程序同時支持HTTP和HTTPS協(xié)議的實(shí)現(xiàn)方法
如今,企業(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ī)器上反饋如下:
可以看出,執(zhí)行完上述命令后在home目錄下多了一個新的.keystore文件。
實(shí)戰(zhàn)首先在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,讀者在實(shí)踐的時候要加上 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)用程序;在瀏覽器中訪問URLhttps://localhost:8443/internal/tomcat.https.properties
在瀏覽器中訪問URLhttp://localhost:8080/internal/application.properties
分析
根據(jù)之前的文章和官方文檔,Spring Boot已經(jīng)對外開放了很多服務(wù)器配置,這些配置信息通過Spring Boot內(nèi)部的ServerProperties類完成綁定,若要參考Spring Boot的通用配置項(xiàng),請點(diǎn)擊這里
Spring Boot不支持通過application.properties同時配置HTTP連接器和HTTPS連接器。在官方文檔70.8中提到一種方法,是將屬性值硬編碼在程序中。
因此我們這里新建一個配置文件tomcat.https.properties來實(shí)現(xiàn),但是這并不符合“Spring Boot風(fēng)格”,后續(xù)有可能應(yīng)該會支持“通過application.properties同時配置HTTP連接器和HTTPS連接器”。我添加的TomcatSslConnectorProperties是模仿Spring Boot中的ServerProperties的使用機(jī)制實(shí)現(xiàn)的,這里使用了自定義的屬性前綴custom.tomcat而沒有用現(xiàn)有的server.前綴,因?yàn)镾erverProperties禁止在其他的配置文件中使用該命名空間。
@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實(shí)例后,需要創(chuàng)建一個EmbeddedServletContainerFactory類型的Spring bean,用于創(chuàng)建EmbeddedServletContainer。
通過createSslConnector方法可以構(gòu)建一個包含了我們指定的屬性值的連接器,然后通過tomcat.addAdditionalTomcatConnectors(createSslConnector(properties));設(shè)置tomcat容器的HTTPS連接器。
參考資料配置SSL
Spring Boot 1.x系列
springboot整合redis進(jìn)行數(shù)據(jù)操作(推薦)
Spring Boot項(xiàng)目中如何定制HTTP消息轉(zhuǎn)換器
Spring Boot整合Mongodb提供Restful接口
總結(jié)
以上所述是小編給大家介紹的Spring Boot應(yīng)用程序同時支持HTTP和HTTPS協(xié)議的實(shí)現(xiàn)方法,希望對大家有所幫助!
- 使用Feign配置請求頭以及支持Https協(xié)議
- Nexus使用nginx代理實(shí)現(xiàn)支持HTTPS協(xié)議
- Spring Boot項(xiàng)目如何同時支持HTTP和HTTPS協(xié)議的實(shí)現(xiàn)
- SpringBoot2.0如何啟用https協(xié)議
- 關(guān)于Https協(xié)議和HttpClient的實(shí)現(xiàn)詳解
- startssl申請SSL證書 并且配置 iis 啟用https協(xié)議
- Java獲取http和https協(xié)議返回的json數(shù)據(jù)
- Linux下nginx配置https協(xié)議訪問的方法
- iOS9蘋果將原h(huán)ttp協(xié)議改成了https協(xié)議的方法
- apache中使用mod_gnutls模塊實(shí)現(xiàn)多個SSL站點(diǎn)配置(多個HTTPS協(xié)議的虛擬主機(jī))
- https協(xié)議詳解
相關(guān)文章
idea使用mybatis插件mapper中的方法爆紅的解決方案
這篇文章主要介紹了idea使用mybatis插件mapper中的方法爆紅的解決方案,文中給出了詳細(xì)的原因分析和解決方案,對大家解決問題有一定的幫助,需要的朋友可以參考下2024-07-07Java中Array List與Linked List的實(shí)現(xiàn)分析
這篇文章主要給大家介紹了關(guān)于Array List與Linked List實(shí)現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Spring Security基本架構(gòu)與初始化操作流程詳解
這篇文章主要介紹了Spring Security基本架構(gòu)與初始化操作流程,Spring Security是一個能夠?yàn)榛赟pring的企業(yè)應(yīng)用系統(tǒng)提供聲明式的安全訪問控制解決方案的安全框架2023-03-03基于JavaSwing+mysql開發(fā)一個學(xué)生社團(tuán)管理系統(tǒng)設(shè)計(jì)和實(shí)現(xiàn)
項(xiàng)目使用Java swing+mysql開發(fā),可實(shí)現(xiàn)基礎(chǔ)數(shù)據(jù)維護(hù)、用戶登錄注冊、社團(tuán)信息列表查看、社團(tuán)信息添加、社團(tuán)信息修改、社團(tuán)信息刪除以及退出注銷等功能、界面設(shè)計(jì)比較簡單易學(xué)、適合作為Java課設(shè)設(shè)計(jì)以及學(xué)習(xí)技術(shù)使用,需要的朋友參考下吧2021-08-08Spring Boot 2 實(shí)戰(zhàn):自定義啟動運(yùn)行邏輯實(shí)例詳解
這篇文章主要介紹了Spring Boot 2 實(shí)戰(zhàn):自定義啟動運(yùn)行邏輯,結(jié)合實(shí)例形式詳細(xì)分析了Spring Boot 2自定義啟動運(yùn)行邏輯詳細(xì)操作技巧與注意事項(xiàng),需要的朋友可以參考下2020-05-05Java8新特性stream和parallelStream區(qū)別
這篇文章主要介紹了Java8新特性stream和parallelStream區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11