当你不知道怎么做的时候,做什么都不知道从何下手...
看了很多前人的文章,终于找到了配置方法!自己记录一份自己配置留下的,才是自己的,打一针预防针!
我这个是在阿里云申请的ssl证书,申请的时候是绑定了域名的,这里因为是Springboot的项目,内置了Tomcat 服务器,所以选择的是Tomcat的服务器类型的;由于之前没想起来是Tomcat的,一直在配置Nginx,配置成功了,访问到Nginx配置成功的页面,但访问不到项目的页面,最后才想起Springboot不是Nginx的配置,尴尬啊,丢 》》》
好了,上干货!
下载下来解压,得到:
将证书拷贝(复制)到 src\main\resources 的资源文件下面:
#端口号
server.port=443
#ssl
server.ssl.key-store=classpath:你的证书文件名
server.ssl.key-store-password=你的密码
server.ssl.key-store-type=PKCS12 #类型
http.port=80
配置 http 自动跳转到HTTPS,在启动文件里添加这两个:
// springboot2 写法
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory(){
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(httpConnector());
return tomcat;
}
@Bean
public Connector httpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(80); //Connector监听的http的端口号
connector.setSecure(false);
connector.setRedirectPort(443); //监听到http的端口号后转向到的https的端口号
return connector;
}
如果本地运行80端口可能会被占用,可以改为别的端口测试,比如 8888, 6666 等端口测试一下,打包之前改回来就可以了;
如果成功跳转到 https:localhost ,说明已经配置成功了:如下,因为在本地运行的,而且证书是绑定域名的,所以https报一个不安全很正常;
最后就是打包部署了,运行得到:
https配置的 Springboot2.0 的项目说明已经部署成功!
参考出处:https://blog.csdn.net/luckly_p/article/details/82351002?utm_source=blogxgwz3