栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot单体架构http请求转换https请求来支持微信小程序调用接口

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot单体架构http请求转换https请求来支持微信小程序调用接口

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);
  }
  

  
  @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("
  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 端口

最后附上一个小编犯的错

把代码打包到服务器了却总是不能访问,后来才发现是忘记配置服务器端口号的白名单,只需放通那个端口号就行了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/136289.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号