Spring Boot 支持 HTTPS 如此簡單,So easy!


這里講的是 Spring Boot 內嵌式 Server 打 jar 包運行的方式,打 WAR 包部署的就不存在要 Spring Boot 支持 HTTPS 了,需要去外部對應的 Server 配置。

你所需具備的基礎

更多請在Java技術棧微信公眾號后台回復關鍵字:boot。

支持 HTTPS

Spring Boot 配置 SSL 很簡單,只需要通過一系列的 server.ssl.* 參數即可完成配置,如下所示。

application.properties 配置文件參考配置:

server.port=8443
server.ssl.protocol=TLS
server.ssl.key-store=classpath:javastack.keystore
server.ssl.key-store-password=javastack
server.ssl.key-store-type=JKS

如何在本地測試創建證書請參考Java技術棧微信公眾號的這篇文章《一分鍾開啟Tomcat https支持》,把生成完的證書復制到 Spring Boot 項目中的 resources 目錄即可。

這邊只是提供了一個 SSL 單向驗證的演示,更多 SSL 參數配置如下。

server.ssl.ciphers= # Supported SSL ciphers.
server.ssl.client-auth= # Whether client authentication is wanted ("want") or needed ("need"). Requires a trust store.
server.ssl.enabled= # Enable SSL support.
server.ssl.enabled-protocols= # Enabled SSL protocols.
server.ssl.key-alias= # Alias that identifies the key in the key store.
server.ssl.key-password= # Password used to access the key in the key store.
server.ssl.key-store= # Path to the key store that holds the SSL certificate (typically a jks file).
server.ssl.key-store-password= # Password used to access the key store.
server.ssl.key-store-provider= # Provider for the key store.
server.ssl.key-store-type= # Type of the key store.
server.ssl.protocol=TLS # SSL protocol to use.
server.ssl.trust-store= # Trust store that holds SSL certificates.
server.ssl.trust-store-password= # Password used to access the trust store.
server.ssl.trust-store-provider= # Provider for the trust store.
server.ssl.trust-store-type= # Type of the trust store.

參數對應的類:org.springframework.boot.web.server.Ssl

上面的例子配置后就能開啟 HTTPS 了,默認的 HTTP 協議就不再支持了,Spring Boot 不支持以配置文件配置的方式同時支持 HTTP 和 HTTPS。

如何同時支持?

如果你需要同時支持 HTTP 和 HTTPS 這兩個協議,就需要把另外一個協議用程序化的方式來配置。因為通過程序的方式配置 HTTP 協議更加簡單一點,所以,Spring Boot 推薦的做法是把 HTTPS 配置在配置文件,HTTP 通過程序來配置。

來,下面示例就是通過程序的方式來額外支持 HTTP 協議。

@SpringBootApplication
public class JavastackApplication {

	@Bean
	public ServletWebServerFactory servletContainer() {
		TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
		tomcat.addAdditionalTomcatConnectors(createStandardConnector());
		return tomcat;
	}

	private Connector createStandardConnector() {
		Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
		connector.setPort(8080);
		return connector;
	}

	public static void main(String[] args) {
		SpringApplication.run(JavastackApplication.class, args);
	}

}

啟動 Spring Boot 之后就會看到下面的同時支持兩個協議日志。

Tomcat started on port(s): 8443 (https) 8080 (http) with context path '/'

Spring Boot 支持 HTTPS 如此簡單,開發現在把運維的事都做了……

好了,今天的分享就到這里,更多 Spring Boot 文章正在撰寫中,關注Java技術棧微信公眾號獲取第一時間推送。

在公眾號后台回復:boot,還能獲取棧長整理的往期 Spring Boot 教程,都是實戰干貨,以下僅為部分預覽。

  • Spring Boot 讀取配置的幾種方式
  • Spring Boot 如何做參數校驗?
  • Spring Boot 最核心的 25 個注解!
  • Spring Boot 2.x 啟動全過程源碼分析
  • Spring Boot 2.x 新特性總結及遷移指南
  • ……

本文原創首發於微信公眾號:Java技術棧(id:javastack),轉載請原樣保留本信息。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM