Springboot单体架构http请求转换https请求来支持微信小程序调用接口
2020-03-11 17:51
1801 查看
http请求转换https请求
1、话不多说,直接上代码!
application.properties配置文件
#(密钥文件路径,也可以配置绝对路径) server.ssl.key-store= classpath:证书文件名.pfx #(密钥生成时输入的密钥库口令) 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; } /** * 让我们的应用支持HTTP是个好想法,但是需要重定向到HTTPS, * 但是不能同时在application.properties中同时配置两个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端口(请求访问时的端口) connector.setSecure(false); connector.setRedirectPort(8444); // application.properties中配置的https端口 return connector; } }
以上代码直接拿走,接下来启动测试
可以访问 http端口,也可访问 https 端口
最后附上一个小编犯的错
把代码打包到服务器了却总是不能访问,后来才发现是忘记配置服务器端口号的白名单,只需放通那个端口号就行了。
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
相关文章推荐
- spring-boot 接口请求之Date、LocalDate、LocalDateTime日期类型转换处理
- springboot部署到阿里云,配置https,springboot项目同时支持http和https请求,阿里云配置https
- springboot工程通过在yml文件中配置使其支持http和https请求
- 微信小程序官方文档请求接口调用报错
- spring boot支持https请求配置
- 微信小程序语音识别服务搭建全过程解析(https api开放,支持新接口mp3录音、老接口silk录音)
- 使用Spring Boot开发Web项目(二)之添加HTTPS支持
- Spring-Boot ☞ ShapeFile文件读写工具类+接口调用
- 详解微信小程序网络请求接口封装实例
- 微信小程序请求后台服务器并向后台传数据的http请求格式的编码
- 之十一-呕血制作-Lettuce IOT框架-移远BC35G+树莓派+华为OC+SpringBoot后台+微信小程序
- 原创:微信小程序调用【统一下单】、【支付】、【支付回调】api并处理请求
- 如何在Spring Boot中,使用Https提供服务,并将Http请求自动重定向到Https。
- 基于springboot+redis+bootstrap+mysql开发一套属于自己的分布式springcloud云权限架构(四)【构建链路调用监控系统】
- 微信小程序如何调用json数据接口并解析
- Spring Boot全局支持CORS(跨源请求)的配置方法
- 如何生成微信小程序正方形二维码(不需请求接口获取)
- 微信小程序---客服消息接口调用,拿来即用
- Spring Boot中扩展XML请求与响应的支持详解
- 【Android】支持https接口调用的简单证书校验