<address id="r9vd9"><address id="r9vd9"><listing id="r9vd9"></listing></address></address>

      歡迎您光臨深圳塔燈網絡科技有限公司!
      電話圖標 余先生:13699882642

      網站百科

      為您解碼網站建設的點點滴滴

      spring boot支持https請求

      發表日期:2017-11 文章編輯:小燈 瀏覽次數:2477

      spring boot給我們提供了很多便利之處,包括spring boot內置了tomcat,所以我們一般啟動都是spring boot內置的tomcat,用HTTP請求進行訪問,但是http請求并不安全,由于我們對項目應用的權限認證變得更加謹慎,需要用到https請求,自己新建了一個spring boot 項目進行測試,現在將怎么新建spring boot工程和使用https請求以及在此過程中遇到的問題記錄一下。

      一.新建spring boot 的web工程,我用的是intelij idea

      1.創建一個項目,創建時選擇Spring Initializr,然后Next

      image.png

      2.填寫項目信息

      image.png

      3.選擇web工程

      image.png

      注:我的intelij版本可能比較低,出現的是這樣的,但是有的可能會出現下圖的,都是一樣的

      image.png

      4.填寫工程名字點擊finish

      image.png

      ok,一個新的web工程就建好了,下載依賴的過程比較慢

      我們新建的是一個web工程,可以在項目的入口類加上@RestController注解,使之變為一個Controller,然后里邊提供一個地址轉換方法

      @RestController @SpringBootApplication public class TestApplication {public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } @RequestMapping(value = "/",produces = "text/plain;charset=UTF-8") String index(){ return "Hello Spring Boot!"; } } 

      啟動main方法,訪問http://localhost:8080/(查看啟動日志時可以發現默認的端口號是8080)可以看到頁面顯示

      image.png

      二.生成ssl證書

      我是自己生成的ssl證書,當然這個證書是不被客戶端認可的
      1.打開cmd,輸入生成證書的命令

      keytool -genkey -alias tomcat-storetype PKCS12 -keyalg RSA -keysize 2048-keystore keystore.p12 -validity 3650 

      關于這幾個參數的解釋如下:

      1.-storetype 指定密鑰倉庫類型
      2.-keyalg 生證書的算法名稱,RSA是一種非對稱加密算法
      3.-keysize 證書大小
      4.-keystore 生成的證書文件的存儲路徑
      5.-validity 證書的有效期

      2.輸入密鑰庫口令

      image.png

      3.依次填寫證書相關的信息

      image.png

      完成之后,系統的當前用戶目錄下會生成一個keystore.p12文件,當然你在生成證書的時候可以改變證書的名稱,那么相應的系統用戶目錄下就會生成相應的文件,將keystore.p12文件拷貝到我們項目的根路徑下,項目的結構如下:

      image.png

      4.修改application.properties文件,添加HTTPS支持

      server.ssl.key-store=keystore.p12 server.ssl.key-store-password=123456 server.ssl.keyStoreType=PKCS12 server.ssl.keyAlias:tomcat 

      注:這里的key-store-password就是我們生成ssl證書時設置的密鑰庫的口令

      此時,我們的工程就支持了https請求,我們就可以啟動項目的main方法,查看啟動時的日志會發現默認的端口依舊是8080,因為我們并沒有額外設置https的端口號,訪問https://localhost:8080/

      image.png

      注:因為我用的是自己生成的ssl證書,所以https請求是不被客戶端識別的,我用的是火狐瀏覽器,在警告頁點擊 高級按鈕,點擊添加例外,點擊確認安全例外就可以正??吹巾撁骘@示的內容,所以當你用瀏覽器進行https請求時未正常顯示并不是我們的項目有問題,修改瀏覽器的安全認證配置就行了

      三.http請求轉換成https請求

      我們已經習慣用http請求訪問,此時我們的項目支持的是https請求,為了方便我們可以將訪問時的http請求轉換成https請求
      在main方法中添加注解bean,代碼如下:

      @RestController @SpringBootApplication public class TestApplication {public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } @RequestMapping(value = "/",produces = "text/plain;charset=UTF-8") String index(){ return "Hello Spring Boot!"; } @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; }@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(8081); return connector; }} 

      注:我們知道spring boot內置的tomcat啟動時的默認的端口號時8080,我們現在需要將http請求轉換成https請求,所以一定要區分端口號,我們在代碼中將http的端口號設置為8080,在application.properties添加https的端口號

      server.ssl.key-store=keystore.p12 server.ssl.key-store-password=123456 server.ssl.keyStoreType=PKCS12 server.ssl.keyAlias:tomcat server.port=8081 

      啟動項目,我們訪問https://localhost:8081/,(同時還是需要設置瀏覽器的安全認證的)頁面可以正常顯示說明項目是支持https請求的

      訪問http://localhost:8080/,我們可以看到頁面的url變成了https://localhost:8081/,頁面也可以正常顯示,說明我們的需求解決了,可以將http請求轉換成https請求。

      四.同時支持http和https請求

      如果我們想要項目同時支持http和https請求,就是說我們用http請求時不再轉換成https請求,同時還支持https請求,很簡單,我們只需要將代碼中的connector.setSecure(false)中的false改成true就可以了,代碼如下:

      @RestController @SpringBootApplication public class TestApplication {public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } @RequestMapping(value = "/",produces = "text/plain;charset=UTF-8") String index(){ return "Hello Spring Boot!"; } @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; }@Bean public Connector httpConnector() { Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); //Connector監聽的http的端口號 connector.setPort(8080); connector.setSecure(true); //監聽到http的端口號后轉向到的https的端口號 connector.setRedirectPort(8081); return connector; }}

      啟動項目,我們訪問https://localhost:8081/,頁面可以正常顯示說明項目是支持https請求的

      訪問http://localhost:8080/,我們可以看到頁面的url并沒有轉換成https請求,頁面也可以正常顯示,說明我們的需求解決了,可以同時支持http和https請求。

      如有問題,歡迎指正!?。。?/p>
      本頁內容由塔燈網絡科技有限公司通過網絡收集編輯所得,所有資料僅供用戶學習參考,本站不擁有所有權,如您認為本網頁中由涉嫌抄襲的內容,請及時與我們聯系,并提供相關證據,工作人員會在5工作日內聯系您,一經查實,本站立刻刪除侵權內容。本文鏈接:http://www.webpost.com.cn/20378.html

      相關開發語言
       八年  行業經驗

      多一份參考,總有益處

      聯系深圳網站公司塔燈網絡,免費獲得網站建設方案及報價

      咨詢相關問題或預約面談,可以通過以下方式與我們聯系

      業務熱線:余經理:13699882642

      Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.    

      • QQ咨詢
      • 在線咨詢
      • 官方微信
      • 聯系電話
        座機0755-29185426
        手機13699882642
      • 預約上門
      • 返回頂部
      国产成人精品综合在线观看