正如您提到的,您是 API 的新手,这里有一个详细的答案。
答案基于您使用的是 tomcat 服务器的假设。有 4 步方法可以让应用程序在 https 上运行,下面是红色的
- 获取 SSL 证书或生成自签名 SSL 证书
- 在应用程序中启用 HTTPS
- 将 HTTP 重定向到 HTTPS
- 将 SSL 证书分发给客户端。
如果您还没有 ssl 证书,请使用 keytool 自行生成。Keytool 是与 JDK 一起提供的证书管理实用程序,因此如果您安装了 JDK,那么您应该已经有了可用的 keytool。
让我们打开终端提示符并编写以下命令来创建 JKS 密钥库:
keytool -genkeypair -alias tomcat -keyalg RSA -keysize 2048 -keystore keystore.jks -validity 3650 -storepass 密码
要创建一个 PKCS12 密钥库,我们应该这样做,命令如下:
keytool -genkeypair -alias tomcat -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore keystore.p12 -validity 3650 -storepass 密码
让我们仔细看看我们刚刚运行的命令:
genkeypair: generates a key pair;
alias: the alias name for the item we are generating;
keyalg: the cryptographic algorithm to generate the key pair;
keysize: the size of the key. We have used 2048 bits, but 4096 would be a better choice for production;
storetype: the type of keystore;
keystore: the name of the keystore;
validity: validity number of days;
storepass: a password for the keystore.
运行上一条命令时,我们会被要求输入一些信息,但我们可以跳过所有信息(只需按 Return 跳过一个选项)。当被问及信息是否正确时,我们应该输入是。最后,我们按回车键将密钥库密码也用作密钥密码。
What is your first and last name?
[Unknown]: What is the name of your organizational unit?
[Unknown]: What is the name of your organization?
[Unknown]: What is the name of your City or Locality?
[Unknown]: What is the name of your State or Province?
[Unknown]: What is the two-letter country code for this unit?
[Unknown]: Is CN=localhost, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown correct?
[no]: yes
Enter key password for <tomcat>
(RETURN if same as keystore password):
验证 keystore 内容 要检查 keystore 遵循 JKS 格式的内容,我们可以再次使用 keytool:
keytool -list -v -keystore keystore.jks
要按照 PKCS12 格式测试密钥库的内容:
keytool -list -v -storetype pkcs12 -keystore keystore.p12
将 JKS 密钥库转换为 PKCS12
如果我们已经有 JKS 密钥库,我们可以选择将其迁移到 PKCS12;keytool 有一个方便的命令:
keytool -importkeystore -srckeystore keystore.jks -destkeystore keystore.p12 -deststoretype pkcs12
2.) 在您的项目中启用 https
如果你有一个 application.properties 文件
server.port=8443
server.ssl.key-store-type=PKCS12
server.ssl.key-store=classpath:keystore.p12
server.ssl.key-store-password=password
server.ssl.key-alias=tomcat
security.require-ssl=true
如果你有 application.yml 文件
server:
ssl:
key-store: classpath:keystore.p12
key-store-password: password
key-store-type: pkcs12
key-alias: tomcat
key-password: password
port: 8443
为了在应用中实现,我们需要扩展WebSecurityConfigurerAdapter
类,因为该security.require-ssl
属性已被弃用。
如果您使用的是旧版本,那么您可以跳过下面提到的代码。
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.requiresChannel()
.anyRequest()
.requiresSecure();
}
}
3.) 将 http 重定向到 https
现在我们已经在 Spring Boot 应用程序中启用了 HTTPS 并阻止了任何 HTTP 请求,我们希望将所有流量重定向到 HTTPS。
Spring 允许在application.properties (or application.yml)
. 由于我们已将它用于 HTTPS,因此我们必须以编程方式为我们的 Tomcat Web 服务器设置 HTTP 连接器。
@Configuration
public class ServerConfig {
@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(getHttpConnector());
return tomcat;
}
private Connector getHttpConnector() {
Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
connector.setScheme("http");
connector.setPort(8080);
connector.setSecure(false);
connector.setRedirectPort(8443);
return connector;
}
}
4.) 将 SSL 证书分发给客户端 当使用自签名 SSL 证书时,我们的浏览器不会信任我们的应用程序,并会警告用户它不安全。这对任何其他客户都是一样的。
通过向客户提供我们的证书,可以让客户信任我们的应用程序。
从密钥库中提取 SSL 证书 我们已将证书存储在密钥库中,因此我们需要提取它。同样,keytool 很好地支持了我们:
keytool -export -keystore keystore.jks -alias tomcat -file myCertificate.crt
让浏览器信任 SSL 证书 当使用行业标准 PKCS12 格式的密钥库时,我们应该能够直接使用它而无需提取证书。
我建议您查看有关如何将 PKCS12 文件导入特定客户端的官方指南。
如果在 localhost 上部署应用程序,我们可能需要在浏览器中执行进一步的步骤:启用不安全的localhost
.
在 Chrome 中,我们可以在搜索栏中输入以下 URL:chrome://flags/#allow-insecure-localhost
并激活相对选项。
在 JRE 密钥库中导入 SSL 证书 为了让 JRE 信任我们的证书,我们需要将它导入到 cacerts 中:JRE 信任库负责保存所有可信任的证书。
首先,我们需要知道 JDK home 的路径。如果我们使用 Eclipse 或 STS 作为我们的 IDE,找到它的快速方法是转到 Preferences > Java > Installed JREs。如果使用 IntelliJ IDEA,我们可以通过转到 Project Structure > SDKs 并查看 JDK home path 字段的值来访问此信息。
然后,在我们的终端提示符下,让我们插入以下命令(我们可能需要通过在它前面加上 sudo 来以管理员权限运行它):
keytool -importcert -file myCertificate.crt -alias tomcat -keystore $JDK_HOME/jre/lib/security/cacerts
你可以在这里参考github上的项目