SpringSecurity整合redis:session存储到redis
SpringSecurity本身依赖于单节点的实现,session存在于内存中。当在多个容器环境或多实例运行时,改造为redis统一存储,即单点登录1 pom<dependency><groupId>org.springframework.session</groupId><artif...
·
SpringSecurity本身依赖于单节点的实现,session存在于内存中。
当在多个容器环境或多实例运行时,改造为redis统一存储,即单点登录
1 pom
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
2 代码配置
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
/**
* redis实现session共享
*/
@Configuration
@EnableRedisHttpSession
public class SessionConfig {
}
配置即可,因为SpringSecurity已经自动实现将session存在redis
3 再记录一下redis配置
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* redis配置类
*/
@Slf4j
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 设置value的序列化规则和 key的序列化规则
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)