2011-04-20 56 views
56

可能的答案很簡單:如何在春季安全中手動註銷當前登錄的用戶? 是否足以撥打:如何手動註銷彈簧安全的用戶?

SecurityContextHolder.getContext().getAuthentication().setAuthenticated(false); 

+0

非常感謝您的回答傢伙,我會檢查出來。 – Erik 2011-04-20 14:27:19

回答

27

在Servlet 3.0容器中Spring的註銷功能與servlet集成在一起,您只需在HttpServletRequest上調用logout()即可。仍然需要編寫有效的響應內容。

根據documentation(春季3.2):

的HttpServletRequest.logout()方法可用於出登錄當前用戶。

通常,這意味着SecurityContextHolder的將被清除出 ,在HttpSession將失效,任何 「記住我」 認證將被清理等

+0

具有的Tomcat 7的'org.apache.catalina.connector.Request對Spring 3.1.2/2.2.0的Grails#logout'實施的NPE。 'clearContext()'沒有這樣的錯誤。 – 2014-10-22 05:15:26

+0

@PavelVlasov我相信這是Spring配置中的一些錯誤,請閱讀SecurityContextHolder。 – 2015-06-19 09:13:30

+1

後'request.logout()'我 'SecurityContextHolder.getContext()。getAuthentication仍然授權用戶()' – GKislin 2017-10-25 15:08:40

56

我很難確定您的代碼是否足夠。然而標準的Spring-security的註銷實現是不同的。如果您在SecurityContextLogoutHandler接過來一看,你會看到他們這樣做:

SecurityContextHolder.clearContext(); 

此外,他們可選擇無效的HttpSession:

if (invalidateHttpSession) { 
     HttpSession session = request.getSession(false); 
     if (session != null) { 
      session.invalidate(); 
     } 
    } 

您可能會發現更多的信息in some other question about logging out in Spring Security,並通過查看the source code of org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler。如果要強制註銷的用戶的所有會話,然後使用getAllSessions方法和調用每個會話的信息expireNow

sessionRegistry.getSessionInformation(sessionId).expireNow(); 

+2

您也想,如果它被設置爲下降了rememberMe餅乾。 – sourcedelica 2011-04-20 13:08:30

9

您還可以使用SessionRegistry作爲。

編輯
這需要ConcurrentSessionFilter(或鏈中的任何其它過濾器),用來檢查SessionInformation和調用所有註銷處理程序,然後執行重定向。

10

要註銷Web應用程序中的用戶,您還可以將其重定向到註銷頁面。 LogoutFilter然後爲你做所有的工作。

註銷頁面的URL設置在安全配置:

<sec:http ...> 
    ... 
    <sec:logout logout-url="/logout" logout-success-url="/login?logout_successful=1" /> 
    ... 
</sec:http> 
+1

這沒有爲我工作。它並不像你所說的那麼簡單。有沒有更多配置細節? – djangofan 2012-11-30 04:14:54

+0

應該有必要的任何其他配置這個工作。您只需將用戶重定向到像「http://example.com/yourctx/logout」這樣的URL即可。 – James 2012-11-30 07:55:20

+0

我得到它的工作,但我不得不重新啓動Tomcat爲它開始工作。 – djangofan 2012-11-30 17:33:22

15

我使用LogoutFilter相同的代碼,重用LogoutHandlers如下:

public static void myLogoff(HttpServletRequest request, HttpServletResponse response) { 
    CookieClearingLogoutHandler cookieClearingLogoutHandler = new CookieClearingLogoutHandler(AbstractRememberMeServices.SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY); 
    SecurityContextLogoutHandler securityContextLogoutHandler = new SecurityContextLogoutHandler(); 
    cookieClearingLogoutHandler.logout(request, response, null); 
    securityContextLogoutHandler.logout(request, response, null); 
} 
+0

+1,因爲CookieClearingLogoutHandler,否則用戶可以再次登錄,如果她有了rememberme Cookie的話。請注意,這個類需要春天3.1 – redochka 2014-05-20 10:22:34

2

簡單地做這樣的(那些評論爲「關注你」):

Authentication auth = SecurityContextHolder.getContext().getAuthentication(); // concern you 

    User currUser = userService.getUserById(auth.getName()); // some of DAO or Service... 

    SecurityContextLogoutHandler ctxLogOut = new SecurityContextLogoutHandler(); // concern you 

    if(currUser == null){ 
     ctxLogOut.logout(request, response, auth); // concern you 
    } 
2

最近我們不得不實現註銷f使用Spring-security 3.0.5的非功能性。儘管這個問題已經回答了上面,我將發佈完整的代碼,這肯定會幫助新手用戶像我一樣:)

配置在Spring-security.xml文件

<http auto-config="false" lowercase-comparisons="false" use-expressions="true"> 
    <custom-filter position="LOGOUT_FILTER" ref="logoutFilter" /> 
</http> 

<beans:bean id="logoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter"> 
     <beans:constructor-arg name="logoutSuccessHandler" ref="xxxLogoutSuccessHandler" /> 
    <beans:constructor-arg name="handlers"> 
     <beans:list> 
      <beans:ref bean="securityContextLogoutHandler"/> 
      <beans:ref bean="xxxLogoutHandler"/> 
     </beans:list> 
    </beans:constructor-arg> 
    <beans:property name="filterProcessesUrl" value="/logout"/> 
</beans:bean> 
<beans:bean id="XXXLogoutSuccessHandler" class="com.tms.dis.sso.XXXLogoutSuccessHandler"/> 
<beans:bean id="securityContextLogoutHandler" class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler"> 
    <beans:property name="invalidateHttpSession" value="true"/> 
</beans:bean> 
<beans:bean id="XXXLogoutHandler" class="com.tms.dis.sso.XXXLogoutHandler"/> 

在這裏,我已經創建了兩個自定義類

  1. XXXLogoutHandler它將實現org.springframework.security.web.authentication.logout.LogoutHandler並將覆蓋logout()方法。
  2. XXXLogoutSuccessHandler它將實現org.springframework.security.web.authentication.logout.LogoutSuccessHanlder並將覆蓋onLoguoutSuccess()方法。在XXXLogoutSuccessHandler.onLogoutSuccess()方法中,調用將用戶註銷到特定targetURL的redirectStrategy.sendRedirect()方法。
  3. org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler做用戶會話失效的任務。

希望這會幫助,並給予正確的方向起動

注意:自定義實施故意沒有公佈代碼。

1

new SecurityContextLogoutHandler().logout(request, null, null);

0

右Oledzki ,我使用例如下面我控制器內註銷並將用戶重定向到登錄頁面,在春季安全4.2.3

SecurityContextHolder.clearContext(); 

if(session != null) 
    session.invalidate(); 

return "redirect:/login";