引
因为每次都要查一遍数据库拿授权,更别说用户特别多,并发什么的了,数据库负担太大,所以就要启用缓存
Cache Manager
shiro架构里边有一个管理缓存的组件,默认用ehcache实现,看一下它的缓存管理
public abstract class CachingRealm implements Realm, Nameable, CacheManagerAware, LogoutAware {
private static final Logger log = LoggerFactory.getLogger(CachingRealm.class);
private static final AtomicInteger INSTANCE_COUNT = new AtomicInteger();
private String name;
private boolean cachingEnabled = true;
private CacheManager cacheManager;
里边有个CacheManager ,说明以后可以通过他修改缓存的实现,先用EhCacheManager整起。
pom依赖加入
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.7.0</version>
</dependency>
shiro配置类关于自定义Realm的部分开启缓存即可
//创建自定义Realm
@Bean("customerRealm")
public Realm getRealm(){
CustomerRealm customerRealm = new CustomerRealm();
//修改凭证校验匹配器
HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
//设置算法为MD5
hashedCredentialsMatcher.setHashAlgorithmName("md5");
//设置散列次数
hashedCredentialsMatcher.setHashIterations(1024);
customerRealm.setCredentialsMatcher(hashedCredentialsMatcher);
//开启缓存管理
customerRealm.setCacheManager(new EhCacheManager());
customerRealm.setCachingEnabled(true);//开启全局缓存
customerRealm.setAuthenticationCachingEnabled(true);//开启认证缓存
customerRealm.setAuthenticationCacheName("authenticationCache");//认证缓存名字
customerRealm.setAuthorizationCachingEnabled(true);//开启授权缓存
customerRealm.setAuthorizationCacheName("authorizationCache");//授权缓存名字
return customerRealm;
}

本文介绍了在SpringBoot中使用Shiro进行权限管理时如何启用缓存以减轻数据库负担。通过Shiro的Cache Manager组件,文章展示了默认的EhCache实现,并说明了如何在配置类中开启缓存。
1960

被折叠的 条评论
为什么被折叠?



