Skip to content

Latest commit

 

History

History
 
 

spring-boot-shiro-redis

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

spring-boot-shiro, 依赖spring-boot-parent

Add dependencies to pom

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

Shiro-Redis configuration

@Bean(name="shrioRedisCacheManager")
@DependsOn(value="redisTemplate")
public ShrioRedisCacheManager redisCacheManager() {
	ShrioRedisCacheManager cacheManager = new ShrioRedisCacheManager(redisTemplate());
	return cacheManager;
}

@Bean(name="redisTemplate")
public RedisTemplate<byte[], Object> redisTemplate() {
    RedisTemplate<byte[], Object> template = new RedisTemplate<>();
    template.setConnectionFactory(connectionFactory());
    return template;
}

@Bean
public JedisConnectionFactory connectionFactory(){
	JedisConnectionFactory conn = new JedisConnectionFactory();
	conn.setDatabase(3);
	conn.setHostName("127.0.0.1");
	conn.setPassword("123456");
	conn.setPort(6379);
	conn.setTimeout(3000);
	return conn;
}

//CacheManager
public class ShrioRedisCacheManager extends AbstractCacheManager{
	private RedisTemplate<byte[], Object> redisTemplate;
	
	public ShrioRedisCacheManager(RedisTemplate<byte[], Object> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	@Override
	protected Cache<byte[], Object> createCache(String name) throws CacheException {
		return new ShrioRedisCache<byte[], Object>(redisTemplate, name);
	}
}