javascript
(39.3) Spring Boot Shiro权限管理【从零开始学Spring Boot】
在學(xué)習(xí)此小節(jié)之前您可能還需要學(xué)習(xí):
?
?
(39.1) Spring Boot Shiro權(quán)限管理【從零開始學(xué)Spring Boot】
http://412887952-qq-com.iteye.com/blog/2299732
(39.2). Spring Boot Shiro權(quán)限管理【從零開始學(xué)Spring Boot】
http://412887952-qq-com.iteye.com/blog/2299777
?
?
????? 相對于上一小節(jié)這個就比較簡單了。
主要分這么幾個步驟:在pom.xml中加入緩存依賴;注入緩存;
(a) 在pom.xml文件中加入依賴:
<!-- shiro ehcache -->
?????? <dependency>
?????????? <groupId>org.apache.shiro</groupId>
?????????? <artifactId>shiro-ehcache</artifactId>
?????????? <version>1.2.2</version>
?????? </dependency>
??????
?????? <!--
?????????? 包含支持UI模版(Velocity,FreeMarker,JasperReports),
?????????? 郵件服務(wù),
?????????? 腳本服務(wù)(JRuby),
?????????? 緩存Cache(EHCache),
?????????? 任務(wù)計劃Scheduling(uartz)。
?????? ?-->
?????? <dependency>
??? ????? <groupId>org.springframework</groupId>
??? ????? <artifactId>spring-context-support</artifactId>
??? ??? </dependency>
?
(b)注入緩存
在com.kfit.config.shiro.ShiroConfiguration中加入如下方法:
/**
??? ?* shiro緩存管理器;
??? ?* 需要注入對應(yīng)的其它的實(shí)體類中:
??? ?* 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");
?????? returncacheManager;
??? }
?
將緩存對象注入到SecurityManager中:
@Bean
??? public SecurityManager securityManager(){
?????? DefaultWebSecurityManager securityManager =? new DefaultWebSecurityManager();
?????? //設(shè)置realm.
?????? securityManager.setRealm(myShiroRealm());
??????
?????? //注入緩存管理器;
?????? securityManager.setCacheManager(ehCacheManager());//這個如果執(zhí)行多次,也是同樣的一個對象;
??????
?????? returnsecurityManager;
??? }
?
(c)添加緩存配置文件:
在src/main/resouces/config添加ehcache-shiro.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="es">
?
??? <diskStore path="java.io.tmpdir"/>
???
??? <!--
?????? name:緩存名稱。
?????? maxElementsInMemory:緩存最大數(shù)目
?????? maxElementsOnDisk:硬盤最大緩存?zhèn)€數(shù)。?
?????? eternal:對象是否永久有效,一但設(shè)置了,timeout將不起作用。?
?????? overflowToDisk:是否保存到磁盤,當(dāng)系統(tǒng)當(dāng)機(jī)時
?????? timeToIdleSeconds:設(shè)置對象在失效前的允許閑置時間(單位:秒)。僅當(dāng)eternal=false對象不是永久有效時使用,可選屬性,默認(rèn)值是0,也就是可閑置時間無窮大。
?????? timeToLiveSeconds:設(shè)置對象在失效前允許存活時間(單位:秒)。最大時間介于創(chuàng)建時間和失效時間之間。僅當(dāng)eternal=false對象不是永久有效時使用,默認(rèn)是0.,也就是對象存活時間無窮大。
?????? diskPersistent:是否緩存虛擬機(jī)重啟期數(shù)據(jù) Whether the disk store persists between restarts of the Virtual Machine. The default value is false.?
?????? diskSpoolBufferSizeMB:這個參數(shù)設(shè)置DiskStore(磁盤緩存)的緩存區(qū)大小。默認(rèn)是30MB。每個Cache都應(yīng)該有自己的一個緩沖區(qū)。?
?????? diskExpiryThreadIntervalSeconds:磁盤失效線程運(yùn)行時間間隔,默認(rèn)是120秒。
?????? memoryStoreEvictionPolicy:當(dāng)達(dá)到maxElementsInMemory限制時,Ehcache將會根據(jù)指定的策略去清理內(nèi)存。默認(rèn)策略是LRU(最近最少使用)。你可以設(shè)置為FIFO(先進(jìn)先出)或是LFU(較少使用)。?
??????? clearOnFlush:內(nèi)存數(shù)量最大時是否清除。
???????? memoryStoreEvictionPolicy:
???????? ?? Ehcache的三種清空策略;
???????? ?? FIFO,first in first out,這個是大家最熟的,先進(jìn)先出。
???????? ?? LFU, Less Frequently Used,就是上面例子中使用的策略,直白一點(diǎn)就是講一直以來最少被使用的。如上面所講,緩存的元素有一個hit屬性,hit值最小的將會被清出緩存。
???????? ?? LRU,Least Recently Used,最近最少使用的,緩存的元素有一個時間戳,當(dāng)緩存容量滿了,而又需要騰出地方來緩存新的元素的時候,那么現(xiàn)有緩存元素中時間戳離當(dāng)前時間最遠(yuǎn)的元素將被清出緩存。
??? -->
???? <defaultCache
??????????? maxElementsInMemory="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>
在配置文件上已經(jīng)有很詳細(xì)的解釋了,所以這里就過多介紹ehcache的配置了。
運(yùn)行程序訪問:http://127.0.0.1:8080/userInfo/userAdd
查看控制臺的打印信息:
權(quán)限配置-->MyShiroRealm.doGetAuthorizationInfo()
這個信息就只打印一次了,說明我們的緩存生效了。
?
?
?【Spring Boot?系列博客】
54. spring boot日志升級篇—logback【從零開始學(xué)Spring Boot】
?
52. spring boot日志升級篇—log4j多環(huán)境不同日志級別的控制【從零開始學(xué)Spring Boot】?
?
51. spring boot屬性文件之多環(huán)境配置【從零開始學(xué)Spring Boot】
?
50. Spring Boot日志升級篇—log4j【從零開始學(xué)Spring Boot】
?
49. spring boot日志升級篇—理論【從零開始學(xué)Spring Boot】
?
48. spring boot單元測試restfull API【從零開始學(xué)Spring Boot】
?
47. Spring Boot發(fā)送郵件【從零開始學(xué)Spring Boot】
?
46. Spring Boot中使用AOP統(tǒng)一處理Web請求日志
?
45. Spring Boot MyBatis連接Mysql數(shù)據(jù)庫【從零開始學(xué)Spring Boot】
?
44. Spring Boot日志記錄SLF4J【從零開始學(xué)Spring Boot】
?
43. Spring Boot動態(tài)數(shù)據(jù)源(多數(shù)據(jù)源自動切換)【從零開始學(xué)Spring Boot】
?
42. Spring Boot多數(shù)據(jù)源【從零開始學(xué)Spring Boot】
?
41. Spring Boot?使用Java代碼創(chuàng)建Bean并注冊到Spring中【從零開始學(xué)Spring Boot】
?
40. springboot + devtools(熱部署)【從零開始學(xué)Spring Boot】?
?
39.4 Spring Boot Shiro權(quán)限管理【從零開始學(xué)Spring Boot】
?
39.3 Spring Boot Shiro權(quán)限管理【從零開始學(xué)Spring Boot】
?
39.2. Spring Boot Shiro權(quán)限管理【從零開始學(xué)Spring Boot】
?
39.1 Spring Boot Shiro權(quán)限管理【從零開始學(xué)Spring Boot】
?
38 Spring Boot分布式Session狀態(tài)保存Redis【從零開始學(xué)Spring Boot】?
?
37 Spring Boot集成EHCache實(shí)現(xiàn)緩存機(jī)制【從零開始學(xué)Spring Boot】?
?
36 Spring Boot Cache理論篇【從零開始學(xué)Spring Boot】
?
35 Spring Boot集成Redis實(shí)現(xiàn)緩存機(jī)制【從零開始學(xué)Spring Boot】?
?
34Spring Boot的啟動器Starter詳解【從零開始學(xué)Spring Boot】
?
33 Spring Boot?監(jiān)控和管理生產(chǎn)環(huán)境【從零開始學(xué)Spring Boot】
?
32 Spring Boot使用@SpringBootApplication注解【從零開始學(xué)Spring Boot】?
?
31 Spring Boot導(dǎo)入XML配置【從零開始學(xué)Spring Boot】
?
?
?
更多查看博客:?http://412887952-qq-com.iteye.com/
?
轉(zhuǎn)載于:https://www.cnblogs.com/hehehaha/p/6147101.html
總結(jié)
以上是生活随笔為你收集整理的(39.3) Spring Boot Shiro权限管理【从零开始学Spring Boot】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 程序开发的艺术
- 下一篇: JQuery全选/反选设置