Shiro整合Springboot缓存之EhCache实现

Shiro整合Springboot缓存之EhCache实现

Cache作用

1.Cache缓存:计算机内存中一段数据
2.作用:用来减轻DB的访问压力,从而提高系统的查询效率
3.流程:
在这里插入图片描述

引入shiro和ehcache的整合依赖

<!--引入shiro和ehcahce依赖-->
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-ehcache</artifactId>
    <version>1.5.3</version>
</dependency>

开启缓存

//3.创建自定义Realm
@Bean
public Realm getRealm(){
    
    
    CustomerRealm customerRealm = new CustomerRealm();
    //修改凭证校验匹配器
    HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
    //设置加密算法为md5
    credentialsMatcher.setHashAlgorithmName("MD5");
    //设置散列次数
    credentialsMatcher.setHashIterations(1024);
    customerRealm.setCredentialsMatcher(credentialsMatcher);

    //开启缓存管理
    customerRealm.setCacheManager(new EhCacheManager());
    customerRealm.setCachingEnabled(true);  //开启全局缓存
    customerRealm.setAuthenticationCachingEnabled(true);  //开启认证缓存
    customerRealm.setAuthenticationCacheName("authenticationCache");
    customerRealm.setAuthorizationCachingEnabled(true);  //开启授权缓存
    customerRealm.setAuthorizationCacheName("authorizationCache");

    return customerRealm;
}

其它的代码实现可以参考:Springboot整合Shiro

猜你喜欢

转载自blog.csdn.net/muriyue6/article/details/120388264