在我的AuthenticationFilter重定向到登录页面后,我想注销到用户。
这就是为什么,我将identity.logout();放在预渲染方法checkPermission(...) of login.xhtml中。
但是,当用户再次登录时,我会得到ViewExpiredException。
我的问题是
1 : --如果我不执行identity.logout();,由于旧的用户会话,用户再次重新登录仍然存在。2 :,如果我做了identity.logout();,当用户再次登录时,我会得到ViewExpiredException。
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);   
        }
    }
}发布于 2012-12-24 04:00:27
不要在identity.logout();方法中使用prerenderview。在AuthenticationFilter中,如果希望销毁当前会话并创建新会话,请按照下面的步骤传递messageID。
if(...) {
    session.invalidate();
    session = httpRequest.getSession(true); 
    ....
} else if(...){
    session.invalidate();
    session = httpRequest.getSession(true); 
    ....
}https://stackoverflow.com/questions/13989489
复制相似问题