Zul*_*yev 2 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>
Run Code Online (Sandbox Code Playgroud)
我在 dispatcher-servlet.xml 中添加了 bean:
<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"/>
Run Code Online (Sandbox Code Playgroud)
我在 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>
Run Code Online (Sandbox Code Playgroud)
这些都是我的配置。任何帮助将不胜感激
我解决了这个问题。首先,我使类可序列化。为此,请遵循这篇文章: 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>
Run Code Online (Sandbox Code Playgroud)
然后我对 web.xml 做了一些更改。为了存储会话,必须有 springSessionRepositoryFilter 和类 org.springframework.web.filter.DelegatingFilterProxy。但是我在 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>
Run Code Online (Sandbox Code Playgroud)
然后我将 /WEB-INF/redis-config.xml 值添加到 context-param 中,但它导致 log4j2 出现问题。这就是为什么我在顶部为 log4j2 编写了上下文参数。
<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>
Run Code Online (Sandbox Code Playgroud)
就这些。现在会话存储在 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>
Run Code Online (Sandbox Code Playgroud)
我忘了提到一些新版本的依赖项不能相互配合。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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1721 次 |
| 最近记录: |