0:目標(biāo):http://localhost訪問直接跳轉(zhuǎn)到https://localhost的訪問
2.組件相關(guān)邏輯參考:
???? https://cloud.tencent.com/document/product/400/4143
3.使用jdk自帶的 keytools 創(chuàng)建證書
http://www.reibang.com/p/8d4aba3b972d
4.核心代碼
https://www.iteye.com/blog/wallimn-2425837
一临谱、啟動程序?
Java代碼
@SpringBootApplication??
public?class?AssetApplication?{??
//如果沒有使用默認(rèn)值80??
@Value("${http.port:80}")??
????Integer?httpPort;??
//正常啟用的https端口?如443??
@Value("${server.port}")??
????Integer?httpsPort;??
//?springboot2?寫法??
@Bean?
public?TomcatServletWebServerFactory?servletContainer()?{??
TomcatServletWebServerFactory?tomcat?=new?TomcatServletWebServerFactory()?{??
@Override??
protected?void?postProcessContext(Context?context)?{??
SecurityConstraint?constraint?=new?SecurityConstraint();??
constraint.setUserConstraint("CONFIDENTIAL");??
SecurityCollection?collection?=new?SecurityCollection();??
collection.addPattern("/*");??
????????????????constraint.addCollection(collection);??
????????????????context.addConstraint(constraint);??
????????????}??
????????};??
????????tomcat.addAdditionalTomcatConnectors(httpConnector());??
return?tomcat;??
????}??
@Bean?
public?Connector?httpConnector()?{??
System.out.println("啟用http轉(zhuǎn)https協(xié)議,http端口:"+this.httpPort+"途茫,https端口:"+this.httpsPort);??
Connector?connector?=new?Connector("org.apache.coyote.http11.Http11NioProtocol");??
connector.setScheme("http");??
//Connector監(jiān)聽的http的端口號??
????????connector.setPort(httpPort);??
connector.setSecure(false);??
//監(jiān)聽到http的端口號后轉(zhuǎn)向到的https的端口號??
????????connector.setRedirectPort(httpsPort);??
return?connector;??
????}}??
二袍患、配置文件?
1.使用http協(xié)議時的配置?
server.port=80? ??
2.使用https及http協(xié)議時的配置?
server.port=443?【默認(rèn)springboot的默認(rèn)啟動端口是8080慕蔚,現(xiàn)在的目標(biāo)是讓默認(rèn)端口為https端口,即443端口森书,那么就要配置設(shè)置成443,并且注意這個路徑 是 server.port 不是 server.tomcat.port】
server.ssl.key-store=classpath:keystore.p12?
server.ssl.key-store-password=your-password?
server.ssl.keyStoreType=PKCS12?
server.ssl.keyAlias=your-cert-alias?
condition.http2https=true?
http.port=80?
3.Spring-Security 啟用安全通道(https)的一步步實現(xiàn)
發(fā)現(xiàn)Spring Security默認(rèn)是內(nèi)置個兩組對應(yīng)的映射端口(80->443融痛,8080->8443)。到這里上面出錯就好理解了神僵,我測試用的tomcat雁刷,設(shè)置的http請求監(jiān)聽端口是8898,根本就找不到對應(yīng)的https端口挑豌。