跳到主要内容

Spring Session - HttpSession(快速开始)

QWen Max 中英对照 JDBC Spring Session - HttpSession (Quick Start)

本指南描述了如何使用Spring Session通过基于XML的配置透明地利用关系型数据库来支持Web应用程序的HttpSession

备注

你可以在httpsession-jdbc-xml 示例应用程序中找到完整的指南。

Index

更新依赖项

在使用 Spring Session 之前,您必须更新依赖项。如果您使用 Maven,则必须添加以下依赖项:

<dependencies>
<!-- ... -->

<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-jdbc</artifactId>
<version>3.4.2</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>6.2.3</version>
</dependency>
</dependencies>
xml

Spring XML 配置

添加所需的依赖项后,我们可以创建Spring配置。Spring配置负责创建一个servlet过滤器,该过滤器将HttpSession实现替换为由Spring Session支持的实现。以下清单展示了如何添加以下Spring配置:

// <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>
xml
  • 我们使用 <context:annotation-config/>JdbcHttpSessionConfiguration 的组合,因为 Spring Session 尚未提供 XML 命名空间支持(参见 gh-104)。这会创建一个名为 springSessionRepositoryFilter 的 Spring bean。该 bean 实现了 Filter。过滤器负责将 HttpSession 实现替换为由 Spring Session 支持的实现。在这种情况下,Spring Session 由关系型数据库支持。

  • 我们创建了一个 dataSource,它将 Spring Session 连接到 H2 数据库的嵌入实例。我们配置 H2 数据库以使用包含在 Spring Session 中的 SQL 脚本来创建数据库表。

  • 我们创建了一个 transactionManager,用于管理之前配置的 dataSource 的事务。

有关如何配置与数据访问相关的问题的更多信息,请参见 Spring 框架参考文档

XML Servlet 容器初始化

我们在Spring 配置中创建了一个名为 springSessionRepositoryFilter 的 Spring bean,它实现了 FilterspringSessionRepositoryFilter bean 负责将 HttpSession 替换为由 Spring Session 支持的自定义实现。

为了让我们的 Filter 发挥作用,我们需要指示 Spring 加载我们的 session.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>
xml

ContextLoaderListener 读取 contextConfigLocation 并加载我们的 session.xml 配置。

最后,我们需要确保我们的 Servlet 容器(即 Tomcat)对每个请求都使用我们的 springSessionRepositoryFilter。以下代码片段为我们完成了这最后一步:

<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>
xml

DelegatingFilterProxy 会查找一个名为 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 示例应用程序

现在你可以尝试使用该应用程序。为此,请用以下信息填写表单:

  • 属性名称: username

  • 属性值: rob

现在点击 设置属性 按钮。你应该能在表格中看到显示的值。

它是如何工作的?

我们在下面的 SessionServlet 中与标准的 HttpSession 进行交互:

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;

}
java

我们不使用 Tomcat 的 HttpSession,而是将值持久化在 H2 数据库中。Spring Session 会在你的浏览器中创建一个名为 SESSION 的 cookie。该 cookie 包含你的会话 ID。你可以查看 cookie(使用 ChromeFirefox)。

您可以使用位于 localhost:8080/h2-console/ 的 H2 Web 控制台来移除会话(使用 jdbc:h2:mem:testdb 作为 JDBC URL)

现在你可以访问 localhost:8080/,并观察我们添加的属性不再显示。