可以先看一下这个官方的文档
http://docs.spring.io/spring-session/docs/current/reference/html5/guides/boot.html
application.properties中的配置
# REDIS (RedisProperties)
spring.redis.database=4
spring.redis.host=127.0.0.1
spring.redis.port=7001
spring.redis.password=123456
spring.redis.pool.maxActive=8
spring.redis.pool.maxIdle=8
spring.redis.pool.maxWait=-1
spring.redis.pool.minIdle=0
spring.redis.timeout=0
读取属性文件用的
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {
private String database;
private String host;
private String password;
private String maxActive;
private String maxWait;
private String maxIdle;
private String minIdle;
private String port;
private String timeout;
public String getDatabase() {
return database;
}
public void setDatabase(String database) {
this.database = database;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getMaxActive() {
return maxActive;
}
public void setMaxActive(String maxActive) {
this.maxActive = maxActive;
}
public String getMaxWait() {
return maxWait;
}
public void setMaxWait(String maxWait) {
this.maxWait = maxWait;
}
public String getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(String maxIdle) {
this.maxIdle = maxIdle;
}
public String getMinIdle() {
return minIdle;
}
public void setMinIdle(String minIdle) {
this.minIdle = minIdle;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public String getTimeout() {
return timeout;
}
public void setTimeout(String timeout) {
this.timeout = timeout;
}
}
配置文件
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.session.data.redis.RedisOperationsSessionRepository;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
@Configuration
@EnableRedisHttpSession
public class RedisSessionConfigurer {
/**
* 加载属性文件数据
*
* @return
*/
@Bean
public RedisProperties redisProperties() {
return new RedisProperties();
}
/**
* 设置redisTemplate的存储格式(在此与Session没有什么关系)
*
* @return
*/
@Bean
@SuppressWarnings("rawtypes")
public RedisSerializer shrioRedisRedisSerializer() {
return new FastJson2JsonRedisSerializer<Object>(Object.class);
}
/**
* 与Session有关设置链接
*
* @return
*/
@Bean
public RedisOperationsSessionRepository sessionRepository() {
RedisOperationsSessionRepository sessionRepository = new RedisOperationsSessionRepository(connectionFactory());
sessionRepository.setDefaultMaxInactiveInterval(60);// 设置session的有效时长
return sessionRepository;
}
/**
* Redis连接客户端
*
* @return
*/
@Bean(name = "connectionFactory")
public RedisConnectionFactory connectionFactory() {
JedisConnectionFactory conn = new JedisConnectionFactory();
if (StringUtils.isNotEmpty(redisProperties().getDatabase())) {
conn.setDatabase(Integer.valueOf(redisProperties().getDatabase()));
}
conn.setHostName(redisProperties().getHost());
conn.setPort(Integer.valueOf(redisProperties().getPort()));
conn.setTimeout(Integer.valueOf(redisProperties().getTimeout()));
return conn;
}
/**
* RedisTemplate
*
* @return
*/
@Bean(name = "redisTemplate")
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory());
RedisSerializer stringSerializer = new StringRedisSerializer();
template.setKeySerializer(stringSerializer);
template.setValueSerializer(shrioRedisRedisSerializer());
template.setHashKeySerializer(stringSerializer);
template.setHashValueSerializer(shrioRedisRedisSerializer());
template.afterPropertiesSet();
return template;
}
如果和shiro集成的话
ShiroConfigurer.java修改的地方
/** * DefaultWebSessionManager * * @return */ @Bean(name = "sessionManager") public ServletContainerSessionManager sessionManager() { ServletContainerSessionManager sessionManager = new ServletContainerSessionManager(); return sessionManager; }
/** * 权限管理器 * * @return */ @Bean public DefaultWebSecurityManager securityManager() { DefaultWebSecurityManager manager = new DefaultWebSecurityManager(); // 数据库认证的实现 manager.setRealm(userCenterRealm()); // session 管理器 manager.setSessionManager(sessionManager()); // 缓存管理器 manager.setCacheManager(shiroCacheManager()); return manager; }