After my AuthenticationFilter
redirect to login page, I would like to logout to user.
That's why, I put identity.logout();
in my pre-render method checkPermission(...)
of login.xhtml
.
But, I get the ViewExpiredException
when the user login again.
My problem is
1 : If I don't do identity.logout();
, the user relogin again because of the old user session still exist.
2 : If I do identity.logout();
, I get the ViewExpiredException
when the user login again.
AuthenticationFilter.java
public class AuthenticationFilter implements Filter {
.....
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
HttpSession session = httpRequest.getSession();
User user = (User) session.getAttribute(Constants.LOGIN_USER);
if (user == null) {
session.setAttribute(Constants.MESSAGE_ID, MessageId.REQUIRED_TO_LOGIN);
String loginView = httpRequest.getContextPath() + Constants.LOGIN_PAGE;
httpResponse.sendRedirect(loginView);
} else if (!user.getRole().equals(Role.SYSTEM_ADMINISTRATOR)) {
System.out.println("User Role : " + user.getRole());
session.setAttribute(Constants.MESSAGE_ID, MessageId.REQUIRED_TO_ADMIN_ROLE);
String loginView = httpRequest.getContextPath() + Constants.LOGIN_PAGE;
httpResponse.sendRedirect(loginView);
} else {
filterChain.doFilter(servletRequest, servletResponse);
}
servletContext.log("Exiting the filter");
}
public void destroy() {
}
}
login.xhtml
....
<f:event listener="#{LoginBean.checkPermission}" type="preRenderView" />
....
LoginBean.java
@Scope(ScopeType.EVENT)
@Name("LoginBean")
public class LoginBean extends BaseBean {
....
public boolean authenticate() {
....
}
public void checkPermission(ComponentSystemEvent event) {
FacesContext context = getFacesContext();
ExternalContext extContext = context.getExternalContext();
String messageId = (String) extContext.getSessionMap().remove(Constants.MESSAGE_ID);
if(messageId != null) {
identity.logout();
addMessage(null, FacesMessage.SEVERITY_ERROR, messageId);
}
}
}
Don't use identity.logout();
in prerenderview
method. In AuthenticationFilter
, do as below before you pass the messageID if you would like to destory current session and create new session.
if(...) {
session.invalidate();
session = httpRequest.getSession(true);
....
} else if(...){
session.invalidate();
session = httpRequest.getSession(true);
....
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With