1.pom.xml中引入依赖
org.springframework.session
spring-session-data-redis
org.springframework.session
spring-session
1.2.2.RELEASE
2.定义redis配置类并用@Configuration注入
RedisConfig
/**
* Created by HASEE on 2018/3/14.
*/
@Configuration
public class RedisConfig {
@Value("${spring.redis.password}")
public String password;
@Value("${spring.redis.host}")
public String host;
// *****************************上**********************************
@Bean
public CachingConfigurerSupport keyGenerator() {
return new MyCachingConfigurerSupport();
}
public class MyCachingConfigurerSupport extends CachingConfigurerSupport {
@Override
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}
}
// TODO: 2019/9/6 SpringCacheRoleHierarchy和SpringCacheModuleSecurity存储的缓存还是乱码
// //缓存管理器
// @SuppressWarnings("rawtypes")
// @Bean
// public CacheManager cacheManager(StringRedisTemplate redisTemplate) {
// RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
// //设置缓存过期时间
// rcm.setDefaultExpiration(1800);//秒
// return rcm;
// }
// *******************************下********************************
/**
* 存入对象tostring后的信息
* 修改默认序列化方式
*/
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate template = new RedisTemplate<>();
// template.setConnectionFactory(jedisConnectionFactory());
template.setConnectionFactory(connectionFactory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的key也采用String的序列化方式,需要配置一下StringSerializer,不然key会乱码 /XX/XX
template.setHashKeySerializer(stringRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
// /**
// * 使用SpringSession里的 @EnableRedisHttpSession(redisNamespace = "jscmp") 注解
// * 该注解开启了redis连接
// */
// @Bean
// JedisConnectionFactory jedisConnectionFactory() {
// JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
// jedisConnectionFactory.setHostName(host);
// jedisConnectionFactory.setPassword(password);
// return jedisConnectionFactory;
// }
/**
* 解决redis集群环境没有开启Keyspace notifications导致的
*
* Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer' defined in class path resource
*
* */
@Bean
public static ConfigureRedisAction configureRedisAction() {
return ConfigureRedisAction.NO_OP;
}
}
- 在
RedisConfig
配置文件中并没有连接redis,那么通过使用@EnableRedisHttpSession
注解来自动连接。进入@EnableRedisHttpSession
注解,看到该注解里有@Import(RedisHttpSessionConfiguration.class)
点击进入RedisHttpSessionConfiguration.class
类中,拉到最后有一个afterPropertiesSet()
方法,这个方法就是redis自动连接的过程。
RedisConfig类如下
/**
* Created by LOG on 2016/9/6.
* 开发环境中如果没有redis 请使用EnableSpringHttpSession, 且将配置文件中的redis配置删除, 以防无法启动
*/
//@EnableSpringHttpSession
@EnableRedisHttpSession(redisNamespace = "jscmp")
//@EnableJdbcHttpSession
public class HttpSessionConfig {
@Value("${session.cookieName}")
public String cookieName;
@Value("${session.headerName}")
public String headerName;
}
- redis自动连接,用户名密码等等是在application.yml中进行配置的
总结:
HttpSessionConfig类只是注入@EnableRedisHttpSession注解,那么@EnableRedisHttpSession注解不一定非要放在这里,也可以在RedisConfig类上使用该注解