|
|
|
@ -5,23 +5,38 @@ import org.springframework.context.annotation.Bean;
|
|
|
|
|
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
|
|
|
|
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
|
|
|
|
|
|
|
|
|
|
//这个类用配置redis服务器的连接
|
|
|
|
|
@EnableRedisHttpSession(maxInactiveIntervalInSeconds= 1800)
|
|
|
|
|
// 这个类用于配置Redis服务器的连接,它是整个项目中与Redis会话相关配置的一部分
|
|
|
|
|
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800)
|
|
|
|
|
// @EnableRedisHttpSession注解用于启用Spring Session基于Redis的HTTP会话管理功能,
|
|
|
|
|
// maxInactiveIntervalInSeconds属性设置了会话的最大非活动时间间隔,这里设置为1800秒(即30分钟),
|
|
|
|
|
// 意味着如果一个会话在30分钟内没有任何活动,将会被认为过期并失效。
|
|
|
|
|
public class SessionConfig {
|
|
|
|
|
|
|
|
|
|
// 使用@Value注解从配置文件(例如application.properties或application.yml)中读取名为"redis.hostname"的属性值,
|
|
|
|
|
// 并将其注入到这个变量中,用于后续设置Redis连接的主机名。
|
|
|
|
|
@Value("${redis.hostname}")
|
|
|
|
|
String HostName;
|
|
|
|
|
|
|
|
|
|
// 同样通过@Value注解从配置文件中读取"redis.port"属性值,注入到这个变量,用于设置Redis连接的端口号。
|
|
|
|
|
@Value("${redis.port}")
|
|
|
|
|
int Port;
|
|
|
|
|
|
|
|
|
|
// 从配置文件读取"redis.password"属性值,注入此变量,用于设置连接Redis服务器时的密码(如果有设置密码的话)。
|
|
|
|
|
@Value("${redis.password}")
|
|
|
|
|
String password;
|
|
|
|
|
|
|
|
|
|
// 使用@Bean注解将这个方法返回的对象注册为Spring容器中的一个Bean,
|
|
|
|
|
// 这个Bean就是JedisConnectionFactory类型,用于创建与Redis服务器的连接。
|
|
|
|
|
@Bean
|
|
|
|
|
public JedisConnectionFactory connectionFactory() {
|
|
|
|
|
JedisConnectionFactory connection = new JedisConnectionFactory();
|
|
|
|
|
// 设置JedisConnectionFactory的端口号,使用前面通过@Value注入获取到的Port变量的值,
|
|
|
|
|
// 以此来确定连接Redis服务器的具体端口。
|
|
|
|
|
connection.setPort(Port);
|
|
|
|
|
// 设置JedisConnectionFactory的主机名,使用注入的HostName变量值,明确要连接的Redis服务器主机地址。
|
|
|
|
|
connection.setHostName(HostName);
|
|
|
|
|
// 设置连接Redis服务器的密码,使用password变量值,确保连接的安全性(如果Redis服务器设置了密码验证的话)。
|
|
|
|
|
connection.setPassword(password);
|
|
|
|
|
return connection;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|