我试图通过添加类似项的依赖关系在Spring Boot应用程序中使用Redis进行会话:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>1.5.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.3.3.RELEASE</version>
</dependency>
Redis的代码如下:
package com.dci.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import
org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer;
@Configuration
@ComponentScan("com.dci")
@EnableRedisHttpSession
public class RedisConfig extends AbstractHttpSessionApplicationInitializer {
@Bean
JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory();
return factory;
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
final RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(jedisConnectionFactory());
template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
return template;
}
}
答案 0 :(得分:0)
Spring引导不需要JedisConnectionFactory和RedisTemplate Bean,但是在文件“ Spring Security Config”中添加:
@Bean
public HttpSessionStrategy httpSessionStrategy() {
return new HeaderHttpSessionStrategy();
}
在pom中添加以下依赖项:
<! - Spring Session ->
<Dependency>
<GroupId> org.springframework.session </ groupId>
<ArtifactId> spring-session </ artifactId>
<Version> 1.3.5.RELEASE </ version>
</ Dependency>
<! - redis server ->
<Dependency>
<GroupId> org.springframework.data </ groupId>
<ArtifactId> spring-data-redis </ artifactId>
</ Dependency>
<Dependency>
<GroupId> redis.clients </ groupId>
<ArtifactId> Jedis </ artifactId>
</ Dependency>
在您的配置文件(例如application.properties)中添加: spring.session.store-type = none
Spring Boot会做其他所有事情。