SpringSecurity——集成SpringSession、Redis和JSON序列化
解决⽅案
官⽅⽂档
Maven
主要
<!--Spring Security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!--Spring Data Redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--Spring Session-->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<!--Spring Data Redis Session-->
<dependency>
<groupId>org.springframework.session</groupId>
spring到底是干啥的<artifactId>spring-session-data-redis</artifactId>
</dependency>
解决⽅案
集成Spring Session
Maven
<!--Spring Session-->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
配置
/**
* @author ShenTuZhiGang
* @version 1.0.0
* @date 2021-02-16 20:27
*/
@Configuration
@EnableSpringHttpSession
public class CustomSpringHttpSessionConfig {
@Bean
public MapSessionRepository sessionRepository() {
return new MapSessionRepository(new ConcurrentHashMap<>());    }
}
集成Spring Session Redis
Maven
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置
取消Spring Session配置
/**
* @author ShenTuZhiGang
* @version 1.0.0
* @date 2021-02-16 20:27
*/
//@Configuration
//@EnableSpringHttpSession
public class CustomSpringHttpSessionConfig {
@Bean
public MapSessionRepository sessionRepository() {
return new MapSessionRepository(new ConcurrentHashMap<>());    }
}
Redis Session配置
@Configuration
public class SecurityConfiguration<S extends Session> extends WebSecurityConfigurerAdapter {
@Autowired
private FindByIndexNameSessionRepository<S> sessionRepository;
@Override
protected void configure(HttpSecurity http) throws Exception {
/
/ @formatter:off
http
// other config
.sessionManagement((sessionManagement) -> sessionManagement
.maximumSessions(2)
.sessionRegistry(sessionRegistry())
);
// @formatter:on
}
@Bean
public SpringSessionBackedSessionRegistry<S> sessionRegistry() {
return new SpringSessionBackedSessionRegistry<>(this.sessionRepository);
}
}
Session Listener
/**
* @author ShenTuZhiGang
* @version 1.0.0
* @date 2021-02-25 10:45
*/
@Configuration
@EnableRedisHttpSession
public class CustomRedisHttpSessionConfig {
/**
* httpSession的会话监听,
*/
@Bean
public HttpSessionEventPublisher httpSessionEventPublisher() {
return new HttpSessionEventPublisher();
}
}
JSON序列化
Jackson2
Redis配置
*/
@Configuration
public class CustomRedisConfig {
// private ObjectMapper objectMapper = new ObjectMapper();
@Autowired
private ObjectMapper objectMapper; //需要另外配置,不是重点,⾃⾏配置
/
**
* @see org.springframework.security.jackson2.SecurityJackson2Modules
* @return Redis序列化器
*/
@Bean
public RedisSerializer<Object> redisSerializer(){
ObjectMapper om = py();
//om.Modules(getClass().getClassLoader()));
//om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);        om.registerModule(new CoreJackson2Module());
//om.registerModule(new CasJackson2Module());
return new GenericJackson2JsonRedisSerializer(om);
}
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setDefaultSerializer(redisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
Redis Session配置
*/
@Configuration
@EnableRedisHttpSession
public class CustomRedisHttpSessionConfig {
private final RedisSerializer<Object> redisSerializer;
public CustomRedisHttpSessionConfig(RedisSerializer<Object> redisSerializer) {
}
/**
* Spring Session Redis JSON序列化
* *注:bean的名称必须为springSessionDefaultRedisSerializer
*
* @see org.springframework.fig.annotation.web.http.RedisHttpSessionConfiguration      */
@Bean
public RedisSerializer<Object> springSessionDefaultRedisSerializer(){
return redisSerializer;
}
/**
* httpSession的会话监听,
*/
@Bean
public HttpSessionEventPublisher httpSessionEventPublisher() {
return new HttpSessionEventPublisher();
}
}
Fastjson
同理,参考:
常见问题
参考⽂章

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。