一、使用springboot-shiro模块
二、整合
2.1、导入依赖
<!--Shiro整合Ehcache-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
2.2、创建配置文件
在resources/ehcache下新建ehcache-shiro.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!--磁盘的缓存位置-->
<diskStore path="java.io.tmpdir" />
<!--默认缓存-->
<defaultCache
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="3600"
overflowToDisk="false">
</defaultCache>
<!-- 登录认证信息缓存:缓存用户角色权限-->
<cache name="loginRolePsCache"
maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="600"
timeToLiveSeconds="0"
overflowToDisk="false"
statistics="true"/>
</ehcache>
2.3、ShiroConfig
//配置SecurityManager
@Bean
public DefaultWebSecurityManager defaultWebSecurityManager(){
//1.创建defaultWebSecurityManager对象
DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
//2.创建加密对象,设置相关属性
HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
//2.1.采用md5加密
matcher.setHashAlgorithmName("md5");
//2.2.迭代加密的次数
matcher.setHashIterations(3);
//3.将加密对象存储到myRealm中
myRealm.setCredentialsMatcher(matcher);
//4.将myRealm存入defaultWebSecurityManager对象
defaultWebSecurityManager.setRealm(myRealm);
//4.5、设置rememberMe
defaultWebSecurityManager.setRememberMeManager(rememberMeManager());
//4.6、设置缓存管理器
defaultWebSecurityManager.setCacheManager(getEhCacheManager());
//5.返回
return defaultWebSecurityManager;
}
//缓存管理器
public EhCacheManager getEhCacheManager(){
EhCacheManager ehCacheManager = new EhCacheManager();
InputStream is = null;
try {
is = ResourceUtils.getInputStreamForPath("classpath:ehcache/ehcache-shiro.xml");
} catch (IOException e) {
e.printStackTrace();
}
CacheManager cacheManager = new CacheManager(is);
ehCacheManager.setCacheManager(cacheManager);
return ehCacheManager;
}