Spring Session - HttpSession (クイックスタート)

このガイドでは、Spring Session を使用して、リレーショナルを透過的に活用し、XML ベースの構成で Web アプリケーションの HttpSession をバックアップする方法について説明します。

依存関係の更新

Spring Session を使用する前に、依存関係を更新する必要があります。Maven を使用している場合は、次の依存関係を追加する必要があります。

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

	<dependency>
		<groupId>org.springframework.session</groupId>
		<artifactId>spring-session-jdbc</artifactId>
		<version>3.3.1</version>
		<type>pom</type>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-web</artifactId>
		<version>6.1.9</version>
	</dependency>
</dependencies>

Spring XML 設定

必要な依存関係を追加した後、Spring 構成を作成できます。Spring 構成は、HttpSession 実装を Spring Session による実装に置き換えるサーブレットフィルターの作成を担当します。次のリストは、次の Spring 構成を追加する方法を示しています。

src/main/webapp/WEB-INF/spring/session.xml
(1)
<context:annotation-config/>
<bean class="org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration"/>

(2)
<jdbc:embedded-database id="dataSource" database-name="testdb" type="H2">
	<jdbc:script location="classpath:org/springframework/session/jdbc/schema-h2.sql"/>
</jdbc:embedded-database>

(3)
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<constructor-arg ref="dataSource"/>
</bean>
1Spring Session はまだ XML 名前空間のサポートを提供していないため、<context:annotation-config/> と JdbcHttpSessionConfiguration の組み合わせを使用します(gh-104 [GitHub] (英語) を参照)。これにより、springSessionRepositoryFilter という名前の Spring Bean が作成されます。その Bean は Filter を実装しています。このフィルターは、Spring Session によってサポートされる HttpSession 実装の置き換えを担当します。この場合、Spring Session はリレーショナルデータベースに支えられています。
2Spring Session を H2 データベースの組み込みインスタンスに接続する dataSource を作成します。Spring Session に含まれている SQL スクリプトを使用して、データベーステーブルを作成するように H2 データベースを構成します。
3 以前に構成された dataSource のトランザクションを管理する transactionManager を作成します。

データアクセス関連の関心事を構成する方法の詳細については、Spring Framework リファレンスドキュメントを参照してください。

XML サーブレットコンテナーの初期化

Spring の設定は、Filter を実装する springSessionRepositoryFilter という名前の Spring Bean を作成しました。springSessionRepositoryFilter Bean は、HttpSession を Spring Session によるカスタム実装に置き換えるロールを果たします。

Filter がその魔法を実行するには、Spring に session.xml 構成をロードするように指示する必要があります。これは、次の構成で行います。

src/main/webapp/WEB-INF/web.xml
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>
		/WEB-INF/spring/session.xml
	</param-value>
</context-param>
<listener>
	<listener-class>
		org.springframework.web.context.ContextLoaderListener
	</listener-class>
</listener>

ContextLoaderListener は contextConfigLocation を読み取り、session.xml 構成を取得します。

最後に、サーブレットコンテナー(つまり、Tomcat)がすべてのリクエストに springSessionRepositoryFilter を使用するようにする必要があります。次のスニペットは、この最後のステップを実行します。

src/main/webapp/WEB-INF/web.xml
<filter>
	<filter-name>springSessionRepositoryFilter</filter-name>
	<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
	<filter-name>springSessionRepositoryFilter</filter-name>
	<url-pattern>/*</url-pattern>
	<dispatcher>REQUEST</dispatcher>
	<dispatcher>ERROR</dispatcher>
</filter-mapping>

DelegatingFilterProxy (Javadoc) は、springSessionRepositoryFilter という名前の Bean を検索し、それを Filter にキャストします。DelegatingFilterProxy が呼び出されるすべてのリクエストに対して、springSessionRepositoryFilter が呼び出されます。

httpsession-jdbc-xml サンプルアプリケーション

このセクションでは、httpsession-jdbc-xml サンプルアプリケーションの操作方法について説明します。

httpsession-jdbc-xml サンプルアプリケーションの実行

サンプルを実行するには、ソースコードを取得し、次のコマンドを呼び出します。

$ ./gradlew :spring-session-sample-xml-jdbc:tomcatRun

これで、localhost:8080/ でアプリケーションにアクセスできるようになります。

httpsession-jdbc-xml サンプルアプリケーションの探索

これで、アプリケーションを使用してみることができます。これを行うには、フォームに次の情報を入力します。

  • 属性名 : ユーザー名

  • 属性値 : rob

次に、属性を設定するボタンをクリックします。これで、テーブルに表示された値が表示されます。

それはどのように機能しますか?

次の SessionServlet で標準の HttpSession と対話します。

src/main/java/sample/SessionServlet.java
public class SessionServlet extends HttpServlet {

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String attributeName = req.getParameter("attributeName");
		String attributeValue = req.getParameter("attributeValue");
		req.getSession().setAttribute(attributeName, attributeValue);
		resp.sendRedirect(req.getContextPath() + "/");
	}

	private static final long serialVersionUID = 2878267318695777395L;

}

Tomcat の HttpSession を使用する代わりに、H2 データベースに値を保持します。Spring Session は、ブラウザーに SESSION という名前の Cookie を作成します。その Cookie には、セッションの ID が含まれています。Cookie を表示できます(Chrome (英語) または Firefox [Mozilla] (英語) を使用)。

localhost:8080/h2-console/ で入手可能な H2Web コンソールを使用して、セッションを削除できます。(JDBCURL に jdbc:h2:mem:testdb を使用する)

これで、localhost:8080/ のアプリケーションにアクセスして、追加した属性が表示されなくなったことを確認できます。