掲示板

Logout and redirect from filter

11年前 に Anil KC によって更新されました。

Logout and redirect from filter

Junior Member 投稿: 35 参加年月日: 12/12/21 最新の投稿
I have a certain service pre action filter which needs user to redirect to a page after user is logged out programatically. Is there any way to do this? How to logout user and redirect to a page?
thumbnail
11年前 に mohammad azaruddin によって更新されました。

RE: Logout and redirect from filter

Expert 投稿: 492 参加年月日: 12/09/17 最新の投稿
Hi
To logout programatically use below sniplet

ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
themeDisplay.getURLSignOut();


For redirect try this
11年前 に Anil KC によって更新されました。

RE: Logout and redirect from filter

Junior Member 投稿: 35 参加年月日: 12/12/21 最新の投稿
It just returns Logout URL. How to logout and then redirect programatically in a method?
thumbnail
11年前 に mohammad azaruddin によって更新されました。

RE: Logout and redirect from filter

Expert 投稿: 492 参加年月日: 12/09/17 最新の投稿
Hi
To logout programatically use below sniplet(in your action class)

ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
actionResponse.sendRedirect(themeDisplay.getURLSignOut());

And after logout u might use Post Logout Event(not sure whether it is proper way)

Regards
thumbnail
11年前 に Jitendra Rajput によって更新されました。

RE: Logout and redirect from filter

Liferay Master 投稿: 875 参加年月日: 11/01/07 最新の投稿
To logout pragmatically and redirect to custom page you can try with below approach.

Copy code from LogoutAction.java and paste in your custom method. when you want to logout pro grammatically user your custom method.
Once you are done with logout use SendRedirect as suggested in above post you can redirect to your custom page.

Below is the code for force user logout.

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);

			Cookie rememberMeCookie = new Cookie(
				CookieKeys.REMEMBER_ME, StringPool.BLANK);

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

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

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

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

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

			request.setAttribute(WebKeys.LOGOUT, true);