知識庫 / Spring RSS 訂閱

TLS 配置與 Spring 框架

Security,Spring
HongKong
4
12:31 PM · Dec 06 ,2025

1. 概述

安全通信在現代應用程序中發揮着重要作用。客户端與服務器之間在純 HTTP 上進行通信是不安全的。對於生產級別的應用程序,我們應該通過在應用程序中啓用 TLS(傳輸層安全)協議來啓用 HTTPS。在本教程中,我們將討論如何在 Spring Boot 應用程序中啓用 TLS 技術。

2. TLS 協議

TLS 提供數據在客户端和服務器之間傳輸時的保護,是 HTTPS 協議的關鍵組成部分。安全套接層 (SSL) 和 TLS 經常被混用,但它們並非完全相同。事實上,TLS 是 SSL 的後繼協議。 TLS 可以實現單向或雙向通信。

2.1 單向 TLS

在單向 TLS 中,僅客户端驗證服務器,以確保它從受信任的服務器接收到數據。為了實現單向 TLS,服務器會將其公鑰證書與客户端共享。

2.2. 雙向 TLS

在雙向 TLS(或稱多重 TLS,mTLS)中,客户端和服務器互相驗證,以確保通信雙方都已得到信任。為了實現 mTLS,雙方會互相共享各自的公鑰證書。

3. 配置 Spring Boot 中的 TLS

This section describes how to configure TLS (Transport Layer Security) in a Spring Boot application. TLS provides encryption for communication between your application and clients, ensuring data confidentiality and integrity.

Prerequisites:

  • A valid TLS certificate (e.g., from Let's Encrypt or a commercial Certificate Authority).
  • A properly configured Java Development Kit (JDK) with support for TLS.

Configuration Steps:

  1. Add Dependencies: Ensure your pom.xml (for Maven) or build.gradle (for Gradle) includes the necessary TLS dependencies. Typically, this involves adding Spring Security's TLS support.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    
  2. Configure Security: Extend your WebSecurity configuration to enable TLS. This typically involves setting the ssl() method.

    @Configuration
    @EnableWebSecurity
    @SecurityConfiguration(
            value = { "/*" },
            objectMatchers = { "/**" },
            http = {
                @HttpSecurity.WebFilterBean(
                        value = "springSecurityFilterChain",
                        filterImplementation = CustomFilter.class
                )
            }
    )
    public class SecurityConfig extends WebSecurityConfiguration {
    }
    
  3. Specify Certificate Locations: Tell Spring Boot where to find your TLS certificate and private key. This is done using the server.ssl.keyStoreUrl and server.ssl.keyStorePassword properties in your application.properties or application.yml file.

    server.ssl.keyStoreUrl=file:./keystore.jks
    server.ssl.keyStorePassword=yourKeystorePassword
    
  4. Enable TLS: Enable TLS in your application.properties or application.yml file.

    server.ssl.enabled=true
    
  5. Test Your Configuration: After configuring TLS, test your application to ensure that it is correctly configured and that communication is encrypted. You can use tools like openssl to verify the TLS connection.

Important Considerations:

  • Certificate Chain: Ensure that your certificate chain is properly configured. This may involve including intermediate certificates in your keystore.
  • Cipher Suites: Carefully select your cipher suites to ensure strong encryption and compatibility with your clients.
  • Client Compatibility: Verify that your clients support the TLS versions and cipher suites you are using.

3.1. 生成密鑰對

為了啓用 TLS,我們需要生成公鑰/私鑰對。為此,我們使用 keytoolkeytool 命令隨默認 Java 發行版一起提供。 讓我們使用 keytool 生成密鑰對並將其存儲在 keystore.p12 文件中:

keytool -genkeypair -alias baeldung -keyalg RSA -keysize 4096 \
  -validity 3650 -dname "CN=localhost" -keypass changeit -keystore keystore.p12 \
  -storeType PKCS12 -storepass changeit

keystore 文件可以採用不同的格式。其中兩種最流行的格式是 Java KeyStore (JKS) 和 PKCS#12。JKS 專用於 Java,而 PKCS#12 是一種行業標準格式,屬於 公鑰加密標準 (PKCS) 家族。

3.2. 在 Spring 中配置 TLS

讓我們首先配置單向 TLS。我們通過在 application.properties文件中配置 TLS 相關屬性來實現:

# enable/disable https
server.ssl.enabled=true
# keystore format
server.ssl.key-store-type=PKCS12
# keystore location
server.ssl.key-store=classpath:keystore/keystore.p12
# keystore password
server.ssl.key-store-password=changeit

在配置 SSL 協議時,我們將使用 TLS 並告知服務器使用 TLS 1.2:

# SSL protocol to use
server.ssl.protocol=TLS
# Enabled SSL protocols
server.ssl.enabled-protocols=TLSv1.2

為了驗證一切運行正常,只需運行 Spring Boot 應用程序:

3.3. 配置 Spring 中的 mTLS

為了啓用 mTLS,我們使用 client-auth 屬性,並將其設置為 need

server.ssl.client-auth=need

當使用 need 值時,客户端認證是必需的且必須完成。這意味着客户端和服務器必須共享其公共證書。在 Spring Boot 應用程序中存儲客户端證書,我們使用 truststore 文件並在 application.properties 文件中進行配置:

#trust store location
server.ssl.trust-store=classpath:keystore/truststore.p12
#trust store password
server.ssl.trust-store-password=changeit

用於指定位置的 信任存儲庫 是包含機器用於 SSL 服務器身份驗證所信任的證書頒發機構的文件的。 信任存儲庫 的密碼是訪問 信任存儲庫 文件所需的密碼。

4. 配置 Tomcat 中的 TLS

默認情況下,在未啓用 TLS 功能的情況下,Tomcat 啓動時會使用 HTTP 協議。要啓用 Tomcat 中的 TLS,請配置 <em >server.xml</em > 文件:

<Connector
  protocol="org.apache.coyote.http11.Http11NioProtocol"
  port="8443" maxThreads="200"
  scheme="https" secure="true" SSLEnabled="true"
  keystoreFile="${user.home}/.keystore" keystorePass="changeit"
  clientAuth="false" sslProtocol="TLS" sslEnabledProtocols="TLSv1.2"/>

為了啓用mTLS,我們將設置 clientAuth=”true”

5. 調用 HTTPS API

為了調用 REST API,我們將使用 curl 工具:

curl -v http://localhost:8443/baeldung

由於我們未指定 https,將會輸出錯誤:

Bad Request
This combination of host and port requires TLS.

此問題通過使用 https 協議解決的:

curl -v https://localhost:8443/baeldung

然而,這又導致了另一個錯誤:

SSL certificate problem: self signed certificate

當使用自簽名證書時,會發生這種情況。要解決這個問題,我們需要在客户端請求中使用服務器證書。首先,我們將從服務器的 keystore 文件中複製服務器證書 baeldung.cer。然後,我們將使用服務器證書以及 –cacert 選項,在 curl 請求中使用它:

curl --cacert baeldung.cer https://localhost:8443/baeldung

6. 結論

為了確保客户端與服務器之間傳輸數據的安全性,TLS 可以單向或雙向實施。本文檔描述瞭如何在 Spring Boot 應用程序的 application.properties 文件和 Tomcat 配置文件的中配置 TLS 的方法。

user avatar
0 位用戶收藏了這個故事!
收藏

發佈 評論

Some HTML is okay.