首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Springboot配置https,使用腾讯云免费证书

Springboot配置https,使用腾讯云免费证书

原创
作者头像
wayn
发布2021-12-05 13:54:33
发布2021-12-05 13:54:33
15.7K1
举报
文章被收录于专栏:wayn的程序开发wayn的程序开发

1. 申请腾讯云免费ssl证书

1.1 登陆腾讯云在我的证书列表页面点击申请免费证书

企业微信截图_20210419145434.png
企业微信截图_20210419145434.png

2.2 提交资料,必填证书绑定域名以及申请邮箱,绑定域名填写springboot项目部署的服务器域名

企业微信截图_20210419145537.png
企业微信截图_20210419145537.png

2.3 选择验证方式,默认即可

企业微信截图_20210419145805.png
企业微信截图_20210419145805.png

2.4 验证域名,一般2、3分钟就验证完毕了

企业微信截图_20210419145827.png
企业微信截图_20210419145827.png

2.5 验证完毕后在证书列表页面下载证书文件,选择tomcat目录下的jks文件即可

证书列表

企业微信截图_20210419151236.png
企业微信截图_20210419151236.png

证书压缩包文件

企业微信截图_20210419150107.png
企业微信截图_20210419150107.png

2. springboot配置ssl证书

1.1 将jks文件导入springboot项目resoures目录下

企业微信截图_20210419150306.png
企业微信截图_20210419150306.png

2.2 在application.yml文件中配置如下代码

代码语言:txt
复制
server:
  port: 443
  ssl: # ssl相关配置
    enabled: true
    key-store: classpath:mall.wayn.ltd.jks
    key-store-password: idFXdK.Rnm3CgZp
    key-store-type: JKS

http-port: 8080 # http重定向https配置

2.3 添加HttpsConfiguration文件,将 HTTP 请求重定向到HTTPS

代码语言:txt
复制
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.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HttpsConfiguration {

    @Value("${http-port}")
    private int port;

    @Value("${server.port}")
    private int sslPort;

    @Bean
    public ServletWebServerFactory servletContainer() {
        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(redirectConnector());
        return tomcat;
    }

    private Connector redirectConnector() {
        Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
        connector.setScheme("http");
        connector.setPort(port);
        connector.setSecure(false);
        connector.setRedirectPort(sslPort);
        return connector;
    }

}
  1. 访问浏览器http://localhost8080,会自动重定向到https://localhost

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 申请腾讯云免费ssl证书
  • 2. springboot配置ssl证书
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档