Springboot單體架構(gòu)http請求轉(zhuǎn)換https請求來支持微信小程序調(diào)用接口
http請求轉(zhuǎn)換https請求
1、話不多說,直接上代碼!
application.properties配置文件

#(密鑰文件路徑,也可以配置絕對路徑) server.ssl.key-store= classpath:證書文件名.pfx #(密鑰生成時(shí)輸入的密鑰庫口令) server.ssl.key-store-password:123456 #(密鑰類型,與密鑰生成命令一致) server.ssl.key-store-type:PKCS12
證書一般最好放在resources目錄下
接下來配置啟動類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;
}
}
以上代碼直接拿走,接下來啟動測試
可以訪問 http端口,也可訪問 https 端口

最后附上一個(gè)小編犯的錯(cuò)
把代碼打包到服務(wù)器了卻總是不能訪問,后來才發(fā)現(xiàn)是忘記配置服務(wù)器端口號的白名單,只需放通那個(gè)端口號就行了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot實(shí)現(xiàn)對Http接口進(jìn)行監(jiān)控的代碼
- springboot配置請求超時(shí)時(shí)間(Http會話和接口訪問)
- springboot 實(shí)現(xiàn)Http接口加簽、驗(yàn)簽操作方法
- SpringBoot 項(xiàng)目使用hutool 工具進(jìn)行 http 接口調(diào)用的處理方法
- SpringBoot 接口開發(fā)教程(httpclient客戶端)
- SpringBoot使用RestTemplate實(shí)現(xiàn)HTTP請求詳解
- springboot中RestTemplate配置HttpClient連接池詳解
- 基于springboot的RestTemplate、okhttp和HttpClient對比分析
- SpringBoot如何使用Template請求http接口
相關(guān)文章
SpringBoot2.x 整合Spring-Session實(shí)現(xiàn)Session共享功能
這篇文章主要介紹了SpringBoot2.x 整合Spring-Session實(shí)現(xiàn)Session共享功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07
Spring-cloud-eureka使用feign調(diào)用服務(wù)接口
這篇文章主要為大家詳細(xì)介紹了Spring-cloud-eureka使用feign調(diào)用服務(wù)接口,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
解決idea不支持SpringBoot yml文件的圖文教程
這篇文章主要介紹了解決idea不支持SpringBoot yml文件,需要的朋友可以參考下2018-06-06
java實(shí)現(xiàn)音頻轉(zhuǎn)文本的實(shí)現(xiàn)步驟
本文主要介紹了java實(shí)現(xiàn)音頻轉(zhuǎn)文本的實(shí)現(xiàn)步驟,可以通過使用一些現(xiàn)成的庫或者API來實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05
使用java數(shù)組 封裝自己的數(shù)組操作示例
這篇文章主要介紹了使用java數(shù)組 封裝自己的數(shù)組操作,結(jié)合實(shí)例形式分析了java數(shù)組索引、遍歷等相關(guān)封裝操作技巧與注意事項(xiàng),需要的朋友可以參考下2020-03-03
MybatisPlus字段類型轉(zhuǎn)換的實(shí)現(xiàn)示例
本文主要介紹了MybatisPlus如何完成字段類型轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
關(guān)于Springboot數(shù)據(jù)庫配置文件明文密碼加密解密的問題
這篇文章主要介紹了Springboot數(shù)據(jù)庫配置文件明文密碼加密解密的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
Java軟件生產(chǎn)監(jiān)控工具Btrace使用方法詳解
這篇文章主要介紹了Java軟件生產(chǎn)監(jiān)控工具Btrace使用方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07

