You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@shiro.apache.org by Karsten Priegnitz <ko...@petoria.de> on 2015/02/21 08:32:47 UTC

how to clear cache objects for certain user

Hello,

I want to clear the cached data for a certain user. All I have in my 
hand is the username, which is not the name of the user which is logged 
in. No Principle objects, Realms and so on. What I tried is this:

         for (Realm realm : realms) {
             if (!(realm instanceof AuthorizingRealm)) {
                 continue;
             }

             final org.apache.shiro.cache.Cache<Object, 
AuthorizationInfo> zcache = ((AuthorizingRealm) 
realm).getAuthorizationCache();

             LOG.debug("cached credentials found for {}", realm.getName());

             for (final Object key : zcache.keys()) { // zcache.keys() 
is always empty
                 final PrincipalCollection principals = 
(PrincipalCollection) key;

                 // what to do now?
                 // I want to delete everything for String username
             }
         }

But I even don't get to the "what to do now" line, because zcache.keys() 
is always empty.

I'm using shiro 1.2.3 and ehcache with standard config.

Best,
Karsten

Re: how to clear cache objects for certain user

Posted by koem <ko...@petoria.de>.
Ok, I found out that logging out seems to do a little more than my code. When
I call logout in the onFailure method it works better than my code (I have a
counter of failed login attempts that must be re-read but isn't without
logging out).



--
View this message in context: http://shiro-user.582556.n2.nabble.com/how-to-clear-cache-objects-for-certain-user-tp7580454p7580502.html
Sent from the Shiro User mailing list archive at Nabble.com.

Re: how to clear cache objects for certain user

Posted by Lenny Primak <lp...@hope.nyc.ny.us>.
onLogout() gets called when user logs out.  When called directly it clears the cache. When called directly the user is not necessarily logged out. 



> On Feb 22, 2015, at 7:06 PM, koem <ko...@petoria.de> wrote:
> 
> But onLogout indicates, that caches are cleared when logging out only. 
> My code does it without.
> 
> 
>> Am 22.02.2015 um 23:49 schrieb lprimak [via Shiro User]:
>> Your code does exactly the same as mine but with much greater lines of 
>> code
>> If you look at what onLogout does it does the cache clearing.
> 
> 
> 
> 
> 
> --
> View this message in context: http://shiro-user.582556.n2.nabble.com/how-to-clear-cache-objects-for-certain-user-tp7580454p7580461.html
> Sent from the Shiro User mailing list archive at Nabble.com.
> 

Re: how to clear cache objects for certain user

Posted by koem <ko...@petoria.de>.
But onLogout indicates, that caches are cleared when logging out only. 
My code does it without.


Am 22.02.2015 um 23:49 schrieb lprimak [via Shiro User]:
> Your code does exactly the same as mine but with much greater lines of 
> code
> If you look at what onLogout does it does the cache clearing.





--
View this message in context: http://shiro-user.582556.n2.nabble.com/how-to-clear-cache-objects-for-certain-user-tp7580454p7580461.html
Sent from the Shiro User mailing list archive at Nabble.com.

Re: how to clear cache objects for certain user

Posted by Lenny Primak <lp...@hope.nyc.ny.us>.
Your code does exactly the same as mine but with much greater lines of code  
If you look at what onLogout does it does the cache clearing. 

> On Feb 22, 2015, at 4:31 AM, koem <ko...@petoria.de> wrote:
> 
> Thanks for that answer. Though it didn't really prove to be useful to me  it
> made me look deeper into the AuthorizingRealm and AuthenticatingRealm
> classes.
> 
> This here works without logging out:
> 
>        LOG.trace("called: clearShiroCacheFor('{}')", usernames);
> 
>        if (usernames == null) {
>            return;
>        }
> 
>        Object[][] oousers = ca.oltis.eweb.services.rest.Helper.splitUpIds(
>                usernames, String.class);
> 
>        RealmSecurityManager mgr = (RealmSecurityManager) SecurityUtils
>                .getSecurityManager();
> 
>        Collection<Realm> realmCollection = mgr.getRealms();
>        for (Realm realm : realmCollection) {
>            if (realm instanceof AuthorizingRealm) {
>                for (Object[] ouser : oousers) {
>                    SimplePrincipalCollection spc = new
> SimplePrincipalCollection();
>                    spc.add((String) ouser[0], realm.getName());
> 
>                    AuthorizingRealm authRealm = (AuthorizingRealm) realm;
>                    authRealm.getAuthenticationCache().remove(spc);
>                    authRealm.getAuthorizationCache().remove(spc);
>                }
> 
>                LOG.debug("cleared caches in realm '{}'", realm.getName());
>            }
>        }
> 
> 
> 
> 
> --
> View this message in context: http://shiro-user.582556.n2.nabble.com/how-to-clear-cache-objects-for-certain-user-tp7580454p7580456.html
> Sent from the Shiro User mailing list archive at Nabble.com.
> 

Re: how to clear cache objects for certain user

Posted by koem <ko...@petoria.de>.
Thanks for that answer. Though it didn't really prove to be useful to me  it
made me look deeper into the AuthorizingRealm and AuthenticatingRealm
classes.

This here works without logging out:

        LOG.trace("called: clearShiroCacheFor('{}')", usernames);

        if (usernames == null) {
            return;
        }

        Object[][] oousers = ca.oltis.eweb.services.rest.Helper.splitUpIds(
                usernames, String.class);

        RealmSecurityManager mgr = (RealmSecurityManager) SecurityUtils
                .getSecurityManager();

        Collection<Realm> realmCollection = mgr.getRealms();
        for (Realm realm : realmCollection) {
            if (realm instanceof AuthorizingRealm) {
                for (Object[] ouser : oousers) {
                    SimplePrincipalCollection spc = new
SimplePrincipalCollection();
                    spc.add((String) ouser[0], realm.getName());

                    AuthorizingRealm authRealm = (AuthorizingRealm) realm;
                    authRealm.getAuthenticationCache().remove(spc);
                    authRealm.getAuthorizationCache().remove(spc);
                }

                LOG.debug("cleared caches in realm '{}'", realm.getName());
            }
        }




--
View this message in context: http://shiro-user.582556.n2.nabble.com/how-to-clear-cache-objects-for-certain-user-tp7580454p7580456.html
Sent from the Shiro User mailing list archive at Nabble.com.

Re: how to clear cache objects for certain user

Posted by Lenny Primak <lp...@hope.nyc.ny.us>.
here is an example where UserAuth is a principal type for this realm
    
    /**
     * removed local cached authorization info in all realms from a particular user
     * @param id 
     */
    public void removeUserFromLocalCache(int id)
    {
        UserAuth auth = new UserAuth(id, true);
        realms.values().stream().forEach(realm -> realm.onLogout(new SimplePrincipalCollection(auth, 
                realm.getName())));
    }
    
    

> On Feb 21, 2015, at 2:32 AM, Karsten Priegnitz <ko...@petoria.de> wrote:
> 
> Hello,
> 
> I want to clear the cached data for a certain user. All I have in my hand is the username, which is not the name of the user which is logged in. No Principle objects, Realms and so on. What I tried is this:
> 
>        for (Realm realm : realms) {
>            if (!(realm instanceof AuthorizingRealm)) {
>                continue;
>            }
> 
>            final org.apache.shiro.cache.Cache<Object, AuthorizationInfo> zcache = ((AuthorizingRealm) realm).getAuthorizationCache();
> 
>            LOG.debug("cached credentials found for {}", realm.getName());
> 
>            for (final Object key : zcache.keys()) { // zcache.keys() is always empty
>                final PrincipalCollection principals = (PrincipalCollection) key;
> 
>                // what to do now?
>                // I want to delete everything for String username
>            }
>        }
> 
> But I even don't get to the "what to do now" line, because zcache.keys() is always empty.
> 
> I'm using shiro 1.2.3 and ehcache with standard config.
> 
> Best,
> Karsten
>