Spring Boot - 启用 HTTPS



默认情况下,Spring Boot 应用程序启动时使用 HTTP 8080 端口。

...
[2024-09-10T16:57:23Z] [org.apache.juli.logging.DirectJDKLog] [main] [173] [INFO ] Starting ProtocolHandler ["http-nio-8080"]
[2024-09-10T16:57:23Z] [org.springframework.boot.web.embedded.tomcat.TomcatWebServer] [main] [243] [INFO ] Tomcat started on port 8080 (http) with context path '/'
[2024-09-10T16:57:24Z] [org.springframework.boot.StartupInfoLogger] [main] [56] [INFO ] Started DemoApplication in 1.558 seconds (process running for 2.343)

您需要按照以下步骤在 Spring Boot 应用程序中配置 HTTPS 和 443 端口:

  • 获取 SSL 证书 – 创建自签名证书或从证书颁发机构获取证书

  • 启用 HTTPS 和 443 端口

自签名证书

要创建自签名证书,Java 运行时环境捆绑了证书管理实用程序 keytool。此实用程序用于创建自签名证书。代码如下所示:

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
   
Generating 2,048 bit RSA key pair and self-signed certificate (SHA384withRSA) with a validity of 3,650 days
        for: CN=Unknown, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown

此代码将生成名为 keystore.p12 的 PKCS12 密钥库文件,证书别名为 tomcat。我们将密钥库存储在 **E:/ > Dev** 目录中,密钥库密码为 springboot。

配置 HTTPS

我们需要将服务器端口 (443)、密钥库文件路径、密钥库密码、密钥库类型和密钥别名提供给 application.properties 文件。请观察以下代码:

server.port: 443
server.ssl.key-store: E:/Dev/keystore.p12
server.ssl.key-store-password: springboot
server.ssl.keyStoreType: PKCS12
server.ssl.keyAlias: tomcat

如果您使用 YAML 属性,可以使用以下 application.yml 代码:

server:
   port: 443
   ssl:
      key-store: E:/Dev/keystore.p12
      key-store-password: springboot
      keyStoreType: PKCS12
      keyAlias: tomcat

您可以创建一个可执行的 JAR 文件,并使用以下 Maven 或 Gradle 命令运行 Spring Boot 应用程序。

对于 Maven,您可以使用以下命令:

mvn clean install

“BUILD SUCCESS”之后,您可以在 target 目录下找到 JAR 文件。

对于 Gradle,您可以使用以下命令:

gradle clean build

“BUILD SUCCESSFUL”之后,您可以在 build/libs 目录下找到 JAR 文件。

现在,使用以下命令运行 JAR 文件:

java –jar <JARFILE>

现在,应用程序已在使用 https 的 Tomcat 443 端口上启动,如下所示:

[2024-09-10T17:01:59Z] [org.apache.juli.logging.DirectJDKLog] [main] [173] [INFO ] Starting ProtocolHandler ["https-jsse-nio-443"]
[2024-09-10T17:01:59Z] [org.apache.juli.logging.DirectJDKLog] [main] [173] [INFO ] Connector [https-jsse-nio-443], TLS virtual host [_default_], certificate type [UNDEFINED] configured from keystore [C:\Users\Tutorialspoint\.keystore] using alias [tomcat] with trust store [null]
[2024-09-10T17:01:59Z] [org.springframework.boot.web.embedded.tomcat.TomcatWebServer] [main] [243] [INFO ] Tomcat started on port 443 (https) with context path '/'
[2024-09-10T17:01:59Z] [org.springframework.boot.StartupInfoLogger] [main] [56] [INFO ] Started DemoApplication in 1.789 seconds (process running for 2.557)
广告