Springboot配置https,使用腾讯云免费证书
2023-04-18 12:28:22 时间
1. 申请腾讯云免费ssl证书
1.1 登陆腾讯云在我的证书列表页面点击申请免费证书
2.2 提交资料,必填证书绑定域名以及申请邮箱,绑定域名填写springboot项目部署的服务器域名
2.3 选择验证方式,默认即可
2.4 验证域名,一般2、3分钟就验证完毕了
2.5 验证完毕后在证书列表页面下载证书文件,选择tomcat目录下的jks文件即可
证书列表
证书压缩包文件
2. springboot配置ssl证书
1.1 将jks文件导入springboot项目resoures目录下
2.2 在application.yml
文件中配置如下代码
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
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;
}
}
- 访问浏览器http://localhost8080,会自动重定向到https://localhost
相关文章
- 【技术种草】cdn+轻量服务器+hugo=让博客“云原生”一下
- CLB运维&运营最佳实践 ---访问日志大洞察
- vnc方式登陆服务器
- 轻松学排序算法:眼睛直观感受几种常用排序算法
- 十二个经典的大数据项目
- 为什么使用 CDN 内容分发网络?
- 大数据——大数据默认端口号列表
- Weld 1.1.5.Final,JSR-299 的框架
- JavaFX 2012:彻底开源
- 提升as3程序性能的十大要点
- 通过凸面几何学进行独立于边际的在线多类学习
- 利用行动影响的规律性和部分已知的模型进行离线强化学习
- ModelLight:基于模型的交通信号控制的元强化学习
- 浅谈Visual Source Safe项目分支
- 基于先验知识的递归卡尔曼滤波的代理人联合状态和输入估计
- 结合网络结构和非线性恢复来提高声誉评估的性能
- 最佳实践丨云开发CloudBase多环境管理实践
- TimeVAE:用于生成多变量时间序列的变异自动编码器
- 具有线性阈值激活的神经网络:结构和算法
- 内网渗透之横向移动 -- 从域外向域内进行密码喷洒攻击