Spring Boot 简明教程
Spring Boot - Enabling HTTPS
默认情况下,Spring Boot 应用程序在应用程序启动时使用 HTTP 8080 端口。
By default, Spring Boot application uses HTTP 8080 port when the application starts up.

你需要执行以下步骤配置 Spring Boot 应用程序中的 HTTPS 和端口 443 −
You need to follow the steps given below to configure the HTTPS and the port 443 in Spring Boot application −
-
Obtain the SSL certificate – Create a self-signed certificate or get one from a Certificate Authority
-
Enable HTTPS and 443 port
Self-Signed Certificate
要创建自签名证书,Java 运行时环境自带证书管理实用程序 key tool。该实用程序用于创建自签名证书。在给定的代码中展示了这一点 −
To create a self-signed certificate, Java Run Time environment comes bundled with certificate management utility key tool. This utility tool is used to create a Self-Signed certificate. It is shown in the code given here −
keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650
Enter keystore password:
Re-enter new password:
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 = Unknown, OU=Unknown, O = Unknown, L = Unknown, ST = Unknown, C = Unknown correct?
[no]: yes
该代码将生成一个名为 keystore.p12 的 PKCS12 密钥库文件,证书别名为 tomcat。
This code will generate a PKCS12 keystore file named as keystore.p12 and the certificate alias name is tomcat.
Configure HTTPS
我们需要在 application.properties 文件中提供服务器端口 443、密钥库文件路径、密钥库密码、密钥库类型和密钥别名。观察给定的代码 −
We need to provide the server port as 443, key-store file path, key-store-password, key-store-type and key alias name into the application.properties file. Observe the code given here −
server.port: 443
server.ssl.key-store: keystore.p12
server.ssl.key-store-password: springboot
server.ssl.keyStoreType: PKCS12
server.ssl.keyAlias: tomcat
如果你使用 YAML 属性的话,可以使用以下代码在下面使用 application.yml −
You can use the following code if you are using YAML properties use below application.yml −
server:
port: 443
ssl:
key-store: keystore.p12
key-store-password: springboot
keyStoreType: PKCS12
keyAlias: tomcat
你可以创建一个可执行 JAR 文件,然后使用以下 Maven 或 Gradle 命令运行 Spring Boot 应用程序。
You can create an executable JAR file, and run the spring boot application by using the following Maven or Gradle commands.
对于 Maven,可以使用以下命令:
For Maven, you can use the following command −
mvn clean install
“BUILD SUCCESS”之后,您可以在目标目录中找到 JAR 文件。
After “BUILD SUCCESS”, you can find the JAR file under the target directory.
对于 Gradle,你可以使用以下命令
For Gradle, you can use the command
gradle clean build
“BUILD SUCCESSFUL”之后,您可以在 build/libs 目录中找到 JAR 文件。
After “BUILD SUCCESSFUL”, you can find the JAR file under the build/libs directory.
现在,通过使用以下命令运行 JAR 文件 −
Now, run the JAR file by using the following command −
java –jar <JARFILE>
现在,该应用程序已经使用 https(如下图所示)在 Tomcat 端口 443 上启动 −
Now, the application has started on the Tomcat port 443 with https as shown −
