為您解碼網(wǎng)站建設(shè)的點(diǎn)點(diǎn)滴滴
發(fā)表日期:2017-11 文章編輯:小燈 瀏覽次數(shù):2630
spring boot給我們提供了很多便利之處,包括spring boot內(nèi)置了tomcat,所以我們一般啟動(dòng)都是spring boot內(nèi)置的tomcat,用HTTP請(qǐng)求進(jìn)行訪問(wèn),但是http請(qǐng)求并不安全,由于我們對(duì)項(xiàng)目應(yīng)用的權(quán)限認(rèn)證變得更加謹(jǐn)慎,需要用到https請(qǐng)求,自己新建了一個(gè)spring boot 項(xiàng)目進(jìn)行測(cè)試,現(xiàn)在將怎么新建spring boot工程和使用https請(qǐng)求以及在此過(guò)程中遇到的問(wèn)題記錄一下。
1.創(chuàng)建一個(gè)項(xiàng)目,創(chuàng)建時(shí)選擇Spring Initializr,然后Next
2.填寫(xiě)項(xiàng)目信息
3.選擇web工程
注:我的intelij版本可能比較低,出現(xiàn)的是這樣的,但是有的可能會(huì)出現(xiàn)下圖的,都是一樣的
4.填寫(xiě)工程名字點(diǎn)擊finish
ok,一個(gè)新的web工程就建好了,下載依賴(lài)的過(guò)程比較慢
我們新建的是一個(gè)web工程,可以在項(xiàng)目的入口類(lèi)加上@RestController注解,使之變?yōu)橐粋€(gè)Controller,然后里邊提供一個(gè)地址轉(zhuǎn)換方法
@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!"; } }
啟動(dòng)main方法,訪問(wèn)http://localhost:8080/(查看啟動(dòng)日志時(shí)可以發(fā)現(xiàn)默認(rèn)的端口號(hào)是8080)可以看到頁(yè)面顯示
我是自己生成的ssl證書(shū),當(dāng)然這個(gè)證書(shū)是不被客戶(hù)端認(rèn)可的
1.打開(kāi)cmd,輸入生成證書(shū)的命令
keytool -genkey -alias tomcat-storetype PKCS12 -keyalg RSA -keysize 2048-keystore keystore.p12 -validity 3650
關(guān)于這幾個(gè)參數(shù)的解釋如下:
1.-storetype 指定密鑰倉(cāng)庫(kù)類(lèi)型
2.-keyalg 生證書(shū)的算法名稱(chēng),RSA是一種非對(duì)稱(chēng)加密算法
3.-keysize 證書(shū)大小
4.-keystore 生成的證書(shū)文件的存儲(chǔ)路徑
5.-validity 證書(shū)的有效期
2.輸入密鑰庫(kù)口令
3.依次填寫(xiě)證書(shū)相關(guān)的信息
完成之后,系統(tǒng)的當(dāng)前用戶(hù)目錄下會(huì)生成一個(gè)keystore.p12文件,當(dāng)然你在生成證書(shū)的時(shí)候可以改變證書(shū)的名稱(chēng),那么相應(yīng)的系統(tǒng)用戶(hù)目錄下就會(huì)生成相應(yīng)的文件,將keystore.p12文件拷貝到我們項(xiàng)目的根路徑下,項(xiàng)目的結(jié)構(gòu)如下:
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證書(shū)時(shí)設(shè)置的密鑰庫(kù)的口令
此時(shí),我們的工程就支持了https請(qǐng)求,我們就可以啟動(dòng)項(xiàng)目的main方法,查看啟動(dòng)時(shí)的日志會(huì)發(fā)現(xiàn)默認(rèn)的端口依舊是8080,因?yàn)槲覀儾](méi)有額外設(shè)置https的端口號(hào),訪問(wèn)https://localhost:8080/
注:因?yàn)槲矣玫氖亲约荷傻膕sl證書(shū),所以https請(qǐng)求是不被客戶(hù)端識(shí)別的,我用的是火狐瀏覽器,在警告頁(yè)點(diǎn)擊 高級(jí)按鈕,點(diǎn)擊添加例外,點(diǎn)擊確認(rèn)安全例外就可以正??吹巾?yè)面顯示的內(nèi)容,所以當(dāng)你用瀏覽器進(jìn)行https請(qǐng)求時(shí)未正常顯示并不是我們的項(xiàng)目有問(wèn)題,修改瀏覽器的安全認(rèn)證配置就行了
我們已經(jīng)習(xí)慣用http請(qǐng)求訪問(wèn),此時(shí)我們的項(xiàng)目支持的是https請(qǐng)求,為了方便我們可以將訪問(wèn)時(shí)的http請(qǐng)求轉(zhuǎn)換成https請(qǐng)求
在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監(jiān)聽(tīng)的http的端口號(hào) connector.setPort(8080); connector.setSecure(false); //監(jiān)聽(tīng)到http的端口號(hào)后轉(zhuǎn)向到的https的端口號(hào) connector.setRedirectPort(8081); return connector; }}
注:我們知道spring boot內(nèi)置的tomcat啟動(dòng)時(shí)的默認(rèn)的端口號(hào)時(shí)8080,我們現(xiàn)在需要將http請(qǐng)求轉(zhuǎn)換成https請(qǐng)求,所以一定要區(qū)分端口號(hào),我們?cè)诖a中將http的端口號(hào)設(shè)置為8080,在application.properties添加https的端口號(hào)
server.ssl.key-store=keystore.p12 server.ssl.key-store-password=123456 server.ssl.keyStoreType=PKCS12 server.ssl.keyAlias:tomcat server.port=8081
啟動(dòng)項(xiàng)目,我們?cè)L問(wèn)https://localhost:8081/,(同時(shí)還是需要設(shè)置瀏覽器的安全認(rèn)證的)頁(yè)面可以正常顯示說(shuō)明項(xiàng)目是支持https請(qǐng)求的
訪問(wèn)http://localhost:8080/,我們可以看到頁(yè)面的url變成了https://localhost:8081/,頁(yè)面也可以正常顯示,說(shuō)明我們的需求解決了,可以將http請(qǐng)求轉(zhuǎn)換成https請(qǐng)求。
如果我們想要項(xiàng)目同時(shí)支持http和https請(qǐng)求,就是說(shuō)我們用http請(qǐng)求時(shí)不再轉(zhuǎn)換成https請(qǐng)求,同時(shí)還支持https請(qǐng)求,很簡(jiǎn)單,我們只需要將代碼中的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監(jiān)聽(tīng)的http的端口號(hào) connector.setPort(8080); connector.setSecure(true); //監(jiān)聽(tīng)到http的端口號(hào)后轉(zhuǎn)向到的https的端口號(hào) connector.setRedirectPort(8081); return connector; }}
啟動(dòng)項(xiàng)目,我們?cè)L問(wèn)https://localhost:8081/,頁(yè)面可以正常顯示說(shuō)明項(xiàng)目是支持https請(qǐng)求的
訪問(wèn)http://localhost:8080/,我們可以看到頁(yè)面的url并沒(méi)有轉(zhuǎn)換成https請(qǐng)求,頁(yè)面也可以正常顯示,說(shuō)明我們的需求解決了,可以同時(shí)支持http和https請(qǐng)求。
如有問(wèn)題,歡迎指正?。。。?/p>
本頁(yè)內(nèi)容由塔燈網(wǎng)絡(luò)科技有限公司通過(guò)網(wǎng)絡(luò)收集編輯所得,所有資料僅供用戶(hù)學(xué)習(xí)參考,本站不擁有所有權(quán),如您認(rèn)為本網(wǎng)頁(yè)中由涉嫌抄襲的內(nèi)容,請(qǐng)及時(shí)與我們聯(lián)系,并提供相關(guān)證據(jù),工作人員會(huì)在5工作日內(nèi)聯(lián)系您,一經(jīng)查實(shí),本站立刻刪除侵權(quán)內(nèi)容。本文鏈接:http://www.cokiv.cn/20378.html