Springsession和redis混合会话

时间:2017-10-03 09:48:31

标签: java session redis spring-session

我们使用redis配置springsessions作为会话存储。在更大的负载下,它结束了用户碰巧找回随机会话数据的结果。

不幸的是,我们无法再次重现它,并且在测试环境中无法重现。

有没有人有类似的经历,可以指出一个方向看什么?

1 个答案:

答案 0 :(得分:1)

我在我的项目中使用了redis会话存储的spring会话如下,它正常工作,我分享它,希望它有所帮助!

使用了spring boot starter version:1.4.3

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>

     <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session</artifactId>
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>4.2.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>biz.paluch.redis</groupId>
        <artifactId>lettuce</artifactId>
        <version>3.5.0.Final</version>
    </dependency>

Spring session redis配置类:

package az.com.cybernet.aas.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.lettuce.
LettuceConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.
EnableRedisHt
tpSession;
import org.springframework.session.web.http.CookieHttpSessionStrategy;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;
import org.springframework.session.web.http.HttpSessionStrategy;



@EnableRedisHttpSession
public class RedisConfig {

@Value("${spring.redis.host}")
private String hostname;
@Value("${spring.redis.port}")
private String port;
// @Value("${spring.redis.password}")
// private String password;

@Bean
public LettuceConnectionFactory connectionFactory() {
    LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory();
    connectionFactory.setHostName(hostname);
    // connectionFactory.setPassword(password);
    connectionFactory.setDatabase(2);
    connectionFactory.setPort(Integer.parseInt(port));
    return connectionFactory;
}

@Bean
public CookieSerializer cookieSerializer() {
    DefaultCookieSerializer serializer = new DefaultCookieSerializer();
    serializer.setCookieName("JSESSIONID");
    serializer.setCookiePath("/");
    serializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$");
    return serializer;
}

@Bean
public HttpSessionStrategy strategy() {
    CookieHttpSessionStrategy cookieHttpSessionStrategy = new CookieHttpSessionStrategy();

    cookieHttpSessionStrategy.setCookieSerializer(cookieSerializer());
    return cookieHttpSessionStrategy;

}

}