Spring Session - Spring Boot

本指南說明當您使用 Spring Boot 時,如何使用 Spring Session 透明地利用關聯式資料庫來支援 Web 應用程式的 HttpSession

您可以在 httpsession-jdbc-boot 範例應用程式中找到已完成的指南。

更新相依性

在使用 Spring Session 之前,您必須更新您的相依性。我們假設您正在使用一個可運作的 Spring Boot Web 應用程式。如果您使用 Maven,您必須新增以下相依性

pom.xml
<dependencies>
	<!-- ... -->

	<dependency>
		<groupId>org.springframework.session</groupId>
		<artifactId>spring-session-jdbc</artifactId>
	</dependency>
</dependencies>

Spring Boot 為 Spring Session 模組提供相依性管理,因此您無需明確宣告相依性版本。

Spring Boot 組態

新增必要的相依性後,我們可以建立我們的 Spring Boot 組態。由於一流的自動組態支援,只需新增相依性,Spring Boot 就會為我們設定由關聯式資料庫支援的 Spring Session。

如果類別路徑上存在單一 Spring Session 模組,Spring Boot 會自動使用該儲存實作。如果您有多個實作,您必須選擇您希望用來儲存 session 的 StoreType,如上所示。

在底層,Spring Boot 套用相當於手動新增 @EnableJdbcHttpSession 註解的組態。這會建立一個名為 springSessionRepositoryFilter 的 Spring bean。該 bean 實作 Filter。此 filter 負責替換 HttpSession 實作,使其由 Spring Session 支援。

您可以透過使用 application.properties 進一步自訂。以下清單顯示如何執行此操作

src/main/resources/application.properties
server.servlet.session.timeout= # Session timeout. If a duration suffix is not specified, seconds are used.
spring.session.jdbc.initialize-schema=embedded # Database schema initialization mode.
spring.session.jdbc.schema=classpath:org/springframework/session/jdbc/schema-@@platform@@.sql # Path to the SQL file to use to initialize the database schema.
spring.session.jdbc.table-name=SPRING_SESSION # Name of the database table used to store sessions.

如需更多資訊,請參閱 Spring Boot 文件中關於 Spring Session 的部分。

設定 DataSource

Spring Boot 會自動建立一個 DataSource,將 Spring Session 連接到 H2 資料庫的嵌入式執行個體。在生產環境中,您需要更新您的組態以指向您的關聯式資料庫。例如,您可以將以下內容包含在您的 application.properties 中

src/main/resources/application.properties
spring.datasource.url= # JDBC URL of the database.
spring.datasource.username= # Login username of the database.
spring.datasource.password= # Login password of the database.

如需更多資訊,請參閱 Spring Boot 文件中關於 設定 DataSource 的部分。

Servlet 容器初始化

我們的 Spring Boot 組態 建立了一個名為 springSessionRepositoryFilter 的 Spring bean,它實作 FilterspringSessionRepositoryFilter bean 負責將 HttpSession 替換為由 Spring Session 支援的自訂實作。

為了讓我們的 Filter 發揮作用,Spring 需要載入我們的 Config 類別。最後,我們需要確保我們的 Servlet 容器(即 Tomcat)針對每個請求都使用我們的 springSessionRepositoryFilter。幸運的是,Spring Boot 為我們處理了這兩個步驟。

httpsession-jdbc-boot 範例應用程式

httpsession-jdbc-boot 範例應用程式示範了當您使用 Spring Boot 時,如何使用 Spring Session 透明地利用 H2 資料庫來支援 Web 應用程式的 HttpSession

執行 httpsession-jdbc-boot 範例應用程式

您可以透過取得 原始碼 並調用以下命令來執行範例

$ ./gradlew :spring-session-sample-boot-jdbc:bootRun

您現在應該可以透過 localhost:8080/ 存取該應用程式

探索安全性範例應用程式

您現在可以嘗試使用該應用程式。若要執行此操作,請輸入以下內容以登入

  • 使用者名稱 user

  • 密碼 password

現在點擊登入按鈕。您現在應該會看到一則訊息,指出您已使用先前輸入的使用者登入。使用者的資訊儲存在 H2 資料庫中,而不是 Tomcat 的 HttpSession 實作中。

它是如何運作的?

我們不是使用 Tomcat 的 HttpSession,而是將值持久化到 H2 資料庫中。Spring Session 使用由關聯式資料庫支援的實作來替換 HttpSession。當 Spring Security 的 SecurityContextPersistenceFilterSecurityContext 儲存到 HttpSession 時,它隨後會被持久化到 H2 資料庫中。

當建立新的 HttpSession 時,Spring Session 會在您的瀏覽器中建立一個名為 SESSION 的 cookie。該 cookie 包含您的 session ID。您可以使用 ChromeFirefox 查看 cookie。

您可以使用 H2 Web Console 移除 session,網址為:localhost:8080/h2-console/(JDBC URL 使用 jdbc:h2:mem:testdb)。

現在您可以造訪 localhost:8080/ 應用程式,並看到我們不再通過驗證。