访问地址:
第一步搭建:https://blog.csdn.net/u014203449/article/details/79330811
访问地址地址:点击打开链接 http://47.98.153.30:8080/ 账号:melo 密码:12345678
github地址:点击打开链接https://github.com/MeloFocus/focus
1.shiro为什么频繁调用doGetAuthorizationInfo授权方法?
自定义realm继承了AuthorizingRealm,实际上是每调用一个需要权限码的接口,会 先执行AuthorizingRealm的getAuthorizationInfo方法,
看AuthorizingRealm源码:
逻辑是先判断缓存中是否有用户的授权信息(用户拥有的操作码),如果有 就直返回不调用自定义 realm的授权方法了, 如果没缓存,再调用自定义realm的doGetAuthorizationInfo。 如果 在安全管理器中注入了 缓存,授权信息就会自动保存在缓存中,下一次调用需要操作码的接口时, 就肯定不会再调用自定义realm授权方法了
protected AuthorizationInfo getAuthorizationInfo(PrincipalCollection principals) {
if (principals == null) {
return null;
}
AuthorizationInfo info = null;
if (log.isTraceEnabled()) {
log.trace("Retrieving AuthorizationInfo for principals [" + principals + "]");
}
Cache<Object, AuthorizationInfo> cache = getAvailableAuthorizationCache();
if (cache != null) {
if (log.isTraceEnabled()) {
log.trace("Attempting to retrieve the AuthorizationInfo from cache.");
}
Object key = getAuthorizationCacheKey(principals);
info = cache.get(key);
if (log.isTraceEnabled()) {
if (info == null) {
log.trace("No AuthorizationInfo found in cache for principals [" + principals + "]");
} else {
log.trace("AuthorizationInfo found in cache for principals [" + principals + "]");
}
}
}
if (info == null) {
// Call template method if the info was not found in a cache
info = doGetAuthorizationInfo(principals);
// If the info is not null and the cache has been created, then cache the authorization info.
if (info != null && cache != null) {
if (log.isTraceEnabled()) {
log.trace("Caching authorization info for principals: [" + principals + "].");
}
Object key = getAuthorizationCacheKey(principals);
cache.put(key, info);
}
}
return info;
}
2.MemoryContrainedCacheManager
shiro提供了一个名为
MemoryConstrainedCacheManager
的内存级别的缓存,其核心是使用hashmap来缓存其中的对象。所以只要new MemoryConstrainedCacheManager()
,然后直接赋值给securityManager
即可。
@Bean
public SecurityManager securityManager() {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
// 设置realm.
securityManager.setRealm(myShiroRealm());
//将缓存注入安全管理器,就不会反复执行 realm的授权方法了;只要实现了shiro的cache接口、CacheManager接口就可以用来注入安全管理器
//shiro自带的一个内存缓存,本质是hashmap,MemoryConstrainedCacheManager(),试验没问题,非常轻,简单的登录用这个
securityManager.setCacheManager(new MemoryConstrainedCacheManager());}
3.用redis缓存
Shiro 提供了一系列让我们自己实现的接口,包括org.apache.shiro.cache.CacheManager 、org.apache.shiro.cache.Cache 等接口。那么我们要对这些做实现,就实现了 Shiro 对 Session 和用户认证信息、用户缓存信息等的缓存,存储。我们可以用缓存,如 Redis 、 memcache 、 EHCache
网上有shiro-redis包,实现了shiro的cache接口、CacheManager接口,而且可以将session托管给redis
<!-- shiro+redis缓存插件 -->
<dependency>
<groupId>org.crazycake</groupId>
<artifactId>shiro-redis</artifactId>
<version>2.4.2.1-RELEASE</version>
</dependency>
shiro配置:
@Bean
public SecurityManager securityManager() {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
// 设置realm.
securityManager.setRealm(myShiroRealm());
//将缓存注入安全管理器,就不会反复执行 realm的授权方法了;只要实现了shiro的cache接口、CacheManager接口就可以用来注入安全管理器
//shiro自带的一个内存缓存,本质是hashmap,MemoryConstrainedCacheManager(),试验没问题,非常轻,简单的登录用这个
//securityManager.setCacheManager(new MemoryConstrainedCacheManager());
//用了redis缓存注入安全管理器,会报一个序列化失败的错误,推测是new SimpleAuthenticationInfo 时 user对象无法序列化,加上序列化就好了
securityManager.setCacheManager(cacheManager());
//将session托管给redis,nigix试验分布式,确实 做到了session共享
securityManager.setSessionManager(SessionManager());
return securityManager;
}
/**
* 身份认证realm; (这个需要自己写,账号密码校验;权限等)
* @return
*/
@Bean
public MyShiroRealm myShiroRealm() {
MyShiroRealm myShiroRealm = new MyShiroRealm();
return myShiroRealm;
}
/**
* 配置shiro redisManager
* 网上的一个 shiro-redis 插件,实现了shiro的cache接口、CacheManager接口就
* @return
*/
@Bean
public RedisManager redisManager() {
RedisManager redisManager = new RedisManager();
redisManager.setHost("localhost");
redisManager.setPort(6379);
redisManager.setExpire(18000);// 配置过期时间
// redisManager.setTimeout(timeout);
// redisManager.setPassword(password);
return redisManager;
}
/**
* cacheManager 缓存 redis实现
* 网上的一个 shiro-redis 插件
* @return
*/
@Bean
public RedisCacheManager cacheManager() {
RedisCacheManager redisCacheManager = new RedisCacheManager();
redisCacheManager.setRedisManager(redisManager());
return redisCacheManager;
}
/**
* RedisSessionDAO shiro sessionDao层的实现 通过redis
*/
public RedisSessionDAO redisSessionDAO() {
RedisSessionDAO redisSessionDAO = new RedisSessionDAO();
redisSessionDAO.setRedisManager(redisManager());
return redisSessionDAO;
}
/**
* shiro session的管理
*/
public DefaultWebSessionManager SessionManager() {
DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
sessionManager.setSessionDAO(redisSessionDAO());
return sessionManager;
}