文章目录
- 引言
- 详细配置步骤
-
- [第一步:生成 SSL 证书 (Keystore)](#第一步:生成 SSL 证书 (Keystore))
- [第二步:将证书文件放入 Spring Boot 项目](#第二步:将证书文件放入 Spring Boot 项目)
- [第三步:配置 `application.properties`](#第三步:配置
application.properties) - [第四步:(推荐) 将 HTTP 请求重定向到 HTTPS](#第四步:(推荐) 将 HTTP 请求重定向到 HTTPS)
- 使用方式

引言
本篇博客将详细介绍从生成自签名证书到在Spring Boot应用中完成HTTPS配置,并实现HTTP请求自动重定向的详细步骤。无需配置nginx即可完全https的配置
详细配置步骤
第一步:生成 SSL 证书 (Keystore)
Spring Boot 使用一个叫做 keystore 的文件来存放证书。我们可以使用 JDK 自带的 keytool 命令来生成一个。
- 打开你的命令行工具(Terminal 或 PowerShell)。
- 执行以下命令。请记得将
**yourpassword**替换成一个你自己的密码。
shell
# yourip:替换你自己部署的服务地址 172.16.114.23
# yourpassword:替换成一个你自己的密码
keytool -genkeypair -alias tomcat -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore keystore.p12 -validity 3650 -dname "CN=yourip, OU=IT, O=MyCompany, L=City, ST=State, C=CN" -storepass yourpassword -keypass yourpassword
命令参数解释:
-genkeypair: 生成一个密钥对。-alias tomcat: 给这个证书起一个别名,Spring Boot 默认会找tomcat。-keyalg RSA -keysize 2048: 使用 RSA 算法,密钥长度为 2048 位。-storetype PKCS12: 生成PKCS12格式的 keystore 文件(推荐,比旧的 JKS 更通用)。-keystore keystore.p12: 生成的文件名。-validity 3650: 证书有效期,这里设置为 3650 天(10年)。-dname "...": 证书的拥有者信息。最关键的是**CN=172.16.114.23**,它应该与你的服务器IP或域名匹配。-storepass yourpassword -keypass yourpassword: 设置访问 keystore 文件和私钥的密码,请务必记住。
示范:
shell
# 本地
keytool -genkeypair -alias tomcat -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore keystore.p12 -validity 3650 -dname "CN=127.0.0.1, OU=IT, O=MyCompany, L=City, ST=State, C=CN" -storepass dtaichat -keypass dtaichat
# 服务器
keytool -genkeypair -alias tomcat -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore keystore.p12 -validity 3650 -dname "CN=172.16.114.23, OU=IT, O=MyCompany, L=City, ST=State, C=CN" -storepass dtaichat -keypass dtaichat
执行后,你会在当前目录下得到一个名为 keystore.p12 的文件。
第二步:将证书文件放入 Spring Boot 项目
将上一步生成的 keystore.p12 文件复制到你的 Spring Boot 项目的 src/main/resources 目录下。这样它就会被打包到最终的 jar 文件中。
shell
my-spring-boot-app/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/example/...
│ │ └── resources/
│ │ ├── application.properties
│ │ ├── static/
│ │ └── keystore.p12 <-- 把文件放在这里
│ └── test/
└── pom.xml
第三步:配置 application.properties
打开你的 src/main/resources/application.properties 文件,添加以下配置:
yaml
# password修改为自己的密码
server:
# 启用 SSL
ssl:
enabled: true
# Keystore 文件路径 (classpath: 表示从 resources 目录加载)
key-store: classpath:keystore.p12
# Keystore 文件类型
key-store-type: PKCS12
# Keystore 文件的密码 (就是你第一步设置的密码)
key-store-password: yourpassword
# 证书的别名 (就是你第一步设置的别名)
key-alias: tomcat
# (可选) 将 HTTPS 端口设置为 8999
# 如果你希望保留 HTTP 访问,可以设置为 8443,并在下一步配置重定向
port: 8999
第四步:(推荐) 将 HTTP 请求重定向到 HTTPS
外部访问的端口 8999 -> 你自己服务的端口 6789
这个 Java 配置类与你的 YAML 或 Properties 文件无关,它仍然是实现 HTTP 自动跳转到 HTTPS 的最佳实践。
请在你的项目中创建这个 SslConfig.java 文件(如果还没有的话)。
java
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.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SslConfig {
@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> containerCustomizer() {
return factory -> {
// 添加一个 HTTP 连接器,监听 原始的服务 端口
Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
connector.setPort(8999);
connector.setScheme("http");
// 将此连接器设置为重定向连接器
connector.setSecure(false);
connector.setRedirectPort(6789); // 重定向到你的 HTTPS 端口
factory.addAdditionalTomcatConnectors(connector);
// 配置安全约束,强制所有请求都通过 HTTPS
factory.addContextCustomizers(context -> {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
});
};
}
}
使用方式
访问服务器地址(http请求的1793端口):http://172.16.114.23:1793/aiapps/christmas/index.html
即可重定向到https的1794端口:https://172.16.114.23:1794/aiapps/christmas/index.html
