Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewExpiredException after identity.logout(); in JBoss Seam

Tags:

java

jsf

seam

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);   
        }
    }
}
like image 901
Zaw Than oo Avatar asked Dec 21 '12 11:12

Zaw Than oo


1 Answers

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); 
    ....
}
like image 128
Ye Wint Avatar answered Sep 26 '22 22:09

Ye Wint