1. 程式人生 > >Spring Boot Shiro 許可權資訊快取處理,記住我,thymleaf使用shiro標籤

Spring Boot Shiro 許可權資訊快取處理,記住我,thymleaf使用shiro標籤

轉:
http://412887952-qq-com.iteye.com/blog/2299784

許可權資訊快取處理

實際中我們的許可權資訊是不怎麼會改變的,所以我們希望是第一次訪問,然後進行快取處理,那麼Shiro是否支援呢,答案是肯定的,我們在下一小節進行講解,如何在Shiro中加入快取機制。

主要分這麼幾個步驟:在pom.xml中加入快取依賴;注入快取;
(a) 在pom.xml檔案中加入依賴:
  1. <!-- shiro ehcache -->
  2.         <dependency>
  3.             <groupId
    >org.apache.shiro</groupId>
  4.             <artifactId>shiro-ehcache</artifactId>
  5.             <version>1.2.3</version>
  6.         </dependency>
  7.         <!-- 包含支援UI模版(Velocity,FreeMarker,JasperReports), 郵件服務, 指令碼服務(JRuby), 快取Cache(EHCache),   
  8.             任務計劃Scheduling(uartz)。 -->
  9.         <dependency>
  10.             <groupId>org.springframework</groupId>
  11.             <artifactId>spring-context-support</artifactId>
  12.         </dependency>
  13.     </dependencies>
(b)注入快取
在ShiroConfiguration中加入如下方法:
  1. /**  
  2.     * shiro快取管理器;  
  3.     * 需要注入對應的其它的實體類中:  
  4.     * 1、安全管理器:securityManager  
  5.     * 可見securityManager是整個shiro的核心;  
  6.     * @return  
  7.     */  
  8.    @Bean  
  9.    public EhCacheManager ehCacheManager(){  
  10.       System.out.println("ShiroConfiguration.getEhCacheManager()");  
  11.       EhCacheManager cacheManager = new EhCacheManager();  
  12.       cacheManager.setCacheManagerConfigFile("classpath:config/ehcache-shiro.xml");  
  13.       return cacheManager;  
  14.    }  

將快取物件注入到SecurityManager中:
  1. @Bean  
  2. public SecurityManager securityManager(){  
  3.        DefaultWebSecurityManager securityManager =  new DefaultWebSecurityManager();  
  4.        //設定realm.  
  5.        securityManager.setRealm(myShiroRealm());  
  6.        //注入快取管理器;  
  7.        securityManager.setCacheManager(ehCacheManager());//這個如果執行多次,也是同樣的一個物件;  
  8.        return securityManager;  
  9.     }  
(c)新增快取配置檔案:
在src/main/resouces/config新增ehcache-shiro.xml配置檔案:
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <ehcachename="es">
  3.     <diskStorepath="java.io.tmpdir"/>
  4.          <!--  
  5.        name:快取名稱。  
  6.        maxElementsInMemory:快取最大數目  
  7.        maxElementsOnDisk:硬碟最大快取個數。   
  8.        eternal:物件是否永久有效,一但設定了,timeout將不起作用。   
  9.        overflowToDisk:是否儲存到磁碟,當系統當機時  
  10.        timeToIdleSeconds:設定物件在失效前的允許閒置時間(單位:秒)。僅當eternal=false物件不是永久有效時使用,可選屬性,預設值是0,也就是可閒置時間無窮大。  
  11.        timeToLiveSeconds:設定物件在失效前允許存活時間(單位:秒)。最大時間介於建立時間和失效時間之間。僅當eternal=false物件不是永久有效時使用,預設是0.,也就是物件存活時間無窮大。  
  12.        diskPersistent:是否快取虛擬機器重啟期資料 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.   
  13.        diskSpoolBufferSizeMB:這個引數設定DiskStore(磁碟快取)的快取區大小。預設是30MB。每個Cache都應該有自己的一個緩衝區。   
  14.        diskExpiryThreadIntervalSeconds:磁碟失效執行緒執行時間間隔,預設是120秒。  
  15.        memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據指定的策略去清理記憶體。預設策略是LRU(最近最少使用)。你可以設定為FIFO(先進先出)或是LFU(較少使用)。   
  16.        clearOnFlush:記憶體數量最大時是否清除。  
  17.        memoryStoreEvictionPolicy:  
  18.             Ehcache的三種清空策略;  
  19.             FIFO,first in first out,這個是大家最熟的,先進先出。  
  20.             LFU, Less Frequently Used,就是上面例子中使用的策略,直白一點就是講一直以來最少被使用的。如上面所講,快取的元素有一個hit屬性,hit值最小的將會被清出快取。  
  21.             LRU,Least Recently Used,最近最少使用的,快取的元素有一個時間戳,當快取容量滿了,而又需要騰出地方來快取新的元素的時候,那麼現有快取元素中時間戳離當前時間最遠的元素將被清出快取。  
  22.     -->
  23.     <defaultCache
  24.             maxElementsInMemory="10000"
  25.             eternal="false"
  26.             timeToIdleSeconds="120"
  27.             timeToLiveSeconds="120"
  28.             overflowToDisk="false"
  29.             diskPersistent="false"
  30.             diskExpiryThreadIntervalSeconds="120"
  31.             />
  32.     <!-- 登入記錄快取鎖定10分鐘 -->
  33.     <cachename="passwordRetryCache"
  34.            maxEntriesLocalHeap="2000"
  35.            eternal="false"
  36.            timeToIdleSeconds="3600"
  37.            timeToLiveSeconds="0"
  38.            overflowToDisk="false"
  39.            statistics="true">
  40.     </cache>
  41. </ehcache>
在配置檔案上已經有很詳細的解釋了,所以這裡就過多介紹ehcache的配置了。
執行程式訪問:http://127.0.0.1:8080/userInfo/userAdd
檢視控制檯的列印資訊:
許可權配置-->MyShiroRealm.doGetAuthorizationInfo()

這個資訊就只打印一次了,說明我們的快取生效了

密碼多次輸入錯誤

CredentialsMatcher是shiro提供的用於加密密碼和驗證密碼服務的介面,而HashedCredentialsMatcher正是CredentialsMatcher的一個實現類

  1. public class SimpleCredentialsMatcher extends CodecSupport implements CredentialsMatcher  
  2. public class HashedCredentialsMatcher extends SimpleCredentialsMatcher  

自定義RetryLimitHashedCredentialsMatcher繼承HashedCredentialsMatcher
  1. package com.example.config.shiro;  
  2. import java.util.concurrent.atomic.AtomicInteger;  
  3. import org.apache.shiro.authc.AuthenticationInfo;  
  4. import org.apache.shiro.authc.AuthenticationToken;  
  5. import org.apache.shiro.authc.ExcessiveAttemptsException;  
  6. import org.apache.shiro.authc.credential.HashedCredentialsMatcher;  
  7. import org.apache.shiro.cache.Cache;  
  8. import org.apache.shiro.cache.CacheManager;  
  9. public class RetryLimitHashedCredentialsMatcher extends  HashedCredentialsMatcher{  
  10.     private Cache<String, AtomicInteger> passwordRetryCache;    
  11.     public RetryLimitHashedCredentia