springboot中使用緩存shiro-ehcache

在pom.xml中注入緩存依賴,版本(Sep 09, 2016)
spring-context-support
包含支持UI模版(Velocity,FreeMarker,JasperReports),
郵件服務,
腳本服務(JRuby),
緩存Cache(EHCache),
任務計劃Scheduling(uartz)。

<dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-ehcache</artifactId><version>1.3.2</version>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.3.7.RELEASE</version>
</dependency>

在ShiroConfiguration中注入緩存

/*** shiro緩存管理器;* 需要注入對應的其它的實體類中:* 1、安全管理器:securityManager* 可見securityManager是整個shiro的核心;* @return*/@Beanpublic EhCacheManager ehCacheManager(){System.out.println("ShiroConfiguration.getEhCacheManager()");EhCacheManager cacheManager = new EhCacheManager();cacheManager.setCacheManagerConfigFile("classpath:config/ehcache-shiro.xml");return cacheManager;}

將緩存對象注入到SecurityManager中:

@Beanpublic SecurityManager securityManager(){DefaultWebSecurityManager securityManager =  new DefaultWebSecurityManager();securityManager.setRealm(myShiroRealm());securityManager.setCacheManager(ehCacheManager());return securityManager;}

添加緩存配置文件:
在src/main/resouces/config添加ehcache-shiro.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="es"><diskStore path="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,最近最少使用的,緩存的元素有一個時間戳,當緩存容量滿了,而又需要騰出地方來緩存新的元素的時候,那么現有緩存元素中時間戳離當前時間最遠的元素將被清出緩存。--><defaultCachemaxElementsInMemory="10000"eternal="false"timeToIdleSeconds="120"timeToLiveSeconds="120"overflowToDisk="false"diskPersistent="false"diskExpiryThreadIntervalSeconds="120"/><!-- 登錄記錄緩存鎖定10分鐘 --><cache name="passwordRetryCache"maxEntriesLocalHeap="2000"eternal="false"timeToIdleSeconds="3600"timeToLiveSeconds="0"overflowToDisk="false"statistics="true"></cache></ehcache>

在配置文件上已經有很詳細的解釋了,所以這里就過多介紹ehcache的配置了。
運行程序訪問:http://127.0.0.1:8080/userInfo/userAdd
查看控制臺的打印信息:
權限配置-->MyShiroRealm.doGetAuthorizationInfo()
這個信息就只打印一次了,說明我們的緩存生效了。

文章主要參考于作者林祥纖的博客

http://412887952-qq-com.iteye.com/blog/2299780

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541751.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541751.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541751.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

oracle 微信公眾號,關于微信公眾號貼代碼的方法

微信公眾號碼上貼代碼一直一來都是個頭疼的問題。吐槽一句&#xff1a;要是后臺編輯器支持markdown就好了。今天教大家用在線markdown排版工具&#xff0c;把代碼完美貼到微信公眾號上。長話短說&#xff0c;今天用到的兩個工具&#xff1a;首先&#xff0c;以一段代碼為例。假…

計算理論 形式語言與自動機_下推式自動機(PDA)| 計算理論

計算理論 形式語言與自動機Pushdown Automaton (PDA) is a kind of Automaton which comes under the theory of Computation that appoints stack. The word Pushdown stands due to the fact that the stack can be pushed down as operations can only work on the elements…

運維人員究竟如何提升價值,持續獲得高薪?

作者簡介&#xff1a;老男孩&#xff0c;北京老男孩IT教育創始人&#xff0c;17年IT經驗&#xff0c;資深Linux實戰專家&#xff0c;IT培訓界實戰派頂尖大師&#xff0c;國內將實戰心理學體系大量注入IT運維培訓領域的第一人&#xff0c;多本IT暢銷圖書作者&#xff0c;51CTO金…

Webservice soap wsdl區別之個人見解

Web Service實現業務訴求&#xff1a;Web Service是真正“辦事”的那個&#xff0c;提供一種辦事接口的統稱。WSDL提供“能辦的事的文檔說明”&#xff1a;對要提供的服務的一種描述格式。我想幫你的忙&#xff0c;但是我要告訴你我都能干什么&#xff0c;以及干這些事情需要的…

java uuid靜態方法_Java UUID nameUUIDFromBytes()方法及示例

java uuid靜態方法UUID類名UUIDFromBytes()方法 (UUID Class nameUUIDFromBytes() method) nameUUIDFromBytes() method is available in java.util package. java.util包中提供了nameUUIDFromBytes()方法 。 nameUUIDFromBytes() method is used to get a UUID constructed fr…

清空 linux 服務器,Linux服務器清理

Why?廢話不多說直接來圖&#xff0c;可以看出磁盤已經快要滿了未清之前What?可以看出mnt文件夾占用的最大&#xff0c;然后進入mnt目錄里通過命令,根據文件大小對該路徑下文件排序du -h --max-depth1我們服務器出現磁盤快滿了的原因是因為&#xff0c;服務器部署了多個tomcat…

Git中的AutoCRLF與SafeCRLF換行符問題

2019獨角獸企業重金招聘Python工程師標準>>> 原文&#xff1a;http://www.cnblogs.com/flying_bat/archive/2013/09/16/3324769.html 最近在使用GitHub&#xff0c;發現不時沒有修改過的文件要提交&#xff0c;對比發現文件全部修改&#xff0c;但找不到不一樣的地方…

stringwriter_Java StringWriter getBuffer()方法與示例

stringwriterStringWriter類的getBuffer()方法 (StringWriter Class getBuffer() method) getBuffer() method is available in java.io package. getBuffer()方法在java.io包中可用。 getBuffer() method is used to get the StringBuffer that holds the present buffer valu…

linux 下郵件服務器,Linux 下搭建Postfix郵件服務器

Linux 下搭建Postfix郵件服務器詳解&#xff1a;1、首先關閉sendmail服務service sendmail stop2、chkconfig sendmail off(關閉開機自啟動)3、修改DNS正解文件&#xff0c;使DNS能夠解析郵箱服務添加下面兩行mail.zhubf.com. IN A 172.17.17.2zhubf.com. IN M…

Java PipedInputStream close()方法與示例

PipedInputStream類close()方法 (PipedInputStream Class close() method) close() method is available in java.io package. close()方法在java.io包中可用。 close() method is used to close this PipedInputStream and free all system resources linked with this stream…

Coreseek Windows下安裝調試

由于項目需要全文檢索&#xff0c;后面就去網上查了下資料&#xff0c;找到了Sphinx【中文是獅身人面像】這個全文檢索引擎&#xff0c;聽說挺好用的&#xff0c;不過沒有中文分詞。后面又去找了一下&#xff0c;找到了Coreseek&#xff0c;一款中文全文檢索/搜索軟件。 一、Sp…

linux sudo命令全稱,linux sudo命令的概念與使用

1.sudo介紹本文引用地址&#xff1a;http://www.eepw.com.cn/article/201610/305498.htmsudo是linux下常用的允許普通用戶使用超級用戶權限的工具&#xff0c;允許系統管理員讓普通用戶執行一些或者全部的root命令&#xff0c;如halt&#xff0c;reboot&#xff0c;su等等。這樣…

java 方法 示例_Java語言環境getISOCountries()方法與示例

java 方法 示例區域設置類getISOCountries()方法 (Locale Class getISOCountries() method) getISOCountries() method is available in java.util package. getISOCountries()方法在java.util包中可用。 getISOCountries() method is used to return an array of string that …

android shape.xml 屬性詳解

轉載源:http://blog.csdn.net/harvic880925/article/details/41850723 一、簡單使用 剛開始&#xff0c;就先不講一堆標簽的意義及用法&#xff0c;先簡單看看shape標簽怎么用。 1、新建shape文件 首先在res/drawable文件夾下&#xff0c;新建一個文件&#xff0c;命名為&#…

linux檢查防火墻是否阻擋端口,淺析linux查看防火墻狀態和對外開放的端口狀態...

1.查看防火墻狀態查看防火墻狀態 systemctl status firewalld開啟防火墻 systemctl start firewalld關閉防火墻 systemctl stop firewalld開啟防火墻 service firewalld start若遇到無法開啟先用&#xff1a;systemctl unmask firewalld.service然后&#xff1a;systemctl star…

Java類class getClasses()方法及示例

類的類getClasses()方法 (Class class getClasses() method) getClasses() method is available in java.lang package. getClasses()方法在java.lang包中可用。 getClasses() method is used to return an array that contains Class objects denoting all the public classes…

linux內核計數函數,linux中的內核引用計數器

linux中的內核引用計數器文檔 /Documentation/kref.txt翻譯。krefs能讓你往你的對象中添加一個引用計數器。如果你有一些需要在多處被使用和傳遞的對象&#xff0c;而你并沒有給這些對象中添加引用計數器的話&#xff0c;你的代碼肯定會有某些缺陷&#xff0c;會出現一些問題。…

jQuery常用的全局方法源碼

下面常用方法的詳細使用請查看&#xff1a;http://www.cnblogs.com/moqiutao/p/4775725.html 1.$.noConflict()方法 語法&#xff1a;jQuery.noConflict(removeAll) removeAll&#xff1a;布爾值。指示是否允許徹底將 jQuery 變量還原。 源碼&#xff1a; var// Map over jQuer…

isinstance_Java類class isInstance()方法及示例

isinstance類class isInstance()方法 (Class class isInstance() method) isInstance() method is available in java.lang package. isInstance()方法在java.lang包中可用。 isInstance() method is used to check whether the given object is an instance with the object d…

Linux比較大文件內容,Linux系統最大文件打開數優化,解決Too many open files報錯

這是一個Linux系統常見的故障&#xff0c;網絡上也能輕易的找到解決辦法&#xff0c;我也只是在工作中遇到了這個問題&#xff0c;所以在博客記錄下&#xff0c;以備不時之需。一、報錯截圖&#xff1a;圖為resin的報錯日志&#xff0c;很明顯提示了Too many open files&#x…