您好,登錄后才能下訂單哦!
小編給大家分享一下Springboot如何實(shí)現(xiàn)單體架構(gòu)http請求轉(zhuǎn)換https請求來支持微信小程序調(diào)用接口,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
http請求轉(zhuǎn)換https請求
1、實(shí)例如下
application.properties配置文件
#(密鑰文件路徑,也可以配置絕對路徑) server.ssl.key-store= classpath:證書文件名.pfx #(密鑰生成時(shí)輸入的密鑰庫口令) server.ssl.key-store-password:123456 #(密鑰類型,與密鑰生成命令一致) server.ssl.key-store-type:PKCS12
證書一般最好放在resources目錄下
接下來配置啟動(dòng)類RUN.java的代碼
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.transaction.annotation.EnableTransactionManagement; 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.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; @SpringBootApplication @EnableTransactionManagement @EnableScheduling public class Run{ public static void main(String[] args) throws Exception { SpringApplication.run(Run.class, args); } /** * 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(initiateHttpConnector()); return tomcat; } /** * 讓我們的應(yīng)用支持HTTP是個(gè)好想法,但是需要重定向到HTTPS, * 但是不能同時(shí)在application.properties中同時(shí)配置兩個(gè)connector, * 所以要以編程的方式配置HTTP connector,然后重定向到HTTPS connector * @return Connector */ private Connector initiateHttpConnector() { Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); connector.setPort(8084); // http端口(請求訪問時(shí)的端口) connector.setSecure(false); connector.setRedirectPort(8444); // application.properties中配置的https端口 return connector; } }
以上代碼直接拿走,接下來啟動(dòng)測試
可以訪問 http端口,也可訪問 https 端口
最后附上一個(gè)小編犯的錯(cuò)
把代碼打包到服務(wù)器了卻總是不能訪問,后來才發(fā)現(xiàn)是忘記配置服務(wù)器端口號的白名單,只需放通那個(gè)端口號就行了。
以上是“Springboot如何實(shí)現(xiàn)單體架構(gòu)http請求轉(zhuǎn)換https請求來支持微信小程序調(diào)用接口”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。