redis是啥时候把shiro中的权限信息放到缓存中的

前言:

自己自定了缓存管理器public class RedisCacheManager implements CacheManager,

package com.hdys.www.background.shiro;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.hdys.www.background.shiro.RedisCache;


@Service  
public class RedisCacheManager implements CacheManager {  
      
    @Autowired  
    private RedisTemplate redisTemplate; 
    
    @Override  
    public <K, V> Cache<K, V> getCache(String name) throws CacheException {  
      
        return  new RedisCache<K, V>(120, redisTemplate);
    }  
  
}  

自定义了Redis缓存class RedisCache<K, V> implements Cache<K, V>

package com.hdys.www.background.shiro;

import java.util.Collection;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.springframework.data.redis.core.RedisTemplate;

public class RedisCache<K, V> implements Cache<K, V> {

private long expireTime = 120;

private RedisTemplate<K, V> redisTemplate;

public RedisCache() {
super();
}


public RedisCache(long expireTime, RedisTemplate<K, V> redisTemplate) {
super();
this.expireTime = expireTime;
this.redisTemplate = redisTemplate;
}


/**
* 通过key来获取对应的缓存对象
*/
@Override
public V get(K key) throws CacheException {
return redisTemplate.opsForValue().get(key);
}


/**
* 将权限信息加入缓存中
*/
@Override
public V put(K key, V value) throws CacheException {
redisTemplate.opsForValue().set(key, value, this.expireTime, TimeUnit.SECONDS);
return value;
}


/**
* 将权限信息从缓存中删除
*/
@Override
public V remove(K key) throws CacheException {
V v = redisTemplate.opsForValue().get(key);
redisTemplate.opsForValue().getOperations().delete(key);
return v;
}


@Override
public void clear() throws CacheException {

}


@Override
public int size() {
return 0;
}


@Override
public Set<K> keys() {
return null;
}


@Override
public Collection<V> values() {
return null;
}


}

在shiroConfig中设置注入自定义的缓存管理器

@Bean
public SecurityManager securityManager() {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
// 设置realm.
securityManager.setRealm(myShiroRealm());
// 自定义缓存实现 使用redis
securityManager.setCacheManager(redisCacheManager);

// 自定义session管理 使用redis
securityManager.setSessionManager(sessionManager());
//注入记住我管理器;
    securityManager.setRememberMeManager(rememberMeManager());
  //注入安全管理器,里面包含了大部分信息,比较重要;

    SecurityUtils.setSecurityManager(securityManager);

return securityManager;

}


顺序:

其他顺序略。。。。。。。。

1:调用doGetAuthorizationInfo权限认证,获取角色信息和权限信息

  2:调用AuthorizingRealm的 protected AuthorizationInfo getAuthorizationInfo(PrincipalCollection principals) 方法                  里面调用了这句话:cache.put(key, info);

这个Key默认的是你当前登录人的信息,info里存的是你的角色和权限信息,具体流程细节暂未研究,这个key感觉可以自定义

猜你喜欢

转载自blog.csdn.net/carrybest/article/details/80699095