content: package com.ctg.test.config;
  
  import com.fasterxml.jackson.annotation.JsonAutoDetect;
  import com.fasterxml.jackson.annotation.PropertyAccessor;
  import com.fasterxml.jackson.databind.ObjectMapper;
  import org.springframework.cache.CacheManager;
  import org.springframework.cache.annotation.EnableCaching;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.data.redis.cache.RedisCacheManager;
  import org.springframework.data.redis.connection.RedisConnectionFactory;
  import org.springframework.data.redis.core.RedisTemplate;
  import org.springframework.data.redis.core.StringRedisTemplate;
  import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
  import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  import org.springframework.data.redis.serializer.StringRedisSerializer;
  import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
  import org.springframework.session.web.http.CookieSerializer;
  import org.springframework.session.web.http.DefaultCookieSerializer;
  
  import java.util.Arrays;
  
  /**
   * @Description:
   * 1)RedisTemplate默认的系列化类是JdkSerializationRedisSerializer，
   * 用JdkSerializationRedisSerializer序列化的话，被序列化的对象必须实现Serializable接口。
   * 在存储内容时，除了属性的内容外还存了其它内容在里面，总长度长
   * 2)GenericJackson2JsonRedisSerializer,在json中加入@class属性，类的全路径包名，方便反序列化
   * 3)跨域的 “域” 指的是 URL，包括协议、域名、端口;
   * 准确的说，cookie 不能跨域名，而不是 cookie 不能跨域
   * cookie区分域，而不区分端口，也就是说，同一个ip下的多个端口下的cookie是共享的
   * @Author: yanhonghai
   * @Date: 2018/9/13 16:03
   */
  @Configuration
  @EnableCaching//开启缓存注解
  //maxInactiveIntervalInSeconds:session的统一过期时间,默认是1800秒过期，这里测试修改为60秒
  @EnableRedisHttpSession(maxInactiveIntervalInSeconds = 60)//注解，开启redis集中session管理
  public class RedisConfig {
      // 以下redisTemplate自由根据场景选择
      //默认的String-String
      //    @Bean
      public RedisTemplate RedisTemplate(RedisConnectionFactory factory) {
          StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
          stringRedisTemplate.setConnectionFactory(factory);
          return stringRedisTemplate;
      }
  
      @Bean
      public RedisTemplate<String, Object> RedisTemplate2(RedisConnectionFactory factory) {
          RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
          template.setConnectionFactory(factory);
          template.setKeySerializer(new StringRedisSerializer());
          //GenericJackson2JsonRedisSerializer方便反序列化，redis中也方便查看json
          template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
          template.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());
          template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
          template.afterPropertiesSet();
          return template;
      }
      @Bean
      public RedisTemplate<Object, Object> RedisTemplate3(RedisConnectionFactory factory) {
          RedisTemplate<Object, Object> template = new RedisTemplate<>();
          template.setConnectionFactory(factory);
          //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值（默认使用JDK的序列化方式）
          Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
  
          ObjectMapper mapper = new ObjectMapper();
          mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
          mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
          serializer.setObjectMapper(mapper);
          //使用StringRedisSerializer来序列化和反序列化redis的key值
          template.setKeySerializer(new StringRedisSerializer());
          //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值（默认使用JDK的序列化方式）
          template.setValueSerializer(serializer);
          // 设置hash key 和value序列化模式
          template.setHashKeySerializer(new StringRedisSerializer());
          template.setHashValueSerializer(serializer);
          template.afterPropertiesSet();
          return template;
      }
      /**
       * redis作为缓存
       * @param redisTemplate
       * @return
       */
      @Bean
      public CacheManager cacheManager(RedisTemplate<String, Object>  redisTemplate) {
          RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
          // 多个缓存的名称,目前只定义了一个
          rcm.setCacheNames(Arrays.asList("user"));
          //设置缓存过期时间(秒)
          rcm.setDefaultExpiration(60);
          return rcm;
      }
  
      /**
       * 在springboot中使用spring-session的时候，
       * 在不同的域名下面需要配置cookie主域否则session共享不生效
       * @return
       */
      @Bean
      public CookieSerializer cookieSerializer() {
          DefaultCookieSerializer defaultCookieSerializer = new DefaultCookieSerializer();
          //cookie名字
          defaultCookieSerializer.setCookieName("sessionId");
          //不同子域时设置
          //defaultCookieSerializer.setDomainName("xxx.com");
          //各web应用返回的cookiePath一致
          defaultCookieSerializer.setCookiePath("/");
          return defaultCookieSerializer;
      }
  }
  