比如:服务器 为 39.98.163.107

1. 需要得到这个服务器的 SSL 证书

1.1 需要一个域名(控制台--》产品与服务---》域名 --》域名列表)

http 转为 https

2.2 在域名列表中,随便选一个域名,点击 ‘解析’。

 

点击 “解析”后 ,出现的页面。

http 转为 https

点击 "添加记录" , 主机记录 随便填 , 记录值 填 你要把 http ---转为->https 的服务器/ip地址;

 

 

3.3 下面弄SSL证书

看你用什么服务器的, Tomcat 就下载 tomcat的SSL

http 转为 https

 

 

2. 把证书放入项目

http 转为 https

 

3. 在application.properties中添加ssl证书配置参数

server.port=443

server.ssl.key-store=server.keystore

server.ssl.key-alias=tomcat

server.ssl.enabled=true

server.ssl.key-store-password=123456

server.ssl.key-store-type=JKS

http 转为 https

 

4.http访问自动转https访问 (向spring容器中注入两个Bean,代码如下)

向spring容器中注入两个Bean,代码如下

 

 

    @Bean
    public Connector connector(){
        Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(80);
        connector.setSecure(false);
        connector.setRedirectPort(443);
        return connector;
    }

    @Bean
    public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector){
        TomcatServletWebServerFactory tomcat=new TomcatServletWebServerFactory(){
            @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(connector);
        return tomcat;
    }

 

 

5. 完成

SpringBoot配置HTTPS,并实现HTTP访问自动转HTTPS访问: https://www.jianshu.com/p/8d4aba3b972d

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-03
  • 2021-12-03
  • 2021-12-03
  • 2021-11-17
  • 2021-12-11
猜你喜欢
  • 2021-05-24
  • 2021-11-17
  • 2021-11-17
  • 2021-11-07
  • 2021-11-04
  • 2022-12-23
  • 2021-12-03
相关资源
相似解决方案