shiro-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Richard Bradley <Richard.Brad...@softwire.com>
Subject RE: Get list of all logged users from Apache Shiro
Date Tue, 10 May 2016 14:06:15 GMT
If you are using in-memory sessions or EHCache, then DefaultSessionManager.getActiveSessions()
should work. It's a "protected" method which is designed for use by the stale session sweeper
thread.

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.DefaultSessionManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;

        DefaultSecurityManager manager = (DefaultSecurityManager) SecurityUtils.getSecurityManager();
        DefaultWebSessionManager sessionManager = (DefaultWebSessionManager) manager.getSessionManager();
        // invoke "sessionManager.getActiveSessions()" via reflection:
        Method getActiveSessionsMethod = DefaultSessionManager.class.getDeclaredMethod("getActiveSessions");
        getActiveSessionsMethod.setAccessible(true);
        Collection<Session> activeSessions = (Collection<Session>) getActiveSessionsMethod.invoke(sessionManager);

        return activeSessions.toString();


If you have a more complicated setup, then you need to have a look at the implementation of
your SessionDAO and adjust the above code accordingly.
(The default setup should work with the above code; I think you can remove the cache you added
in your email below.)

GL


Rich


-----Original Message-----
From: Peter Penzov [mailto:peter.penzov@gmail.com]
Sent: 10 May 2016 11:07
To: dev@shiro.apache.org
Subject: Re: Get list of all logged users from Apache Shiro

Thanks, I added

cacheManager = org.apache.shiro.cache.MemoryConstrainedCacheManager
securityManager.cacheManager = $cacheManager

How I can get the sessions using Java. Can you show me some Java code sample, please?



On Tue, May 10, 2016 at 12:56 PM, Thibault TIGEON <thibault.tigeon@gmail.com
> wrote:

> You can find the documentation concerning the cache here :
> http://shiro.apache.org/caching.html
>
> Rgds,
>
> Thibault
>
> 2016-05-10 11:33 GMT+02:00 Peter Penzov <peter.penzov@gmail.com>:
>
> > Hi Darin,
> >    Thank you for the response. I use this shiro.ini configuration:
> >
> > [main]
> > shiro.loginUrl = /authentication/login.xhtml dataSource =
> > org.apache.shiro.jndi.JndiObjectFactory
> > dataSource.resourceName = jdbc/DefaultDB dataSource.resourceRef =
> > true jdbcRealm = com.crm.web.authentication.JdbcRealm
> > jdbcRealm.dataSource = $dataSource
> > jdbcRealm.permissionsLookupEnabled = true securityManager.realm =
> > $jdbcRealm passwordMatcher =
> > org.apache.shiro.authc.credential.Sha256CredentialsMatcher
> > credentialsMatcher =
> > org.apache.shiro.authc.credential.HashedCredentialsMatcher
> > credentialsMatcher.hashAlgorithmName = SHA-256
> > credentialsMatcher.storedCredentialsHexEncoded = true
> > credentialsMatcher.hashIterations = 5000 multipleroles =
> com.crm.web.authentication.MultipleRolesAuthorizationFilter
> >
> > [urls]
> > /authentication/login.xhtml = authc
> > /authentication/passwordreset.xhtml = anon
> > /javax.faces.resource/** = anon
> > /** = authc
> >
> > How I can add cache?
> >
> > On Tue, May 10, 2016 at 12:18 PM, Darin Gordon <darinc@gmail.com> wrote:
> >
> > > If you're using a cache, you could get active sessions from it ,
> > > deserialize each session, and find those that have the " is
> > authenticated "
> > > flag set.  Authenticated sessions will have user identification in
> them,
> > > too.
> > > On May 10, 2016 2:26 AM, "Peter Penzov" <peter.penzov@gmail.com>
> wrote:
> > >
> > > > Hi All,
> > > >    How I can get all logged in users as a list in Apache Shiro?
> > > >
> > > > Can you give me some example?
> > > >
> > >
> >
>
Richard Bradley
Tel : 020 7485 7500 ext 3230 | Fax : 020 7485 7575

softwire
Sunday Times Best Small Companies - UK top 25 six years running
Web : www.softwire.com<http://www.softwire.com/> | Follow us on Twitter : @SoftwireUK<https://twitter.com/SoftwireUK>
Addr : 110 Highgate Studios, 53-79 Highgate Road, London NW5 1TL
Softwire Technology Limited. Registered in England no. 3824658. Registered Office : Gallery
Court, 28 Arcadia Avenue, Finchley, London. N3 2FG
Mime
View raw message