如何配置 Spring 会话以在 xml 中使用 Redis? [英] How to configure Spring sessions to work with Redis in xml?
问题描述
在我们的项目中,我们使用 xml 配置.我的任务是在 Redis 中存储会话.我在不同的网站上寻找解决方案,但找不到合适的解决方案.您能否给我解决问题的相关方法或说我做错了什么?这是我为 redis 添加的依赖项:
In our project we use xml configurations. My task is to store sessions in Redis. I looked for the solution in different sites but I couldn't find appropriate solving. Could you please give me the relevant way to solve problem or to say what I am doing wrongly? Here is dependencies which I added for 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>
我在 dispatcher-servlet.xml 中添加了 bean:
I added beans to 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 中的更改:
My changes in 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>
这些都是我的配置.任何帮助将不胜感激
These are all my configurations. Any help will be appreciated
推荐答案
我解决了这个问题.首先,我使类可序列化.为此,请遵循以下帖子:如何使生成的java类可序列化wsdl
I solved this issue. Firstly, I make classes serializable. For doing this, follow this post: How to make java class Serializable which is generated by wsdl
然后我在webapp/WEB-INF下创建了redis-config.xml:
Then I created redis-config.xml under webapp/WEB-INF:
<?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 做了一些改动.为了存储会话,必须有 springSessionRepositoryFilter 和类 org.springframework.web.filter.DelegatingFilterProxy.但是我在 web.xml 中使用了另一个过滤器名称的类.为了让程序正常工作,需要先编写 springSessionRepositoryFilter :
Then I did some changes to web.xml. For storing sessions there must be springSessionRepositoryFilter with the class org.springframework.web.filter.DelegatingFilterProxy. But I had this class in web.xml with another filter-name. In order for the program to work, springSessionRepositoryFilter should be written firstly:
<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 编写了上下文参数.
Then I added /WEB-INF/redis-config.xml value to context-param, but it caused problem for log4j2. That is why I wrote context-param for log4j2 in the top.
<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 中
That is all. Now sessions store in Redis
编辑:上面的代码仅适用于本地 Redis.当我编写远程 Redis 服务器时,它会抛出如下异常:无法将 Redis 配置为键空间通知.为了解决这个问题,我改变了我的 redis-config.xml 如下:
EDIT: The code above was working only with local Redis. When I wrote remote Redis server it throws exception like this: Unable to configure Redis to keyspace notifications. For solving this, I changed my redis-config.xml as follows:
<?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的依赖应该如下:
I forgot to mention that some new version of dependencies don't work with each other. The dependencies for redis should be as follows:
<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>
这篇关于如何配置 Spring 会话以在 xml 中使用 Redis?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!