010-shiro integrates with Spring Web project "quad" cache Ehcache, Redis

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.