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

#(密鑰文件路徑,也可以配置絕對(duì)路徑) server.ssl.key-store= classpath:證書文件名.pfx #(密鑰生成時(shí)輸入的密鑰庫(kù)口令) 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端口(請(qǐng)求訪問時(shí)的端口)
connector.setSecure(false);
connector.setRedirectPort(8444); // application.properties中配置的https端口
return connector;
}
}
以上代碼直接拿走,接下來啟動(dòng)測(cè)試
可以訪問 http端口,也可訪問 https 端口

最后附上一個(gè)小編犯的錯(cuò)
把代碼打包到服務(wù)器了卻總是不能訪問,后來才發(fā)現(xiàn)是忘記配置服務(wù)器端口號(hào)的白名單,只需放通那個(gè)端口號(hào)就行了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot實(shí)現(xiàn)對(duì)Http接口進(jìn)行監(jiān)控的代碼
- springboot配置請(qǐng)求超時(shí)時(shí)間(Http會(huì)話和接口訪問)
- 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請(qǐng)求詳解
- springboot中RestTemplate配置HttpClient連接池詳解
- 基于springboot的RestTemplate、okhttp和HttpClient對(duì)比分析
- SpringBoot如何使用Template請(qǐng)求http接口
相關(guān)文章
SpringBoot Security整合JWT授權(quán)RestAPI的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot Security整合JWT授權(quán)RestAPI的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Java新特性之Nashorn_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java新特性之Nashorn的相關(guān)資料,需要的朋友可以參考下2017-06-06
Java web項(xiàng)目啟動(dòng)Tomcat報(bào)錯(cuò)解決方案
這篇文章主要介紹了Java web項(xiàng)目啟動(dòng)Tomcat報(bào)錯(cuò)解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Java實(shí)現(xiàn)SM3加密和驗(yàn)證的示例代碼
在商用密碼體系中,SM3主要用于數(shù)字簽名及驗(yàn)證、消息認(rèn)證碼生成及驗(yàn)證、隨機(jī)數(shù)生成等,其算法公開,本文給大家詳細(xì)介紹了使用Java實(shí)現(xiàn)SM3加密和驗(yàn)證,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2023-12-12
SpringCloudAlibaba微服務(wù)調(diào)用組件OpenFeign的方法
Feign是Netflix開發(fā)的聲明式、模板化的HTTP客戶端,其靈感來自Retrofit、JAXRS-2.0以及WebSocket,Feign可幫助我們更加便捷、優(yōu)雅地調(diào)用HTTP API,這篇文章主要介紹了SpringCloudAlibaba微服務(wù)調(diào)用組件OpenFeign,需要的朋友可以參考下2024-07-07
字節(jié)碼調(diào)教入口JVM?寄生插件javaagent
這篇文章主要介紹了字節(jié)碼調(diào)教入口JVM?寄生插件javaagent方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Java Web應(yīng)用程序?qū)崿F(xiàn)基礎(chǔ)的文件下載功能的實(shí)例講解
這里我們演示了Servelet驅(qū)動(dòng)Tomcat來進(jìn)行HTTP下載的方法,接下來就詳細(xì)來看Java Web應(yīng)用程序?qū)崿F(xiàn)基礎(chǔ)的文件下載功能的實(shí)例講解2016-05-05

