配置证书
切换到 jdk 的 bin 目录下,输入命令:
keytool -genkey -alias michaelSpica -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore ebininfosoft-ssl-key.p12 -validity 3650
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vPhx35tb-1670403111426)(assets/image-20221206093840-cyhztdm.png)]](/https://i-blog.csdnimg.cn/blog_migrate/69018f18f42b92b0c26a1986e0b95298.png)
证书会下载到 jdk 的 bin 目录下:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YBukqnSC-1670403111427)(assets/image-20221206092947-skqocp6.png)]](/https://i-blog.csdnimg.cn/blog_migrate/9f0266890fae5841b467e99e9fa8a3d1.png)
放入 SpringBoot 的资源目录,配置 application.properties:
# 证书的路径,可用绝对路径,如果放到项目资源文件路径需要添加 classpath:
server.ssl.key-store=classpath:ebininfosoft-ssl-key.p12
# 证书的密码
server.ssl.key-store-password=w50029804
# 证书的类型
server.ssl.key-store-type=PKCS12
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5IVbpD5T-1670403111428)(assets/image-20221206101730-rd4vndi.png)]](/https://i-blog.csdnimg.cn/blog_migrate/70f042ab0cb858a7f904f381500e6458.png)
pom.xml 把这个文件加入编译,如果不加会报错 Could not load key store 'classpath:ebininfosoft-ssl-key.p12':
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BiWMVtxL-1670403111429)(assets/image-20221206101843-lmp4yjh.png)]···](/https://i-blog.csdnimg.cn/blog_migrate/e43fbb583d199e570cc34c0b16cef042.png)
刷新 Maven,clean 后重新 compile。
此时若用 http 访问,则会提示:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mx4sS5aD-1670403111429)(assets/image-20221206103237-8fs3xny.png)]](/https://i-blog.csdnimg.cn/blog_migrate/2a9f06b0ae80727efcfb0756e859d9a8.png)
http 重定向至 https
若要把对 http 端口访问的全部重定向到 https,则需配置转换器,例如将 9002 端口访问都定向到 9000:
package com.huawei.oss.telcloudsimulationuiserver.config;
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.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HttpConnectorConfig {
/**
* 获取Http连接器
* @return Connector
*/
public Connector getHttpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http"); // 使用http协议
connector.setSecure(false); // 非安全传输
connector.setPort(9002); // HTTP监听端口
connector.setRedirectPort(9000); // 重定向端口
return connector;
}
@Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory() {
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(getHttpConnector()); // 添加连接器
return tomcat;
}
}
同时启用 http 和 https
如果要在两个端口分别启用 http 和 https,则可做以下配置,例如将 9002 端口作为 http 访问端口:
package com.huawei.oss.telcloudsimulationuiserver.config;
import org.apache.catalina.connector.Connector;
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 HttpConnectorConfig {
private int httpPort = 9002;
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(createStandardConnector());
return tomcat;
}
private Connector createStandardConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setPort(httpPort);
return connector;
}
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jIl45uMz-1670403111430)(assets/image-20221206104121-tl7w242.png)]](/https://i-blog.csdnimg.cn/blog_migrate/79f9c07fa0d3c21a6c19c3f88d886ffa.png)
本文介绍如何在Spring Boot应用中配置SSL证书,并实现从HTTP到HTTPS的重定向。通过详细步骤说明证书生成及配置过程,同时提供代码示例展示如何配置HTTP重定向。



2731

被折叠的 条评论
为什么被折叠?



