Spring Boot Shiro 許可權資訊快取處理,記住我,thymleaf使用shiro標籤
阿新 • • 發佈:2019-02-07
轉:
http://412887952-qq-com.iteye.com/blog/2299784
許可權資訊快取處理
實際中我們的許可權資訊是不怎麼會改變的,所以我們希望是第一次訪問,然後進行快取處理,那麼Shiro是否支援呢,答案是肯定的,我們在下一小節進行講解,如何在Shiro中加入快取機制。
主要分這麼幾個步驟:在pom.xml中加入快取依賴;注入快取;(a) 在pom.xml檔案中加入依賴:
- <!-- shiro ehcache -->
- <dependency>
-
<groupId
- <artifactId>shiro-ehcache</artifactId>
- <version>1.2.3</version>
- </dependency>
- <!-- 包含支援UI模版(Velocity,FreeMarker,JasperReports), 郵件服務, 指令碼服務(JRuby), 快取Cache(EHCache),
-
任務計劃Scheduling(uartz)。 -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- </dependency>
- </dependencies>
在ShiroConfiguration中加入如下方法:
- /**
- * shiro快取管理器;
-
* 需要注入對應的其它的實體類中:
- * 1、安全管理器:securityManager
- * 可見securityManager是整個shiro的核心;
- * @return
- */
- @Bean
- public EhCacheManager ehCacheManager(){
- System.out.println("ShiroConfiguration.getEhCacheManager()");
- EhCacheManager cacheManager = new EhCacheManager();
- cacheManager.setCacheManagerConfigFile("classpath:config/ehcache-shiro.xml");
- return cacheManager;
- }
將快取物件注入到SecurityManager中:
- @Bean
- public SecurityManager securityManager(){
- DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
- //設定realm.
- securityManager.setRealm(myShiroRealm());
- //注入快取管理器;
- securityManager.setCacheManager(ehCacheManager());//這個如果執行多次,也是同樣的一個物件;
- return securityManager;
- }
在src/main/resouces/config新增ehcache-shiro.xml配置檔案:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <ehcachename="es">
- <diskStorepath="java.io.tmpdir"/>
- <!--
- name:快取名稱。
- maxElementsInMemory:快取最大數目
- maxElementsOnDisk:硬碟最大快取個數。
- eternal:物件是否永久有效,一但設定了,timeout將不起作用。
- overflowToDisk:是否儲存到磁碟,當系統當機時
- timeToIdleSeconds:設定物件在失效前的允許閒置時間(單位:秒)。僅當eternal=false物件不是永久有效時使用,可選屬性,預設值是0,也就是可閒置時間無窮大。
- timeToLiveSeconds:設定物件在失效前允許存活時間(單位:秒)。最大時間介於建立時間和失效時間之間。僅當eternal=false物件不是永久有效時使用,預設是0.,也就是物件存活時間無窮大。
- diskPersistent:是否快取虛擬機器重啟期資料 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
- diskSpoolBufferSizeMB:這個引數設定DiskStore(磁碟快取)的快取區大小。預設是30MB。每個Cache都應該有自己的一個緩衝區。
- diskExpiryThreadIntervalSeconds:磁碟失效執行緒執行時間間隔,預設是120秒。
- memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據指定的策略去清理記憶體。預設策略是LRU(最近最少使用)。你可以設定為FIFO(先進先出)或是LFU(較少使用)。
- clearOnFlush:記憶體數量最大時是否清除。
- memoryStoreEvictionPolicy:
- Ehcache的三種清空策略;
- FIFO,first in first out,這個是大家最熟的,先進先出。
- LFU, Less Frequently Used,就是上面例子中使用的策略,直白一點就是講一直以來最少被使用的。如上面所講,快取的元素有一個hit屬性,hit值最小的將會被清出快取。
- LRU,Least Recently Used,最近最少使用的,快取的元素有一個時間戳,當快取容量滿了,而又需要騰出地方來快取新的元素的時候,那麼現有快取元素中時間戳離當前時間最遠的元素將被清出快取。
- -->
- <defaultCache
- maxElementsInMemory="10000"
- eternal="false"
- timeToIdleSeconds="120"
- timeToLiveSeconds="120"
- overflowToDisk="false"
- diskPersistent="false"
- diskExpiryThreadIntervalSeconds="120"
- />
- <!-- 登入記錄快取鎖定10分鐘 -->
- <cachename="passwordRetryCache"
- maxEntriesLocalHeap="2000"
- eternal="false"
- timeToIdleSeconds="3600"
- timeToLiveSeconds="0"
- overflowToDisk="false"
- statistics="true">
- </cache>
- </ehcache>
執行程式訪問:http://127.0.0.1:8080/userInfo/userAdd
檢視控制檯的列印資訊:
許可權配置-->MyShiroRealm.doGetAuthorizationInfo()
這個資訊就只打印一次了,說明我們的快取生效了
密碼多次輸入錯誤
CredentialsMatcher是shiro提供的用於加密密碼和驗證密碼服務的介面,而HashedCredentialsMatcher正是CredentialsMatcher的一個實現類
- public class SimpleCredentialsMatcher extends CodecSupport implements CredentialsMatcher
- public class HashedCredentialsMatcher extends SimpleCredentialsMatcher
自定義RetryLimitHashedCredentialsMatcher繼承HashedCredentialsMatcher
- package com.example.config.shiro;
- import java.util.concurrent.atomic.AtomicInteger;
- import org.apache.shiro.authc.AuthenticationInfo;
- import org.apache.shiro.authc.AuthenticationToken;
- import org.apache.shiro.authc.ExcessiveAttemptsException;
- import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
- import org.apache.shiro.cache.Cache;
- import org.apache.shiro.cache.CacheManager;
- public class RetryLimitHashedCredentialsMatcher extends HashedCredentialsMatcher{
- private Cache<String, AtomicInteger> passwordRetryCache;
- public RetryLimitHashedCredentia