美文网首页
springboot -配置session(三)

springboot -配置session(三)

作者: 尼尔君 | 来源:发表于2018-08-06 14:00 被阅读0次

    重新整理下

    pom依赖

    
     <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>
    

    配置类

    
    @Slf4j
    @Configuration
    
    //开启redisSession 并且设置session存活时间
    @EnableRedisHttpSession(maxInactiveIntervalInSeconds = 600)   
    public class RedisConfig {
    
    
        /**
         *
         * 解决hashmap显示问题
         * @return
         */
        @Bean
        public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
            return new Jackson2JsonRedisSerializer<>(Object.class);
        }
        @Primary
        @Bean
        public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {
            RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
            redisTemplate.setConnectionFactory(connectionFactory);
            redisTemplate.setKeySerializer( new StringRedisSerializer());
            redisTemplate.setValueSerializer(new StringRedisSerializer());
            redisTemplate.setHashKeySerializer(new StringRedisSerializer());
            redisTemplate.setHashValueSerializer(new StringRedisSerializer());
            redisTemplate.afterPropertiesSet();
    
            return redisTemplate;
    
        }
    
    
    
    
    }
    
    
    
    
    

    Redis Session 存储结构

    
    spring:session:expirations:1431527520000: set结构  row :1    value:"expires:6c77cd4f-1f81-4949-9058-b2adbf49572e"
    
    spring:session:sessions:6c77cd4f-1f81-4949-9058-b2adbf49572e:hash结构
    
    key:lastAccessedTime  value:1533534325224   (最后一次访问时间)
    key:creationTime           value:1533534325224   (创建时间)
    key:maxInactiveInterval value:600                      (最大存活时间)
    key:sessionAttr:03539250-edd5-4321-9b82-938796c883d1admin  value : "User{id=null, username='admin', password='null'}" (session.setAttribute设置的keyvalue)
    
    spring:session:sessions:expires:6c77cd4f-1f81-4949-9058-b2adbf49572e  string结构
    

    其中 :

    spring:session:expirations:1431527520000: 为该会话的过期时间截止到什么时候
    spring:session:sessions:6c77cd4f-1f81-4949-9058-b2adbf49572e:储存一些session的信息
    spring:session:sessions:expires:6c77cd4f-1f81-4949-9058-b2adbf49572e: 过期凭证

    相关文章

      网友评论

          本文标题:springboot -配置session(三)

          本文链接:https://www.haomeiwen.com/subject/jwowvftx.html