1. https出現的背景:(1)都知道http傳輸協議是裸漏的,明文傳輸的,極易被黑客攔截,因此,(2)人們想出的使用加密,也就是 對稱加密 例如aes,不過這個由於因為對稱加密需要每個客戶端和服務器有獨立一套,當客戶端多的時候維護困難,因此 有了 非對稱加密 例如 RSA,RSA,這個是1977年 麻省理工學院三個程序員發明的,很厲害,目前還未被破解,扯遠了
RSA是一種公鑰密碼體制,現在使用得很廣泛。如果對RSA本身有興趣的,后面看我有沒有時間寫個RSA的具體介紹。
RSA密碼體制是一種公鑰密碼體制,公鑰公開,私鑰保密,它的加密解密算法是公開的。 由公鑰加密的內容可以並且只能由私鑰進行解密,並且由私鑰加密的內容可以並且只能由公鑰進行解密。也就是說,RSA的這一對公鑰、私鑰都可以用來加密和解密,並且一方加密的內容可以由並且只能由對方進行解密。貌似RSA是很安全,其實有個不足的地方,就是當服務器發送給客戶端的時候,被黑客攔截了,用公開的公鑰解密,是可以看到里面的內容的,(3)所以就有了 SSL,涉及SSL證書等等。。。內容太多,可以百度看看
2.可以自己生成 SSL證書,但是這個不被官方存檔認可的,有錢可以去阿里雲買一個,一年也就個把千塊哈哈
keytool -genkeypair -alias tomcat -keyalg RSA -keystore E:\tomcat.key
//其中-alias是證書的別名,RSA是加密算法,-keystore后是輸出證書的路徑所在
到此為止我們已經得到SSL證書了,這個是配置https的必要步驟,那么怎么使用進Spring boot 項目呢
(3).spring boot 配置ssl使用https
首先,將剛剛獲取的證書 放在項目根目錄
其次.在application.yml添加
server: port: 8443 tomcat: max-threads: 800 accept-count: 30000 min-spare-threads: 20 max-connections: 30000 servlet-path: /photography ssl: # 證書路徑 key-store: tomcat.key key-store-type: JKS key-alias: tomcat # 配置密碼,就是在生成證書的時候輸入的密碼 key-store-password: imfjj201314
最后,還要在啟動類 配置
/** * it's for set http url auto change to https */ @Bean public EmbeddedServletContainerFactory servletContainer(){ TomcatEmbeddedServletContainerFactory tomcat=new TomcatEmbeddedServletContainerFactory(){ @Override protected void postProcessContext(Context context) { SecurityConstraint securityConstraint=new SecurityConstraint(); securityConstraint.setUserConstraint("CONFIDENTIAL");//confidential SecurityCollection collection=new SecurityCollection(); collection.addPattern("/*"); securityConstraint.addCollection(collection); context.addConstraint(securityConstraint); } }; tomcat.addAdditionalTomcatConnectors(httpConnector()); return tomcat; } @Bean public Connector httpConnector(){ Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); connector.setPort(8080); connector.setSecure(false); connector.setRedirectPort(8443); return connector; }
訪問一下,看看效果
瀏覽器輸入 http://localhost:8080/photography/html/index.html 跳轉到 ->https://localhost:8443/photography/html/index.html
提示不安全,這是正常的,因為瀏覽器去SSL機構查詢,並沒有我這個證書信息,所以報錯,點擊下面 繼續前往localhost 就可以訪問了
參考了 https://blog.csdn.net/ai15134626825/article/details/78565948/ ,參考了 https://blog.csdn.net/ly131420/article/details/38400583 僅限學習,如果侵權請聯系刪除
鼓勵:覺得寫得有幫助就支付寶掃一下吧,對你沒有損失,也給我動力