1.申请一个免费的阿里云证书

spring boot 配置 https

2.在阿里云后台下载证书

spring boot 配置 https

3.把下载的压缩文件里面的*.pfx(其他发证机构有可能不是这个结构,请自行转化一下,在转化的时候要求输入密码,一定要记住这个密码) 拷贝到resources根目录

openssl pkcs12 -export -out *.pfx -inkey *.key -in **

4.在application.properties中配置,密码也是在压缩文件里面自己去找出来

server.port=443
server.ssl.key-store=classpath:*.pfx
server.ssl.key-store-password=*
server.ssl.keyStoreType=PKCS12

5.http转跳到https,在SpringBootApplication里面添加代码

@Bean
public EmbeddedServletContainerFactory servletContainer() {
   TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
      @Override
      protected void postProcessContext(Context context) {

         SecurityConstraint securityConstraint = new SecurityConstraint();
         securityConstraint.setUserConstraint("CONFIDENTIAL");
         SecurityCollection collection = new SecurityCollection();
         collection.addPattern("/*");
         securityConstraint.addCollection(collection);
         context.addConstraint(securityConstraint);
      }
   };
   tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());
   return tomcat;
}
private Connector initiateHttpConnector() {
   Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
   connector.setScheme("http");
   connector.setPort(80);
   connector.setSecure(false);
   connector.setRedirectPort(443);
   return connector;
}
6.重启服务器搞定



相关文章:

  • 2019-09-25
  • 2021-09-24
  • 2021-11-19
  • 2021-09-27
  • 2021-12-26
  • 2021-11-13
猜你喜欢
  • 2019-07-01
  • 2022-02-22
  • 2022-12-23
  • 2022-12-23
  • 2022-02-24
  • 2022-02-21
  • 2022-12-23
相关资源
相似解决方案