2014-06-10 37 views
-1

我想配置會話超時到我的應用程序。會話超時工作不同,因爲我希望

在web.xml中我配置我分鐘,像這樣:

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

若要確保一切工作我創建我的聽衆,請參見:

<listener> 
    <listener-class>br.com.sender.util.SessionListener</listener-class> 
</listener> 

見我的監聽器類:

package br.com.sender.util; 

import java.util.Date; 

import javax.servlet.http.HttpSessionEvent; 
import javax.servlet.http.HttpSessionListener; 

public class SessionListener implements HttpSessionListener { 
    public void sessionCreated(HttpSessionEvent event) { 
     System.out.printf("Session ID %s created at %s%n", event.getSession().getId(), new Date()); 
    } 

    public void sessionDestroyed(HttpSessionEvent event) { 
     System.out.printf("Session ID %s destroyed at %s%n", event.getSession().getId(), new Date()); 
    } 
} 

當我在我的應用程序登錄會話創建(在這同一時刻我存儲UserMB與sessionScope),請參閱:

Session ID 801040FAFEBC8D21C3C8E0DA56BF9B27 created at Tue Jun 10 18:44:33 BRT 2014 

有些分鐘後沒有動作會被破壞:

Session ID 801040FAFEBC8D21C3C8E0DA56BF9B27 destroyed at Tue Jun 10 18:46:26 BRT 2014 

當某些菜單或按鈕會話用戶點擊重新創建的,但我希望UserMB( SessionScope)被破壞,用戶必須重新登錄,但不會發生,用戶繼續使用應用程序而不重新登錄。

+0

你是如何檢查用戶登錄?按會話!= null或通過session.getAttrribute(「someattribute」)!= null?第一個是錯誤的。 – developerwjk

+0

我有一個UserMBLogged與SessionScope,當我需要檢查用戶是否登錄我稱之爲ManagedBean。我有一個「過濾器」來檢查用戶是否登錄。 –

+0

您稱這個託管bean做什麼? – EJP

回答

1

你需要一個WebFilter,在會話範圍檢查一些屬性,並確定用戶登錄或不

-1

你可能有一些servlet或過濾寫在你的應用程序,它向前攔截所有的請求,然後它因此在那個servlet /過濾器中,你必須檢查session.getAttribute(「someinfo」)!= null,如果它爲null,那麼請將請求轉發到登錄頁面。

0

這一切都取決於您如何配置您未在問題中發佈的security

如果不驗證登錄通過java ee web.xml安全設置的用戶,或者通過一個filter比你的頁面檢查是不安全的,並且當用戶將達到下一個URL的session將被重新創建。

如果不使用集裝箱安全或Java EE的安全性,那麼你可以有一些類型的過濾器,檢查所有網頁除了登錄

@WebFilter("/*") 
public class LoginFilter implements Filter { 

    private ServletContext context; 

    public void init(FilterConfig fConfig) throws ServletException { 
     this.context = fConfig.getServletContext(); 
    } 

    public void doFilter(ServletRequest request, ServletResponse response, 
     FilterChain chain) throws IOException, ServletException { 

     String requestURL = ((HttpServletRequest)request).getRequestURL().toString(); 
     HttpSession session = req.getSession(false); 

     if(session == null && !(requestURL.endsWith("login.xhtml"))){ 
      ((HttpServletResponse)response).sendRedirect("login.xhtml"); 
     }else{ 
      chain.doFilter(request, response); 
     } 
    } 
}