2016-11-24 134 views
0

我正在開發JEE 6 projet,客戶需要有時防止會話超時。我想用布爾型複選框允許永遠的用戶保持聯繫或不喜歡他想要的。如何防止managedBean會話超時?

我通過以下技術,其中的myType必須動心:客戶服務器;

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

你好這個話題已經在這裏討論:[http://stackoverflow.com/questions/2960764/how-to-set-session-timeout-dynamically - 在Java的Web應用程序](http://stackoverflow.com/questions/2960764/how-to-set-session-timeout-dynamically-in-java-web-applications)在這裏:[http:// stackoverflow.com/questions/15382895/session-timeout-in-web-xml](http://stackoverflow.com/questions/15382895/session-timeout-in-web-xml) – aelkz

回答

0

我已經解決了其他方式這個問題:

  1. 我沒有使用javax.faces.STATE_SAVING_METHOD
  2. 在我的web.xml我用:會話超時= 20
  3. 在我登錄表單
  4. 我從j_security_check通過創建改變動作形式j_security_check.jsp JSP文件
  5. 我已經在登錄表單一個複選框加入到知道,如果用戶想留連接與否。
  6. 以我managedBean我檢查KEEP_CONNECT值,禁用超時,直到手動deconnexionuserSession.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()); 
} 
0

通過部署描述符,將其設置爲-1將使其無限期:

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

在這種情況下,它總是** ** indefini te,而不是OP要求的 – Kukeltje