溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Spring Boot 中怎么支持 HTTPS

發(fā)布時(shí)間:2021-07-30 14:32:22 來源:億速云 閱讀:124 作者:Leah 欄目:大數(shù)據(jù)

本篇文章為大家展示了Spring Boot 中怎么支持 HTTPS,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

支持 HTTPS

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

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

如何在本地測(cè)試創(chuàng)建證書請(qǐng)參考Java技術(shù)棧微信公眾號(hào)的這篇文章《一分鐘開啟Tomcat https支持》,把生成完的證書復(fù)制到 Spring Boot 項(xiàng)目中的 resources 目錄即可。

這邊只是提供了一個(gè) SSL 單向驗(yàn)證的演示,更多 SSL 參數(shù)配置如下。

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.

參數(shù)對(duì)應(yīng)的類:org.springframework.boot.web.server.Ssl

上面的例子配置后就能開啟 HTTPS 了,默認(rèn)的 HTTP 協(xié)議就不再支持了,Spring Boot 不支持以配置文件配置的方式同時(shí)支持 HTTP 和 HTTPS。

如何同時(shí)支持?

如果你需要同時(shí)支持 HTTP 和 HTTPS 這兩個(gè)協(xié)議,就需要把另外一個(gè)協(xié)議用程序化的方式來配置。因?yàn)橥ㄟ^程序的方式配置 HTTP 協(xié)議更加簡(jiǎn)單一點(diǎn),所以,Spring Boot 推薦的做法是把 HTTPS 配置在配置文件,HTTP 通過程序來配置。

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

@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);
	}

}

啟動(dòng) Spring Boot 之后就會(huì)看到下面的同時(shí)支持兩個(gè)協(xié)議日志。

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

上述內(nèi)容就是Spring Boot 中怎么支持 HTTPS,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI