spring-boot 集成spring-session redis

查看redis是否可用

redis-cli -h 192.168.1.103 -p 6379
auth 123456
keys *

 9) "spring:session:sessions:expires:c1295cf3-e0f7-4f01-8c31-fe85dab6517b"

默认request header:
Cookie:
SESSION=531b91bc-df97-42c4-91cc-3a29ef5d7c15
如果我们有两套不同的子系统A和B,cookies中session名称相同会导致同一个浏览器登录两个系统会造成相互的干扰;
1,不同系统的redis空间不同
2,不同系统的cookie的key不同
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 7200,redisNamespace="nick")
public class RedisHttpSessionConfig {

    @Bean
    public RedisConnectionFactory connectionFactory() {

        JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
        connectionFactory.setPort(6379);
        connectionFactory.setHostName("***");
        connectionFactory.setPassword("***");
        return connectionFactory;
    }

    /*@Bean
    不要使用这个
    public HttpSessionStrategy httpSessionStrategy() {
        return new HeaderHttpSessionStrategy();
    }*/

    @Bean
    public CookieHttpSessionStrategy cookieHttpSessionStrategy(){
        CookieHttpSessionStrategy strategy=new CookieHttpSessionStrategy();
        DefaultCookieSerializer cookieSerializer=new DefaultCookieSerializer();
        cookieSerializer.setCookieName("NICKSESSIONID");//cookies名称
        cookieSerializer.setCookieMaxAge(1800);//过期时间(秒)
        strategy.setCookieSerializer(cookieSerializer);
        return strategy;
    }

}

项目地址 https://github.com/nick8sky/spring-clouds / spring-session

你可能感兴趣的:(Spring-cloud系列)