如何防止managedBean会话超时?

时间:2016-11-24 13:17:21

标签: jsf session-timeout managed-bean

我正在使用JEE 6项目,客户有时需要防止会话超时。我想使用布尔复选框来允许用户保持联系或不想要他人。

我受到以下技术的诱惑,其中 myType 必须是:客户端服务器;

<context-param>
  <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
  <param-value>#{mySession.myType}</param-value>
</context-param>

2 个答案:

答案 0 :(得分:1)

通过部署描述符,将其设置为-1将使其无限期:

 <session-config>
    <session-timeout>
        -1
    </session-timeout>
</session-config>

答案 1 :(得分:1)

我以其他方式解决了这个问题:

  1. 我没有使用 javax.faces.STATE_SAVING_METHOD
  2. web.xml 中,我已经使用过: session-timeout = 20
  3. 在我的 loginForm
  4. 我已经通过创建 jsp文件操作表单 j_security_check 更改为 j_security_check.jsp >
  5. 我已在登录表单中添加复选框,以了解用户是否希望保持连接状态。
  6. 在我的managedBean中,我检查 KEEP_CONNECT 值,禁用timeOut,直到手动deconnexion userSession.setMaxInactiveInterval(-1); < em>或保持此会话更长(2小时): userSession.setMaxInactiveInterval(7200);
  7. 评论:

    <强>的web.xml

    <session-config\> <session-timeout>20</session-timeout> </session-config>

    <强>登录表单

    <form method=post action="/j_security_check.jsp" > <input type="text" name= "j_username" > <input type="password" name= "j_password" > <input type="checkbox" name="j_remember" /> </form>

    <强> j_security_check.jsp

    //Have we already authenticated someone ?
        if (request.getUserPrincipal() == null) {
    
            String j_username = request.getParameter("j_username");
            String j_password = request.getParameter("j_password");
            String j_remember = request.getParameter("j_remember");
    
            try {
    
                request.login(j_username, j_password);
    
                if("on".equals(j_remember)){
                    session.setAttribute(KEEP_CONNECT, true);
                } else {
                    session.setAttribute(KEEP_CONNECT, false);
                }
    
                logger.debug("Authentication of '" + request.getUserPrincipal() + "' was successful.");
                response.sendRedirect(request.getContextPath() +HOME_PAGE);
            } catch (Exception ex) {
                logger.error(ex,"Authentication failed.");
                response.sendRedirect(request.getContextPath() + ERROR_PAGE);
            }
    
        } else {
            logger.debug("Already authenticated '" + request.getUserPrincipal() + "'.");
            response.sendRedirect(request.getContextPath() + LOGIN_PAGE);
        }
    

    <强> SessionManagedBean

    private void initTimeOut() {
            String login          =           FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal().getName();
            boolean keepConnected = (boolean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(KEEP_CONNECT);
    
            logger.debug(login + " IN > " + userSession.getMaxInactiveInterval());
            logger.debug(" keepConnected ? = " + keepConnected);
    
            if (keepConnected) {
                //keep this session and disable timeOut until the manual deconnexion
                userSession.setMaxInactiveInterval(-1);
            }
    
            logger.debug(login + " OUT > " + userSession.getMaxInactiveInterval());
    }