傳輸層安全性協議(英語:Transport Layer Security,縮寫作 TLS),及其前身安全套接層(Secure Sockets Layer,縮寫作 SSL)是一種安全協議,目的是爲互聯網通信,提供安全及數據完整性保障。網景公司(Netscape)在1994年推出首版網頁瀏覽器,網景導航者時,推出HTTPS協議,以SSL進行加密,這是SSL的起源。IETF將SSL進行標準化,1999年公佈第一版TLS標準文件。隨後又公佈RFC 5246 (2008年8月)與 RFC 6176 (2011年3月)。在瀏覽器、電子郵件、即時通信、VoIP、網絡傳真等應用程序中,廣泛支持這個協議。主要的網站,如Google、Facebook等也以這個協議來創建安全連接,發送數據。目前已成爲互聯網上保密通信的工業標準。
SSL包含記錄層(Record Layer)和傳輸層,記錄層協議確定傳輸層數據的封裝格式。傳輸層安全協議使用X.509認證,之後利用非對稱加密演算來對通信方做身份認證,之後交換對稱密鑰作爲會談密鑰(Session key)。這個會談密鑰是用來將通信兩方交換的數據做加密,保證兩個應用間通信的保密性和可靠性,使客戶與服務器應用之間的通信不被攻擊者竊聽。
在配置TLS/SSL之前我們需要拿到相應簽名的證書,測試實例可以使用Java 下面的 Keytool 來生成證書:
打開控制檯輸入:
keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650
這裏的別名是 keystore.p12,密碼什麼的直接設置就好,然後回車
然後根據路徑找到生成好的證書,把證書複製到項目裏,我是放到了這裏
放好證書後,建立一個index.html放到resources/templates文件夾下,一會用於測試。
再配置properties
server.port=8888
server.tomcat.uri-encoding=utf-8
server.servlet.context-path=/demo
server.ssl.key-store=keystore.p12
server.ssl.key-store-password=123456
server.ssl.key-store-type=PKCS12
server.ssl.key-alias=tomcat
spring.thymeleaf.prefix=classpath:/templates/
配置好properties再加入下面的代碼
@Configuration
public class HttpsConfig {
/**
* spring boot 1.0
*/
/* @Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
@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;
}*/
/**
* spring boot 2.0
* @return
*/
@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() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
//Connector監聽的http的端口號
connector.setPort(8080);
connector.setSecure(false);
//監聽到http的端口號後轉向到的https的端口號
connector.setRedirectPort(8888);
return connector;
}
}
@Controller
@RequestMapping
public class ViewControlller {
@GetMapping("index")
public String index(){
return "index";
}
}
值得注意的是加入的springboot jar的版本不同代碼有一定的改變,我這裏用的是2.0的版本,還有就是要想跳轉到html頁面的時候一定注意的就是千萬不要在Controller中用@RestController而是要用Controller,如果用RestController的話就會直接把你的index解析顯示在頁面當中,就不會跳轉了,還有就是想要跳轉的話一定要加入下面的兩個jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
準備完畢後啓動項目,打印臺顯示
再輸入:
127.0.0.1:8080/demo/index就會自動跳轉