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

所属分类: 软件编程 / java 阅读数: 39
收藏 0 赞 0 分享

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 端口

最后附上一个小编犯的错

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

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

更多精彩内容其他人还在看

Java Set简介_动力节点Java学院整理

Set最大的特性就是不允许在其中存放的元素是重复的。接下来通过本文给大家分享java set常用方法和原理分析,需要的的朋友参考下吧
收藏 0 赞 0 分享

Java Timezone类常见问题_动力节点Java学院整理

这篇文章主要介绍了Java Timezone类常见问题的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

javaWeb项目部署到阿里云服务器步骤详解

本篇文章主要介绍了javaWeb项目部署到阿里云服务器步骤详解,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解使用zxing库生成QR-Code二维码

这篇文章主要介绍了详解使用zxing库生成QR-Code二维码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

java实现对服务器的自动巡检邮件通知

这篇文章主要为大家详细介绍了java实现对服务器的自动巡检邮件通知,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java随机验证码生成实现实例代码

这篇文章主要介绍了java随机验证码生成实现实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

java读取txt文件代码片段

这篇文章主要为大家详细介绍了java读取txt文件的代码片段,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java连接mysql数据库的方法

这篇文章主要为大家详细介绍了java连接mysql数据库的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java 算法之快速排序实现代码

这篇文章主要介绍了java 算法之快速排序实现代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用

这篇文章主要介绍了详解Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多