Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: HOWTO: Acegi Logout

  1. #1
    Join Date
    Aug 2004
    Location
    Allentown, PA
    Posts
    141

    Default HOWTO: Acegi Logout

    All,

    I have been looking for a way to issue a logout commnad with acegi. Is there something I am missing? I tried to just invalidate the session, but that doesn't seem to do it.

    Thanks in advance.

    Dan

  2. #2
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    In your controller set ContextHolder to null.

    AutoIntegrationFilter or whatever subclass of AbstractIntegrationFilter you're using will overwrite the HttpSession (or other well-known location) at the end of the web request.

  3. #3
    Join Date
    Aug 2004
    Location
    Allentown, PA
    Posts
    141

    Default

    Ben,

    Thanks, I will give it a try.

    Dan

  4. #4
    Join Date
    Aug 2004
    Location
    Columbus, OH, USA
    Posts
    133

    Default

    Do you mean to say you set the Context on the ContextHolder to null?

  5. #5
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    Yes, sorry.

  6. #6
    Join Date
    Aug 2004
    Location
    Hamburg, Germany
    Posts
    3

    Default

    I've created a HttpSessionListener that sets the Context to null on session invalidation.

    Here's it is:

    Code:
    package net.sf.acegisecurity.ui;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    
    import javax.servlet.http.HttpSessionEvent;
    import javax.servlet.http.HttpSessionListener;
    
    
    /**
     * @author  Andreas Brenk
     */
    public class AbstractIntegrationListener implements HttpSessionListener {
    
        //~ Static fields/initializers ---------------------------------------------
    
        protected static final Log logger = LogFactory.getLog(AbstractIntegrationListener.class);
    
        //~ Methods ----------------------------------------------------------------
    
        /**
         * @see  javax.servlet.http.HttpSessionListener#sessionCreated(javax.servlet.http.HttpSessionEvent)
         */
        public void sessionCreated(HttpSessionEvent se) {
        }
    
        /**
         * @see  javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
         */
        public void sessionDestroyed(HttpSessionEvent se) {
        }
    }
    and
    Code:
    package net.sf.acegisecurity.ui.webapp;
    
    import net.sf.acegisecurity.context.ContextHolder;
    import net.sf.acegisecurity.ui.AbstractIntegrationListener;
    
    import javax.servlet.http.HttpSessionEvent;
    
    
    /**
     * In web.xml:
     * 
     *   <listener&gt;
     *       <listener-class&gt;net.sf.acegisecurity.ui.webapp.HttpSessionIntegrationListener</listener-class&gt;
     *   </listener&gt;
     * 
     * @author  Andreas Brenk
     */
    public class HttpSessionIntegrationListener
        extends AbstractIntegrationListener &#123;
    
        //~ Methods ----------------------------------------------------------------
    
        public void sessionDestroyed&#40;HttpSessionEvent se&#41; &#123;
    
            if &#40;logger.isInfoEnabled&#40;&#41;&#41; &#123;
                logger.info&#40;"Removing Context from ContextHolder"&#41;;
            &#125;
    
            ContextHolder.setContext&#40;null&#41;;
        &#125;
    &#125;
    I'd be delighted if it could be included in the official release.

    Regards,
    Andreas

  7. #7
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    If AbstractIntegrationFilter is working properly, it will automatically ContextHolder.setContext(null) at the end of each request. As such what value does a HttpSessionListener add?

  8. #8
    Join Date
    Aug 2004
    Location
    Hamburg, Germany
    Posts
    3

    Default

    In 0.50 I simply called request.getSession().invalidate() during logout and everything was fine. After an upgrade to 0.51 this produced "IllegalStateException: Cannot create a session after the response has been committed". The Listener was my solution.

    This way the controller also would not be directly coupled to ContextHolder.

    But please correct me, I'm always keen to learn.

    AB

  9. #9
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    You can provide a logout function by simply invalidating the HttpSession. As the request will still end normally, the AbstractIntegrationFilter will tidy up the ContextHolder (set it to null) and the session invalidation takes care of removing the HttpSession-stored Authentication object.

    So I still can't see any reason to use a HttpSessionListener for the purpose of logout in a normal situation. Some people might need it though, it they had very special needs like tracking simultaneous logins etc.

  10. #10

    Default acegi logout

    i know this was asked a long time ago, but i think invalidating the session doesn't always work for some people. this worked for me.


    import org.acegisecurity.context.SecurityContextHolder;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.AbstractContro ller;

    public class LogoutController extends AbstractController {

    private String redirect;

    public String getRedirect() {
    return redirect;
    }

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
    HttpServletResponse response) throws Exception {

    SecurityContextHolder.getContext().setAuthenticati on(null);

    return new ModelAndView(redirect);
    }

    public void setRedirect(String redirect) {
    this.redirect = redirect;
    }

    }

Similar Threads

  1. Replies: 8
    Last Post: Mar 19th, 2008, 11:13 AM
  2. Acegi running fine. Howto add roles, ...
    By ThomasBecker in forum Security
    Replies: 9
    Last Post: Sep 16th, 2007, 08:16 AM
  3. Replies: 2
    Last Post: Aug 1st, 2005, 04:51 AM
  4. Acegi for LDAP
    By vaibhav.gandhi in forum Security
    Replies: 12
    Last Post: Jul 13th, 2005, 12:33 AM
  5. logout method
    By gmansoor in forum Security
    Replies: 6
    Last Post: May 9th, 2005, 07:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •