First, Ehcache
Shiro each authorization will be through the realm to obtain permission information, in order to improve access speed need to add cache, first read the permission data from realm, and then no longer read, here Shiro and Ehcache integration.
1. Add Ehcache jar Package
<Dependency> <groupId>Net.sf.ehcache</groupId> <Artifactid>Ehcache-core</Artifactid> <version>2.5.3</version> </Dependency> <Dependency> <groupId>Org.apache.shiro</groupId> <Artifactid>Shiro-ehcache</Artifactid> <version>1.3.2</version> </Dependency>
View Code2, Configuration CacheManager
Configure the cache manager in Applicationcontext-shiro.xml.
<!--SecurityManager Security Manager - <BeanID= "SecurityManager"class= "Org.apache.shiro.web.mgt.DefaultWebSecurityManager"> < Propertyname= "Realm"ref= "Customrealm"/> <!--Inject cache manager - < Propertyname= "CacheManager"ref= "CacheManager"/> <!--Inject Session Manager - < Propertyname= "SessionManager"ref= "SessionManager"/> <!--Remember me - < Propertyname= "Remembermemanager"ref= "Remembermemanager"/> </Bean> <!--Cache Manager - <BeanID= "CacheManager"class= "Org.apache.shiro.cache.ehcache.EhCacheManager"> < Propertyname= "Cachemanagerconfigfile"value= "Classpath:shiro-ehcache.xml"/> </Bean>
3, Configuration Shiro-ehcache.xml
<EhcacheXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:nonamespaceschemalocation=".. /config/ehcache.xsd "> <!--diskstore: Directory address for cache data Persistence - <DiskstorePath= "F:\develop\ehcache" /> <Defaultcachemaxelementsinmemory= "+"Maxelementsondisk= "10000000"Eternal= "false"Overflowtodisk= "false"diskpersistent= "false"Timetoidleseconds= "+"Timetoliveseconds= "+"Diskexpirythreadintervalseconds= "+"Memorystoreevictionpolicy= "LRU"> </Defaultcache></Ehcache>
View Code4. Empty the cache
When user rights are modified, user login Shiro will automatically call realm to get permission data from the database, if you want to clear the cache immediately after modifying the permissions, you can call realm's ClearCache method to clear the cache.
Realm defines the Clearcached method:
// Clear Cache Public void clearcached () { = securityutils.getsubject (). Getprincipals (); Super . ClearCache (principals); }
After the permissions have been modified to invoke the Realm method, realm has been managed by spring, so get the realm instance from spring and call the Clearcached method.
010-shiro integrates with Spring Web project "quad" cache Ehcache, Redis