springboot shiro ehcache redis 简单使用
2020-12-13 04:06
标签:url led 关闭 start orm wired password redist spring 引入相关pom 配置properties 编写ShiroConfig 配置类 编写RedisCacheSessionDAO springboot shiro ehcache redis 简单使用 标签:url led 关闭 start orm wired password redist spring 原文地址:https://www.cnblogs.com/rchao/p/11104394.html dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-webartifactId>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-data-redisartifactId>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-cacheartifactId>
dependency>
dependency>
groupId>org.apache.shirogroupId>
artifactId>shiro-spring-boot-web-starterartifactId>
version>1.4.1version>
dependency>
dependency>
groupId>org.apache.shirogroupId>
artifactId>shiro-ehcacheartifactId>
version>1.4.1version>
dependency>
shiro.loginUrl=/login
shiro.unauthorizedUrl=/403
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=123456
spring.redis.timeout=2000
@Bean
public Realm realm(){
UserRealm userRealm = new UserRealm();
userRealm.setCredentialsMatcher(hashedCredentialsMatcher());
return userRealm;
}
@Bean
@DependsOn({"lifecycleBeanPostProcessor"})
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
/**
* 处理@RequiresRole等shiro注解失效问题
*/
//autoProxyCreator.setUsePrefix(true);
autoProxyCreator.setProxyTargetClass(true);
return autoProxyCreator;
}
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(){
AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
return authorizationAttributeSourceAdvisor;
}
@Bean
public ShiroFilterChainDefinition shiroFilterChainDefinition(){
DefaultShiroFilterChainDefinition chain = new DefaultShiroFilterChainDefinition();
Map
public class RedisCacheSessionDAO extends EnterpriseCacheSessionDAO {
private static final Logger log = LoggerFactory.getLogger(RedisCacheSessionDAO.class);
// Session超时时间,单位为秒
private long expireTime = 3600;
@Autowired
private RedisTemplate redisTemplate;
public RedisCacheSessionDAO() {
super();
}
@Override
protected Serializable doCreate(Session session) {
Serializable sessionId = generateSessionId(session);
assignSessionId(session,sessionId);
redisTemplate.opsForValue().set(sessionId, session, expireTime, TimeUnit.SECONDS);
return sessionId;
}
@Override
protected Session doReadSession(Serializable sessionId) {
if(sessionId == null){
return null;
}
//这里一定要在redis拿到session返回,不然在记住账号(remeberMe=true)的情况下一直生成新的session,不记住账号的情况下登录不上session倒是没少生成 单机下这里返回空是没啥问题的
return (Session) redisTemplate.opsForValue().get(sessionId);
}
@Override
protected void doUpdate(Session session) {
super.doUpdate(session);
if(session == null){
return;
}
session.setTimeout(expireTime * 1000);
redisTemplate.opsForValue().set(session.getId(), session, expireTime, TimeUnit.SECONDS);
}
@Override
protected void doDelete(Session session) {
super.doDelete(session);
if(session == null){
return;
}
redisTemplate.opsForValue().getOperations().delete(session.getId());
}
public long getExpireTime() {
return expireTime;
}
public void setExpireTime(long expireTime) {
this.expireTime = expireTime;
}
public RedisTemplate getRedisTemplate() {
return redisTemplate;
}
public void setRedisTemplate(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
}
文章标题:springboot shiro ehcache redis 简单使用
文章链接:http://soscw.com/essay/28875.html