自己定義一個退出方法:
@RequestMapping({"/admin/logout.htm"}) public String logout(HttpServletRequest request, HttpServletResponse response) { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); if (auth != null) {//清除認證 new SecurityContextLogoutHandler().logout(request, response, auth); } return "redirect:/admin/login.htm"; }
首先是確定,用戶點擊登出鏈接的時候,是否是在已驗證的前提下(這里可能是用戶的session失效等情況): SecurityContextHolder.getContext().getAuthentication()
如果上述代碼獲取的Authentication對象不為空,然后我們調用 SecurityContextLogoutHandler().logout(request, response, auth) 來對用戶進行登出操作。
登出的操作需要進行下面幾個步驟:
1、使HTTP Session失效,然后解綁Session上的所以已綁定的對象。
2、將用戶的認證信息從spring security的SecurityContext中移除,以防止並發請求的問題。
3、從當前線程中,完全的清除相關的屬性值。
到這就完成了。