SpringBoot整合HTTPS的項目實踐
1_Https 的作用
保護用戶的隱私信息安全:
在 HTTP 網(wǎng)站數(shù)據(jù)以明文方式傳輸,客戶的隱私極容易被盜取和泄露,而部署 SSL 證書,數(shù)據(jù)以 HTTPS 加密傳輸,可以保護通信不被第三方監(jiān)聽和竊取,從而保護用戶隱私及安全。還可增加用戶信任感和提升企業(yè)形象。
保護網(wǎng)站的安全性和完整性:
HTTPS 是安全套接層超文本傳輸協(xié)議,可以保證網(wǎng)站的信息從用戶瀏覽器到服務器之間的傳輸是高強度加密傳輸?shù)模遣粫坏谌礁`取和篡改的,還能避免網(wǎng)站被劫持而插入廣告等,防止被釣魚網(wǎng)站攻擊,從而保護網(wǎng)站的安全性和完整性
防止被釣魚網(wǎng)站攻擊,有效識別網(wǎng)站真實身份:
http 網(wǎng)站會被瀏覽器標記 “不安全”,而安裝 https 證書會取消這種 “不安全” 的警告,能增加訪客信任度。同時對于安裝了 OV SSL 證書或 EV SSL 證書的網(wǎng)站,還能向用戶證明網(wǎng)站的真實身份,防止網(wǎng)站被釣魚網(wǎng)站仿冒。
2_獲取證書
需要在 SSL 證書頒發(fā)機構(gòu)處申請或購買 SSL 證書,并下載對應的 .pem
和 .key
文件。
由于只是進行本地測試,所以這里將使用JDK1.8 開發(fā)工具包 bin 目錄下的keytool.exe
工具生成ssl
密鑰:
keytool -genkeypair -alias myalias -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore keystore.p12 -validity 3650 -storepass your-password
- -genkeypair : 表示創(chuàng)建密鑰。
- -alias: 保存時的別名。
- -keyalg:加密算法選擇,這里使用RSA。
- -keysize:秘鑰大小,是有固定要求的。
- -storetype:秘鑰存儲庫的類型。
- -keystore:密鑰的存放位置。
- -validity:有效時間,單位是天。
- -storepass:密鑰庫口令。
可以使用代碼驗證秘鑰生成是否正確,如:
package org.example; import java.io.FileInputStream; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; public class KeystoreExample { public static void main(String[] args) { String keystorePath = "path/to/your/keystore.p12"; char[] keystorePassword = "your-keystore-password".toCharArray(); try (FileInputStream fis = new FileInputStream(keystorePath)) { KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); keystore.load(fis, keystorePassword); System.out.println("密鑰庫加載成功"); } catch (java.io.IOException e) { System.err.println("密鑰庫加載失敗: " + e.getMessage()); } catch (NoSuchAlgorithmException | CertificateException | KeyStoreException e) { System.err.println("可能是密碼錯誤導致的解密失敗"); e.printStackTrace(); } } }
或者使用keytool.exe
工具進行驗證:
keytool -list -v -keystore keystore.p12
3_配置項
application.properties
:
server.port=8081 server.error.path=/log server.servlet.session.timeout=30s # 設置應用程序的上下文路徑為 /test002。這意味著所有以 /test002 開始的 URL 都將被認為屬于這個應用程序。 server.servlet.context-path=/test002 server.tomcat.uri-encoding=UTF-8 server.tomcat.threads.max=500 # 表示秘鑰存儲庫的類型 server.ssl.key-store-type=PKCS12 # 表示 SSL 密鑰存儲庫的名稱為 keystore.p12 server.ssl.key-store=classpath:ssl/keystore.p12 # 表示 SSL 密鑰別名為 myalias 。 server.ssl.key-alias=myalias # 這行設置的是 SSL 密鑰存儲庫的密碼為 your-password # 密碼不能特別簡單,不然項目啟動時會出現(xiàn)異常 server.ssl.key-store-password=your-password
其中,server.port 配置 HTTPS 監(jiān)聽端口。server.ssl.key-store 配置證書存放路徑,server.ssl.key-store-password 配置證書密碼,server.ssl.key-store-type 和 server.ssl.key-alias 分別指定證書的類型和別名。
pom.xml
:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>spring_Back</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.9.RELEASE</version> <relativePath/> </parent> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
4_配置類
package org.example.config; import org.apache.catalina.Context; import org.apache.catalina.connector.Connector; import org.apache.tomcat.util.descriptor.web.SecurityCollection; import org.apache.tomcat.util.descriptor.web.SecurityConstraint; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class TomcatConfig { /** * 設置 Tomcat 的Server配置 * @return */ @Bean TomcatServletWebServerFactory tomcatServletWebServerFactory(){ TomcatServletWebServerFactory myFactory = new TomcatServletWebServerFactory(){ //創(chuàng)建一個安全約束對象 @Override protected void postProcessContext(Context context) { SecurityConstraint constraint = new SecurityConstraint(); constraint.setUserConstraint("CONFIDENTIAL");//設置為機密級別 SecurityCollection connection = new SecurityCollection();//設置一個安全連接對象 //作用到所有路由上 connection.addPattern("/*"); //加入 connection 對象到安全路由中去 constraint.addCollection(connection); context.addConstraint(constraint); } }; //配置HTTPS連接器 myFactory.addAdditionalTomcatConnectors(createConnector()); return myFactory; } /** * 創(chuàng)建一個連接兼容Https請求 * @return */ private Connector createConnector(){ //tomcat 9 中 //tomcat/conf/server.xml中不要使用org.apache.coyote.http11.Http11AprProtocol //要用HTTP/1.1 Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);//"HTTP/1.1" connector.setScheme("http"); connector.setPort(8080); connector.setSecure(false);//關(guān)閉ssl檢查 //設置跳轉(zhuǎn)到8081 的端口 connector.setRedirectPort(8081); return connector; } }
這段代碼創(chuàng)建了一個 Tomcat Servlet 容器,并配置了一個 HTTPS 連接器。其中 redirectConnector() 方法實現(xiàn)了將 HTTP 請求重定向到 HTTPS 的功能。
- 啟動項目:完成上述配置后,重新啟動項目,就能夠在 HTTPS 協(xié)議下訪問 Web 應用了。
需要注意的是,在實際生產(chǎn)環(huán)境中,還需要配置服務器的防火墻、負載均衡等相關(guān)組件,保證 HTTPS 的安全性和穩(wěn)定性。
5_控制類
package org.example.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class FirstController { @GetMapping("/hey") public String hey(){ return "hey main"; } }
6_啟動類
package org.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; @EnableAutoConfiguration @ComponentScan public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class,args); } }
此時使用 http 協(xié)議訪問 8080 端口將跳轉(zhuǎn)到 https 協(xié)議下的 8081端口。
到此這篇關(guān)于SpringBoot整合HTTPS的項目實踐的文章就介紹到這了,更多相關(guān)SpringBoot整合HTTPS內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot如何配置ssl支持https
- SpringBoot配置HTTPS及開發(fā)調(diào)試的操作方法
- springboot實現(xiàn)的https單向認證和雙向認證(java生成證書)
- SpringBoot配置Https訪問的詳細步驟
- SpringBoot配置Https入門實踐
- springboot項目開啟https協(xié)議的項目實現(xiàn)
- SpringBoot的HTTPS配置實現(xiàn)
- springboot配置http跳轉(zhuǎn)https的過程
- springboot如何將http轉(zhuǎn)https
- springboot支持https請求的實現(xiàn)
- SpringBoot中支持Https協(xié)議的實現(xiàn)
相關(guān)文章
詳解獲取Spring MVC中所有RequestMapping以及對應方法和參數(shù)
本篇文章主要介紹了詳解獲取Spring MVC中所有RequestMapping以及對應方法和參數(shù),具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03SpringBoot的@Value給靜態(tài)變量注入application.properties屬性值
這篇文章主要介紹了SpringBoot的@Value給靜態(tài)變量注入application.properties屬性值,Spring是一個開源的框架,主要是用來簡化開發(fā)流程,通過IOC,依賴注入(DI)和面向接口實現(xiàn)松耦合,需要的朋友可以參考下2023-05-05SpringMVC中Json數(shù)據(jù)交互處理示例詳解
這篇文章主要介紹了SpringMVC中Json數(shù)據(jù)交互處理的相關(guān)資料,分別講解了JSON的基本概念、構(gòu)成要素、數(shù)據(jù)類型、對象和數(shù)組的表示方法、字符串的轉(zhuǎn)義規(guī)則以及JSON與JavaScript的關(guān)系,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2025-03-03SpringCloud微服務網(wǎng)關(guān)限流方式
這篇文章主要介紹了SpringCloud微服務網(wǎng)關(guān)限流方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08