前言
在 shiro 中每次去攔截請求進行權限認證的時候,都會去數據庫查詢該用戶的所有權限信息, 這個時候就是有一個問題了,因為用戶的權限信息在短時間內是不可變的,每次查詢出來的數據其實都是重復數據,沒必要每次都去重新獲取這個數據,統一放在緩存中進行管理,這個時候,我們只需要獲取一次權限信息,存入到緩存中,待緩存過期后,再次重新獲取即可。
例如,我執行一個查詢多次,它執行多次權限查詢。

使用 Reids 緩存
加入 shiro-redis 依賴
<!-- shiro-redis -->
<dependency>
<groupId>org.crazycake</groupId>
<artifactId>shiro-redis</artifactId>
<version>2.4.2.1-RELEASE</version>
</dependency>
配置 Redis
和前面系統配置 Reids 一樣。
1. 在系統配置文件中加入 Redis 配置參數:
spring:
redis:
host: 192.168.19.200 # host ,默認 localhost
port: 6379 # 端口號,默認6379
pool:
# 設置都是默認值,可以按需求設計
max-active: 8 # 可用連接實例的最大數目,默認值為8;如果賦值為-1,則表示不限制;
max-idle: 8 # 控制一個pool最多有多少個狀態為idle(空閑的)的redis實例,默認值也是8。
max-wait: -1 # 等待可用連接的最大時間,單位毫秒,默認值為-1,表示永不超時。
min-idle: 0 # 控制一個pool最少有多少個狀態為idle(空閑的)的redis實例,默認值為0。
timeout: 0 # 連接超時時間 單位 ms,默認為0
password: master # 密碼,根據自己的 redis 設計,默認為空
- 這個我們是要使用 RedisManager 管理我們的 Redis,它默認沒有注入我們設置的這些參數,需要我們自己手動創建一個注入我們設置的參數。
@ConfigurationProperties(prefix = "spring.redis")
public class CustomRedisManager extends RedisManager {
}
配置 Shiro 緩存
/** * redis 管理 */
@Bean
public CustomRedisManager customRedisManager() {
return new CustomRedisManager();
}
/** * redis 緩存 */
@Bean
public RedisCacheManager cacheManager(CustomRedisManager redisManager) {
RedisCacheManager redisCacheManager = new RedisCacheManager();
redisCacheManager.setRedisManager(redisManager);
return redisCacheManager;
}
@Bean
public SecurityManager securityManager(OAuth2Realm oAuth2Realm, SessionManager sessionManager, RedisCacheManager cacheManager) {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
// 可以添加多個認證,執行順序是有影響的
securityManager.setRealm(oAuth2Realm);
securityManager.setSessionManager(sessionManager);
// 注冊 緩存
securityManager.setCacheManager(cacheManager);
return securityManager;
}
使用測試
我們加入緩存后,看是個什么情況:

執行多次請求,只執行了一次查詢權限的 SQL。
可以去 redis-cli 查看 keys,檢查是否存在權限對象的 key。
使用 Ehcache 緩存
加入依賴
<!-- shiro ehcache -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.3.2</version>
</dependency>
<!-- ehchache -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
Ehcache 配置
新增一個 Ehcache 配置文件 shiro-ehcache.xml:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">
<diskStore path="java.io.tmpdir/Tmp_EhCache" />
<defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" />
<!-- 登錄記錄緩存鎖定1小時 -->
<cache name="passwordRetryCache" maxEntriesLocalHeap="2000" eternal="false" timeToIdleSeconds="3600" timeToLiveSeconds="0" overflowToDisk="false" statistics="true" />
</ehcache>
配置 EhCache 緩存
/** * EhCache 緩存 */
@Bean
public EhCacheManager ehCacheManager() {
EhCacheManager em = new EhCacheManager();
em.setCacheManagerConfigFile("classpath:config/shiro-ehcache.xml");
return em;
}
@Bean
public SecurityManager securityManager(OAuth2Realm oAuth2Realm, SessionManager sessionManager,
RedisCacheManager cacheManager, EhCacheManager ehCacheManager) {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
// 可以添加多個認證,執行順序是有影響的
securityManager.setRealm(oAuth2Realm);
securityManager.setSessionManager(sessionManager);
// 設置緩存
securityManager.setCacheManager(ehCacheManager);
return securityManager;
}
