SpringBoot通过Redis共享Session

原创 Laughing  2019-07-22 11:12  阅读 61 次 评论 0 条
温馨提示

以下内容基于SpringBoot 1.2.7版本

  1. <parent>  
  2.        <groupId>org.springframework.boot</groupId>  
  3.        <artifactId>spring-boot-starter-parent</artifactId>  
  4.        <version>1.2.7.RELEASE</version>  
  5.    </parent>  

添加依赖

主要是添加如下两个依赖

  1. <!-- 添加redis-->  
  2.         <dependency>  
  3.             <groupId>org.springframework.boot</groupId>  
  4.             <artifactId>spring-boot-starter-redis</artifactId>  
  5.             <version>1.3.8.RELEASE</version>  
  6.         </dependency>  
  7. <!--        session依赖-->  
  8.         <dependency>  
  9.             <groupId>org.springframework.session</groupId>  
  10.             <artifactId>spring-session-data-redis</artifactId>  
  11.             <version>1.3.5.RELEASE</version>  
  12.         </dependency>  

安装Redis

具体操作方法请自行百度,不是本文重点

配置Redis

找到application.yml文件,添加如下内容

  1. #redis配置  
  2.   redis:  
  3.     host: 我是IP  
  4.     port: 6379  
  5.     password: 我是密码  
  6.     timeout: 0  
  7.     pool:  
  8.       max-active: 100  
  9.       max-idle: 10  
  10.       max-wait: 100000  
  11.     database: 0  

增加Redis配置类RedisConfiguration.java

  1. package Net.XiangCaoWuYu.Configurations;  
  2.   
  3. import com.fasterxml.jackson.annotation.JsonAutoDetect;  
  4. import com.fasterxml.jackson.annotation.PropertyAccessor;  
  5. import com.fasterxml.jackson.databind.ObjectMapper;  
  6. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;  
  7. import org.springframework.boot.context.properties.ConfigurationProperties;  
  8. import org.springframework.context.annotation.Bean;  
  9. import org.springframework.context.annotation.Configuration;  
  10. import org.springframework.data.redis.connection.RedisConnectionFactory;  
  11. import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;  
  12. import org.springframework.data.redis.core.RedisTemplate;  
  13. import org.springframework.data.redis.core.StringRedisTemplate;  
  14. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;  
  15. import org.springframework.data.redis.serializer.StringRedisSerializer;  
  16. import redis.clients.jedis.JedisPoolConfig;  
  17.   
  18. /** 
  19.  * ClassName: RedisConfiguration <br/> 
  20.  * Description: <br/> 
  21.  * date: 2019/7/22 9:17<br/> 
  22.  * 
  23.  * @author lisen01<br /> 
  24.  * @since JDK 1.8 
  25.  */  
  26. @Configuration  
  27. @EnableAutoConfiguration  
  28. public class RedisConfiguration {  
  29.     /* 
  30.     @Bean 
  31.     @ConfigurationProperties(prefix = "spring.redis.pool") 
  32.     public JedisPoolConfig getJedisPoolConfig() { 
  33.         JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); 
  34.         return jedisPoolConfig; 
  35.     } 
  36.  
  37.     @Bean 
  38.     @ConfigurationProperties(prefix = "spring.redis") 
  39.     public JedisConnectionFactory getConnectionFactory() { 
  40.         JedisConnectionFactory factory = new JedisConnectionFactory(); 
  41.         factory.setUsePool(true); 
  42.         JedisPoolConfig jedisPoolConfig = getJedisPoolConfig(); 
  43.         factory.setPoolConfig(jedisPoolConfig); 
  44.         return factory; 
  45.     } 
  46.  
  47.     @Bean 
  48.     public RedisTemplate<?,?> redisTemplate(){ 
  49.         JedisConnectionFactory factory=getConnectionFactory(); 
  50.         RedisTemplate<?,?> redisTemplate=new StringRedisTemplate(factory); 
  51.         return redisTemplate; 
  52.     } 
  53. */  
  54.     /** 
  55.      * redisTemplate 序列化使用的jdkSerializeable, 存储二进制字节码, 所以自定义序列化类 
  56.      * @param redisConnectionFactory 
  57.      * @return 
  58.      */  
  59.     @Bean  
  60.     public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {  
  61.         RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();  
  62.         redisTemplate.setConnectionFactory(redisConnectionFactory);  
  63.   
  64.         // 使用Jackson2JsonRedisSerialize 替换默认序列化  
  65.         Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);  
  66.   
  67.         ObjectMapper objectMapper = new ObjectMapper();  
  68.         objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);  
  69.         objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);  
  70.   
  71.         jackson2JsonRedisSerializer.setObjectMapper(objectMapper);  
  72.   
  73.         // 设置value的序列化规则和 key的序列化规则  
  74.         redisTemplate.setKeySerializer(new StringRedisSerializer());  
  75.         redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);  
  76.         redisTemplate.afterPropertiesSet();  
  77.         return redisTemplate;  
  78.     }  
  79. }  

增加Session配置SessionConfiguration.java

  1. package Net.XiangCaoWuYu.Configurations;  
  2.   
  3. import org.springframework.context.annotation.Configuration;  
  4. import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;  
  5.   
  6. /** 
  7.  * ClassName: SessionConfiguration <br/> 
  8.  * Description: <br/> 
  9.  * date: 2019/7/22 10:45<br/> 
  10.  * 
  11.  * @author lisen01<br /> 
  12.  * @since JDK 1.8 
  13.  */  
  14. @Configuration  
  15. @EnableRedisHttpSession  
  16. public class SessionConfiguration {  
  17. }  

简单测试

  1. UUID uid = (UUID) session.getAttribute("uid");  
  2.         if (uid == null) {  
  3.             uid = UUID.randomUUID();  
  4.         }  
  5.         session.setAttribute("uid", uid);  
  6.         request.getSession().setAttribute("user",JSON.toJSONString(checkUser));  

本文地址:https://www.lisen.me/springboot-shares-session-through-redis.html
版权声明:本文为原创文章,版权归 木子网 所有,欢迎分享本文,转载请保留出处!

发表评论


表情