Spring Boot通过Redis共享Session

Laughing
2019-07-22 / 0 评论 / 1,318 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2024年03月18日,已超过341天没有更新,若内容或图片失效,请留言反馈。

温馨提示
以下内容基于SpringBoot 1.2.7版本

<parent>  
       <groupId>org.springframework.boot</groupId>  
       <artifactId>spring-boot-starter-parent</artifactId>  
       <version>1.2.7.RELEASE</version>  
   </parent>

添加依赖

主要是添加如下两个依赖

<!-- 添加redis-->  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-redis</artifactId>  
            <version>1.3.8.RELEASE</version>  
        </dependency>  
<!--        session依赖-->  
        <dependency>  
            <groupId>org.springframework.session</groupId>  
            <artifactId>spring-session-data-redis</artifactId>  
            <version>1.3.5.RELEASE</version>  
        </dependency>  

安装Redis

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

配置Redis

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

#redis配置  
  redis:  
    host: 我是IP  
    port: 6379  
    password: 我是密码  
    timeout: 0  
    pool:  
      max-active: 100  
      max-idle: 10  
      max-wait: 100000  
    database: 0 

增加Redis配置类RedisConfiguration.java

package Net.XiangCaoWuYu.Configurations;  
  
import com.fasterxml.jackson.annotation.JsonAutoDetect;  
import com.fasterxml.jackson.annotation.PropertyAccessor;  
import com.fasterxml.jackson.databind.ObjectMapper;  
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;  
import org.springframework.boot.context.properties.ConfigurationProperties;  
import org.springframework.context.annotation.Bean;  
import org.springframework.context.annotation.Configuration;  
import org.springframework.data.redis.connection.RedisConnectionFactory;  
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;  
import org.springframework.data.redis.core.RedisTemplate;  
import org.springframework.data.redis.core.StringRedisTemplate;  
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;  
import org.springframework.data.redis.serializer.StringRedisSerializer;  
import redis.clients.jedis.JedisPoolConfig;  
  
/** 
 * ClassName: RedisConfiguration <br/> 
 * Description: <br/> 
 * date: 2019/7/22 9:17<br/> 
 * 
 * @author lisen01<br /> 
 * @since JDK 1.8 
 */  
@Configuration  
@EnableAutoConfiguration  
public class RedisConfiguration {  
    /* 
    @Bean 
    @ConfigurationProperties(prefix = "spring.redis.pool") 
    public JedisPoolConfig getJedisPoolConfig() { 
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); 
        return jedisPoolConfig; 
    } 
 
    @Bean 
    @ConfigurationProperties(prefix = "spring.redis") 
    public JedisConnectionFactory getConnectionFactory() { 
        JedisConnectionFactory factory = new JedisConnectionFactory(); 
        factory.setUsePool(true); 
        JedisPoolConfig jedisPoolConfig = getJedisPoolConfig(); 
        factory.setPoolConfig(jedisPoolConfig); 
        return factory; 
    } 
 
    @Bean 
    public RedisTemplate<?,?> redisTemplate(){ 
        JedisConnectionFactory factory=getConnectionFactory(); 
        RedisTemplate<?,?> redisTemplate=new StringRedisTemplate(factory); 
        return redisTemplate; 
    } 
*/  
    /** 
     * redisTemplate 序列化使用的jdkSerializeable, 存储二进制字节码, 所以自定义序列化类 
     * @param redisConnectionFactory 
     * @return 
     */  
    @Bean  
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {  
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();  
        redisTemplate.setConnectionFactory(redisConnectionFactory);  
  
        // 使用Jackson2JsonRedisSerialize 替换默认序列化  
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);  
  
        ObjectMapper objectMapper = new ObjectMapper();  
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);  
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);  
  
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);  
  
        // 设置value的序列化规则和 key的序列化规则  
        redisTemplate.setKeySerializer(new StringRedisSerializer());  
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);  
        redisTemplate.afterPropertiesSet();  
        return redisTemplate;  
    }  
}

增加Session配置SessionConfiguration.java

package Net.XiangCaoWuYu.Configurations;  
  
import org.springframework.context.annotation.Configuration;  
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;  
  
/** 
 * ClassName: SessionConfiguration <br/> 
 * Description: <br/> 
 * date: 2019/7/22 10:45<br/> 
 * 
 * @author lisen01<br /> 
 * @since JDK 1.8 
 */  
@Configuration  
@EnableRedisHttpSession  
public class SessionConfiguration {  
}

简单测试

UUID uid = (UUID) session.getAttribute("uid");  
        if (uid == null) {  
            uid = UUID.randomUUID();  
        }  
        session.setAttribute("uid", uid);  
        request.getSession().setAttribute("user",JSON.toJSONString(checkUser));
0

评论 (0)

取消
  1. 头像
    Laughing 作者
    Windows 10 · Google Chrome

    画图

    回复
  2. 头像
    xsxddot
    MacOS · Google Chrome

    666666666666666厉害啊

    回复