可能的答案很簡單:如何在春季安全中手動註銷當前登錄的用戶? 是否足以撥打:如何手動註銷彈簧安全的用戶?
SecurityContextHolder.getContext().getAuthentication().setAuthenticated(false);
?
可能的答案很簡單:如何在春季安全中手動註銷當前登錄的用戶? 是否足以撥打:如何手動註銷彈簧安全的用戶?
SecurityContextHolder.getContext().getAuthentication().setAuthenticated(false);
?
在Servlet 3.0容器中Spring的註銷功能與servlet集成在一起,您只需在HttpServletRequest
上調用logout()
即可。仍然需要編寫有效的響應內容。
根據documentation(春季3.2):
的HttpServletRequest.logout()方法可用於出登錄當前用戶。
通常,這意味着SecurityContextHolder的將被清除出 ,在HttpSession將失效,任何 「記住我」 認證將被清理等
具有的Tomcat 7的'org.apache.catalina.connector.Request對Spring 3.1.2/2.2.0的Grails#logout'實施的NPE。 'clearContext()'沒有這樣的錯誤。 – 2014-10-22 05:15:26
@PavelVlasov我相信這是Spring配置中的一些錯誤,請閱讀SecurityContextHolder。 – 2015-06-19 09:13:30
後'request.logout()'我 'SecurityContextHolder.getContext()。getAuthentication仍然授權用戶()' – GKislin 2017-10-25 15:08:40
我很難確定您的代碼是否足夠。然而標準的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();
:
您也想,如果它被設置爲下降了rememberMe餅乾。 – sourcedelica 2011-04-20 13:08:30
您還可以使用SessionRegistry作爲。
編輯
這需要ConcurrentSessionFilter
(或鏈中的任何其它過濾器),用來檢查SessionInformation和調用所有註銷處理程序,然後執行重定向。
要註銷Web應用程序中的用戶,您還可以將其重定向到註銷頁面。 LogoutFilter然後爲你做所有的工作。
註銷頁面的URL設置在安全配置:
<sec:http ...>
...
<sec:logout logout-url="/logout" logout-success-url="/login?logout_successful=1" />
...
</sec:http>
我使用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);
}
+1,因爲CookieClearingLogoutHandler,否則用戶可以再次登錄,如果她有了rememberme Cookie的話。請注意,這個類需要春天3.1 – redochka 2014-05-20 10:22:34
簡單地做這樣的(那些評論爲「關注你」):
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
}
最近我們不得不實現註銷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"/>
在這裏,我已經創建了兩個自定義類
希望這會幫助,並給予正確的方向起動
注意:自定義實施故意沒有公佈代碼。
new SecurityContextLogoutHandler().logout(request, null, null);
右Oledzki ,我使用例如下面我控制器內註銷並將用戶重定向到登錄頁面,在春季安全4.2.3
SecurityContextHolder.clearContext();
if(session != null)
session.invalidate();
return "redirect:/login";
個
非常感謝您的回答傢伙,我會檢查出來。 – Erik 2011-04-20 14:27:19