如何防止managedBean会话超时?

How to prevent managedBean session times out?

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

我被以下技术所吸引,其中 myType 必须是:clientserver;

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

通过部署描述符,将其设置为-1将使其不确定:

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

我已经用其他方法解决了这个问题:

  1. 我没用过javax.faces.STATE_SAVING_METHOD
  2. 在我的 web.xml 中,我使用了:session-timeout = 20
  3. 在我的 loginForm
  4. 我已将 操作表单 j_security_check 更改为 j_security_check.jsp 通过创建 jsp 文件.
  5. 我在登录表单中添加了一个复选框以了解用户是否想保持连接。
  6. 在我的 managedBean 中,我检查 KEEP_CONNECT 值, 禁用超时,直到手动断开连接userSession.setMaxInactiveInterval(-1); 或者将此会话保持更长的时间(2 小时) : userSession.setMaxInactiveInterval(7200 );

评价:

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