7 Responses to Acegi Concurrent Login

  1. Lady Raveneve says:

    Hi ! I have tried to implement your MyHttpSessionEventPublisher but am having some problems with the declaration of UserContext. May I know which package this class is suppose to be from? Am i suppose to implement my own UserContext?

    Your help is greatly appreciated as my app intermittently throws ConcurrentLoginException even though the user has logged out of the system.

    I am using JDK1.4 and acegi-security 1..0.4. Thanks !

  2. Aleksa Vukotic says:

    Yes, you should have your own UserContext implementation.
    Following implementation has only two methods, User getUser(), which gets the user currently logged in, and void logout(), which destroys the session for user currently logged in.
    Here is the UserContext interface, and sample implementation:

    public interface UserContext {

    /**
    * Gets the current user
    * @return The User object identifying the user
    */
    User getUser();

    /**
    * Performs logout for current user
    */
    void logout();
    }


    public class AcegiUserContext implements UserContext, InitializingBean {
    private static final Log logger = LogFactory.getLog(AcegiUserDetails.class);

    private UserService userService;
    private SessionRegistry sessionRegistry;
    public User getUser() {
    SecurityContext context = SecurityContextHolder.getContext();
    if (context == null) return null;
    Authentication authentication = context.getAuthentication();
    if (authentication == null) return null;

    String username = authentication.getPrincipal().toString();

    if (authentication.getPrincipal() instanceof UserDetails) {
    username = ((UserDetails) authentication.getPrincipal()).getUsername();
    }

    return this.userService.findByUsername(username);
    }

    public void logout() {
    SecurityContext context = SecurityContextHolder.getContext();
    if (context == null) return;
    Authentication authentication = context.getAuthentication();
    if (authentication == null) return;
    String sessionId = SessionRegistryUtils.obtainSessionIdFromAuthentication(authentication);
    this.sessionRegistry.removeSessionInformation(sessionId);

    }

    public void afterPropertiesSet() throws Exception {
    if (this.userService == null) throw new FatalBeanException("Property [userService] of [" + getClass().getName() + "] is required.");
    if (this.sessionRegistry == null) throw new FatalBeanException("Property [sessionRegistry] of [" + getClass().getName() + "] is required.");
    }

    public void setUserService(UserService userService) {
    this.userService = userService;
    }

    public void setSessionRegistry(SessionRegistry sessionRegistry) {
    this.sessionRegistry = sessionRegistry;
    }
    }

    Finally, you should create bean definition for userContext bean:

    <bean id="userContext" class="net.cakesolutions.service.security.acegi.AcegiUserContext">
    <property name="userService" ref="userService"/>
    <property name="sessionRegistry" ref="sessionRegistry" />
    </bean>

    SessionRegistry is the the reference to the implementation shown in the blog, and the userService is s standard service for use management.

    .

    Hope this helps, let us know how are you getting on.

  3. tmjorud says:

    Nice example, thanks you :)

    But, what happens if a user don’t click the logout button and just closes the browser. Will the session be removed from the registry?

  4. Ramesh says:

    Hi,
    Can you send me the source code of this to my email ID?

  5. Aleksa Vukotic says:

    tmjorud,

    unfortunately no it won’t. Session will remain in the registry until it expires. There is no way to actually register browser closing (except by using javascript – and even that won’t solve browser crash situation).
    Best advice when dealing with concurrent login is to pay close attention to session timeout, and using the lowest timeout possible for your scenario.

  6. Morgan Stanley says:

    Hi,
    How do we handle concurrent logins from the same browser, different tabs? The above does not work at all in this scenario :(

  7. WOW GOLD says:

    This isn’t the exact info I was searching for but your post came up as the first choice in GooGle and I’m glad I took the time to check it out…I’ll be back A~

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>