1 Reply Latest reply: Oct 31, 2012 10:17 AM by 920261 RSS

    how to logout the liferay in the autologin hook?

    YueZ-Oracle
      req.getSession().invalidate() does not seem working.

      is there a way to force logout the liferay programmatically?
        • 1. Re: how to logout the liferay in the autologin hook?
          920261
          Maybe session.invalidate() is not enough on its own. There are also cookies. You might look to the LogoutAction class to see how it signs out a user. Here's it's execute method. Looks like there are 3 cookies you'll want to clear in addition to calling session.invalidate(). You might not need the events processing lines, but it might be nice to use those as well so that you leave the door open for adding pre and post logout events to portal-ext.properties.

               public ActionForward execute(
                         ActionMapping mapping, ActionForm form, HttpServletRequest request,
                         HttpServletResponse response)
                    throws Exception {

                    try {
                         HttpSession session = request.getSession();

                         EventsProcessorUtil.process(
                              PropsKeys.LOGOUT_EVENTS_PRE, PropsValues.LOGOUT_EVENTS_PRE,
                              request, response);

                         String domain = CookieKeys.getDomain(request);

                         Cookie companyIdCookie = new Cookie(
                              CookieKeys.COMPANY_ID, StringPool.BLANK);

                         if (Validator.isNotNull(domain)) {
                              companyIdCookie.setDomain(domain);
                         }

                         companyIdCookie.setMaxAge(0);
                         companyIdCookie.setPath(StringPool.SLASH);

                         Cookie idCookie = new Cookie(CookieKeys.ID, StringPool.BLANK);

                         if (Validator.isNotNull(domain)) {
                              idCookie.setDomain(domain);
                         }

                         idCookie.setMaxAge(0);
                         idCookie.setPath(StringPool.SLASH);

                         Cookie passwordCookie = new Cookie(
                              CookieKeys.PASSWORD, StringPool.BLANK);

                         if (Validator.isNotNull(domain)) {
                              passwordCookie.setDomain(domain);
                         }

                         passwordCookie.setMaxAge(0);
                         passwordCookie.setPath(StringPool.SLASH);

                         CookieKeys.addCookie(request, response, companyIdCookie);
                         CookieKeys.addCookie(request, response, idCookie);
                         CookieKeys.addCookie(request, response, passwordCookie);

                         try {
                              session.invalidate();
                         }
                         catch (Exception e) {
                         }

                         EventsProcessorUtil.process(
                              PropsKeys.LOGOUT_EVENTS_POST, PropsValues.LOGOUT_EVENTS_POST,
                              request, response);

                         return mapping.findForward(ActionConstants.COMMON_REFERER);
                    }
                    catch (Exception e) {
                         PortalUtil.sendError(e, request, response);

                         return null;
                    }
               }