2014-07-12 63 views
0

所有工作正常,但註銷和會話銷燬無效,我不知道爲什麼。Java Session無效並且超時不起作用

爲什麼我可以訪問受保護的區域,如果會話無效或達到會話超時。

看這個HTTP-Server-Monitor

'http://localhost:8080/psg/admin/' 

<security-constraint> 
    <display-name>My First Sec Constraint</display-name> 
    <web-resource-collection> 
     <web-resource-name>Protected Area</web-resource-name> 
     <url-pattern>/admin/*</url-pattern> 
     .. 

登錄的Servlet映射到/管理/映射到/管理

HttpSession session = request.getSession(); 

    if (session != null) { 
     session.setAttribute("ID", session.getId()); 
     session.setAttribute("User", request.getRemoteUser()); 
     session.setAttribute("isAuthenticated", true); 
     getServletContext().getRequestDispatcher("/index.jsp").forward(request, response); 
    }  

退出的Servlet /註銷

HttpSession session = request.getSession(false); 

    if(session!=null){ 
     session.invalidate(); 
     response.sendRedirect(request.getContextPath()); 
    } 

如果session-timeout必須銷燬會話,則會出現同樣的問題。 我還可以在此時間後得到一個有效的會話,如果我進入保護區/ PSG /管理/

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

您使用基本身份驗證嗎?然後,您的註銷將不起作用,因爲瀏覽器將重新發送每個請求的用戶憑據。 – Gas

+0

是的,我使用Digist身份驗證。我認爲類似於基本。謝謝你的幫助。 – user3623194

回答

2

在基本情況和摘要式身份驗證瀏覽器會重新發送用戶憑證,以便有效地沒有註銷,只會話失效。

您需要使用基於表單的身份驗證來註銷才能正常工作。

+0

通過基於_form的身份驗證_你的意思是Spring安全嗎?你能提供一個例子嗎? – Lucky

+0

@Lucky不,我的意思是基於Java EE表單的認證,而不是任何Spring相關的。正確的Java EE身份驗證在 FORM ...元素的'web.xml'文件中定義。 – Gas