Ssm+apache Shiro--ehcache Cache cleanup

Source: Internet
Author: User

Using Ehache to cache, you can log on to the authentication after the successful authentication authorization and permission information, but when the user exits, the user's corresponding permissions information can not be updated in real-time, so that the user will be able to exit the system to clean up their corresponding permissions information cache.

In configuring the Ehcache cache we configured the Myshirocache and specified the name of the permission cache in the Myrealm, so the user can get the information through CacheManager and cache name when exiting, and clean up the permission information when exiting the system.

 PackageCom.copsec.railway.im.util;ImportOrg.apache.log4j.Logger;Importorg.apache.shiro.SecurityUtils;ImportOrg.apache.shiro.cache.Cache;ImportOrg.apache.shiro.cache.CacheManager;Importorg.apache.shiro.subject.SimplePrincipalCollection;ImportOrg.apache.shiro.subject.Subject; Public classShiroauthorizationhelper {    Private StaticCacheManager CacheManager; Private StaticLogger Logger = Logger.getlogger (shiroauthorizationhelper.class); /*** Clear User's permissions * *@paramprincipal principal object for landing user*/     Public Static voidClearauthorizationinfo (simpleprincipalcollection principal) {Logger.info ("Clear the User:" + principal.tostring () + "' s Authorizationinfo"); Cache<object, object> cache = Cachemanager.getcache ("Myshirocache");Cache.remove (principal); }    /*** Clear the current user's permissions*/     Public Static voidClearauthorizationinfo () {if(Securityutils.getsubject (). IsAuthenticated ()) {Subject Subject=Securityutils.getsubject (); String username=Subject.getprincipal (). toString (); String Realmname=subject.getprincipals (). Getrealmnames (). iterator (). Next (); Simpleprincipalcollection PrincipalCollection=Newsimpleprincipalcollection (username, realmname); Logger.debug ("Get user principalcollection:" +principalcollection); //invoke cleanup user rightsClearauthorizationinfo (principalcollection); }    }    /*** The object is injected by the spring Bean *@paramCacheManager*/     Public Static voidSetcachemanager (CacheManager cachemanager) {Shiroauthorizationhelper.cachemanager=CacheManager; }}

After completion, it can be called in the logout controller for cleanup permission information.

Ssm+apache Shiro--ehcache Cache cleanup

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.