[英]Unable to store session in Redis using Java Spring Boot Session Data Redis
I think I have problems saving session information in Redis. 我认为我在Redis中保存会话信息时遇到问题。 I've tried to follow the instruction about spring-session-data-redis
, but I can't find any session information within redis when I start a request. 我试图按照spring-session-data-redis
的说明进行操作,但是当我启动请求时,我无法在redis中找到任何会话信息。 The following is my code and config. 以下是我的代码和配置。
application.properties file: application.properties文件:
spring.session.store-type=redis
spring.session.redis.flush-mode= on-save
spring.session.redis.namespace= spring:session
spring.redis.host= 10.10.10.10
spring.redis.port=10000
pom.xml: pom.xml中:
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
<type>jar</type>
</dependency>
Application Configuration class: 应用程序配置类:
@Configuration
@EnableRedisHttpSession
@PropertySource("application.properties")
public class AppConfig {
@Value("${spring.redis.host}")
private String redisHostName;
@Value("${spring.redis.port}")
private int redisPort;
@Bean
JedisConnectionFactory jedisConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(
redisHostName, redisPort);
return new JedisConnectionFactory(redisStandaloneConfiguration);
}
}
And my sample Get request controller: 我的示例获取请求控制器:
// test only
@CrossOrigin
@GetMapping(value = "/test/test")
public String justTest(HttpServletRequest request) {
HttpSession session = request.getSession();
session.setAttribute("sessionId", "ssssss");
String value = (String) session.getAttribute("sessionId").toString();
return value;
}
Am I missing something? 我错过了什么吗? The spring-session-data-redis
is supposed to store session to Redis automatically, but it seems not acting like that. spring-session-data-redis
应该自动将会话存储到Redis,但它似乎不是那样的。 Do I need spring-session
dependency and spring-data-redis
dependency? 我是否需要spring-session
依赖和spring-data-redis
依赖? The redis connection is ok, I set it to listen to all interfaces. redis连接正常,我将其设置为侦听所有接口。
It seems I can retrieve the session id when I start a request, but why the session is not inserted into Redis? 我似乎可以在启动请求时检索会话ID,但为什么会话没有插入Redis?
spring.session.redis.flush-mode= IMMEDIATE
/**
* Flush mode for the Redis sessions. The default is {@code ON_SAVE} which only
* updates the backing Redis when {@link SessionRepository#save(Session)} is invoked.
* In a web environment this happens just before the HTTP response is committed.
* <p>
* Setting the value to {@code IMMEDIATE} will ensure that the any updates to the
* Session are immediately written to the Redis instance.
* @return the {@link RedisFlushMode} to use
* @since 1.1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.