如何配置Spring会话以使用xml中的Redis?

时间:2018-10-17 06:51:35

标签: java spring spring-mvc redis spring-session

在我们的项目中,我们使用xml配置。我的任务是将会话存储在Redis中。我在其他站点中寻找解决方案,但是找不到合适的解决方案。您能给我有关解决问题的方法或说我做错了什么吗? 这是我为redis添加的依赖项:

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session</artifactId>
            <version>1.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
            <version>5.1.1.RELEASE</version>
        </dependency>

我将bean添加到了dispatcher-servlet.xml:

<bean
class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>

<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory" p:host-name="localhost"
          p:port="6379"/>

我对web.xml的更改:

<servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

这些都是我的配置。任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

我解决了这个问题。首先,我使类可序列化。为此,请遵循以下文章: How to make java class Serializable which is generated by wsdl

然后我在webapp / WEB-INF下创建了redis-config.xml:

<?xml version='1.0' encoding='UTF-8' ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <context:annotation-config/>
    <bean
            class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>
    <bean id="jedisConnectionFactory"
          class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="127.0.0.1"
          p:port="6379" p:usePool="true" p:database="0"/>

</beans>

然后,我对web.xml进行了一些更改。为了存储会话,必须有带有类org.springframework.web.filter.DelegatingFilterProxy的springSessionRepositoryFilter。但是我在web.xml中有另一个过滤器名称类。为了使程序正常工作,应首先编写springSessionRepositoryFilter:

<filter>
        <filter-name>springSessionRepositoryFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
<filter-mapping>
        <filter-name>springSessionRepositoryFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>

然后我将/WEB-INF/redis-config.xml值添加到context-param,但是它导致log4j2出现问题。这就是为什么我在顶部为log4j2编写context-param的原因。

<context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring-security.xml
            /WEB-INF/applicationContext.xml
            /WEB-INF/redis-config.xml
        </param-value>
    </context-param>

仅此而已。现在会话存储在Redis中

编辑: 上面的代码仅适用于本地Redis。当我编写远程Redis服务器时,它会引发如下异常:无法将Redis配置为键空间通知。为了解决这个问题,我如下更改了redis-config.xml:

<?xml version='1.0' encoding='UTF-8' ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <context:annotation-config/>
    <bean
            class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" p:configureRedisAction-ref="configureRedisAction" />
    <util:constant id="configureRedisAction"
            static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/>
    <bean id="jedisConnectionFactory"
          class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="xxx.xxx.xx.xxx"
          p:port="6379" p:usePool="true" p:database="0" p:password="xxx"/>
</beans>

我忘了提到某些新版本的依赖项无法相互配合。 Redis的依赖关系应如下:

<dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.8.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session</artifactId>
            <version>1.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
            <version>1.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
            <version>5.1.1.RELEASE</version>
        </dependency>