<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Acegi Concurrent Login</title>
	<atom:link href="http://www.cakesolutions.net/teamblogs/2008/05/08/acegi-concurrent-login/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cakesolutions.net/teamblogs/2008/05/08/acegi-concurrent-login/</link>
	<description>void magic() { }</description>
	<pubDate>Fri, 21 Nov 2008 12:01:20 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Aleksa Vukotic</title>
		<link>http://www.cakesolutions.net/teamblogs/2008/05/08/acegi-concurrent-login/#comment-43</link>
		<dc:creator>Aleksa Vukotic</dc:creator>
		<pubDate>Sun, 01 Jun 2008 18:38:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.cakesolutions.net/teamblogs/2008/05/08/acegi-concurrent-login/#comment-43</guid>
		<description>Yes, you should have your own &lt;code&gt;UserContext&lt;/code&gt; implementation.
Following implementation has only two methods, &lt;code&gt;User getUser()&lt;/code&gt;, which gets the user currently logged in,  and &lt;code&gt;void logout()&lt;/code&gt;, which destroys the session for user currently logged in.
Here is the &lt;code&gt;UserContext&lt;/code&gt; interface, and sample implementation:
&lt;code&gt;
public interface UserContext {

	/**
	 * Gets the current user
	 * @return The User object identifying the user
	 */
	User getUser();
    
    /**
	 * Performs logout for current user
	 */
    void logout();
}
&lt;/code&gt;
&lt;code&gt;
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;
    }
}
&lt;/code&gt;
Finally, you should  create bean definition for userContext bean:
&lt;code&gt;
&#60;bean id="userContext" class="net.cakesolutions.service.security.acegi.AcegiUserContext"&#62;
        &#60;property name="userService" ref="userService"/&#62;
        &#60;property name="sessionRegistry" ref="sessionRegistry" /&#62;
&#60;/bean&#62;
&lt;/code&gt;

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.</description>
		<content:encoded><![CDATA[<p>Yes, you should have your own <code>UserContext</code> implementation.<br />
Following implementation has only two methods, <code>User getUser()</code>, which gets the user currently logged in,  and <code>void logout()</code>, which destroys the session for user currently logged in.<br />
Here is the <code>UserContext</code> interface, and sample implementation:<br />
<code><br />
public interface UserContext {</p>
<p>	/**<br />
	 * Gets the current user<br />
	 * @return The User object identifying the user<br />
	 */<br />
	User getUser();</p>
<p>    /**<br />
	 * Performs logout for current user<br />
	 */<br />
    void logout();<br />
}<br />
</code><br />
<code><br />
public class AcegiUserContext implements UserContext, InitializingBean {<br />
    private static final Log logger = LogFactory.getLog(AcegiUserDetails.class);</p>
<p>    private UserService userService;<br />
    private SessionRegistry sessionRegistry;<br />
    public User getUser() {<br />
        SecurityContext context = SecurityContextHolder.getContext();<br />
        if (context == null) return null;<br />
        Authentication authentication = context.getAuthentication();<br />
        if (authentication == null) return null;</p>
<p>        String username = authentication.getPrincipal().toString();</p>
<p>        if (authentication.getPrincipal() instanceof UserDetails) {<br />
            username = ((UserDetails) authentication.getPrincipal()).getUsername();<br />
        }</p>
<p>        return this.userService.findByUsername(username);<br />
    }</p>
<p>    public void logout() {<br />
        SecurityContext context = SecurityContextHolder.getContext();<br />
        if (context == null) return;<br />
        Authentication authentication = context.getAuthentication();<br />
        if (authentication == null) return;<br />
        String sessionId = SessionRegistryUtils.obtainSessionIdFromAuthentication(authentication);<br />
        this.sessionRegistry.removeSessionInformation(sessionId);</p>
<p>    }</p>
<p>    public void afterPropertiesSet() throws Exception {<br />
        if (this.userService == null) throw new FatalBeanException("Property [userService] of [" + getClass().getName() + "] is required.&#8221;);<br />
        if (this.sessionRegistry == null) throw new FatalBeanException(&#8221;Property [sessionRegistry] of [" + getClass().getName() + "] is required.&#8221;);<br />
    }</p>
<p>    public void setUserService(UserService userService) {<br />
        this.userService = userService;<br />
    }</p>
<p>    public void setSessionRegistry(SessionRegistry sessionRegistry) {<br />
        this.sessionRegistry = sessionRegistry;<br />
    }<br />
}<br />
</code><br />
Finally, you should  create bean definition for userContext bean:<br />
<code><br />
&lt;bean id="userContext" class="net.cakesolutions.service.security.acegi.AcegiUserContext"&gt;<br />
        &lt;property name="userService" ref="userService"/&gt;<br />
        &lt;property name="sessionRegistry" ref="sessionRegistry" /&gt;<br />
&lt;/bean&gt;<br />
</code></p>
<p>SessionRegistry is the the reference to the implementation shown in the blog, and the userService is s standard service for use management.</p>
<p>     .</p>
<p>Hope this helps, let us know how are you getting on.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lady Raveneve</title>
		<link>http://www.cakesolutions.net/teamblogs/2008/05/08/acegi-concurrent-login/#comment-42</link>
		<dc:creator>Lady Raveneve</dc:creator>
		<pubDate>Fri, 30 May 2008 07:17:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.cakesolutions.net/teamblogs/2008/05/08/acegi-concurrent-login/#comment-42</guid>
		<description>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 !</description>
		<content:encoded><![CDATA[<p>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? </p>
<p>Your help is greatly appreciated as my app intermittently throws ConcurrentLoginException even though the user has logged out of the system.</p>
<p>I am using JDK1.4 and acegi-security 1..0.4. Thanks !</p>
]]></content:encoded>
	</item>
</channel>
</rss>
