在系統中一般都有退出登錄的操作。退出登錄后,Spring Security進行了以下操作:
- 清除認證狀態
- 銷毀HttpSession對象
- 跳轉到登錄頁面
配置退出登錄的路徑和退出后跳轉的路徑
//退出登錄配置
http.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/login.html")
.clearAuthentication(true)
.invalidateHttpSession(true);
在網頁中添加退出登錄超鏈接
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
<meta charset="UTF-8">
<title>主頁面</title>
</head>
<body>
<h1>主頁面</h1>
<a href="/logout" rel="external nofollow" >退出登錄</a>
</body>
</html>
退出成功處理器
我們也可以自定義退出成功處理器,在退出后清理一些數據,寫法如下:
自定義退出成功處理器
/**
* @Author yqq
* @Date 2022/05/17 18:09
* @Version 1.0
*/
public class LogoutSuccessHandler implements org.springframework.security.web.authentication.logout.LogoutSuccessHandler {
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
System.out.println("清楚一些數據");
response.sendRedirect("/login.html");
}
}
配置退出成功處理器
//退出登錄配置
http.logout()
.logoutUrl("/logout")
// .logoutSuccessUrl("/login.html")
.logoutSuccessHandler(new LogoutSuccessHandler())
.clearAuthentication(true)
.invalidateHttpSession(true);
測試
到此這篇關于Spring Security實現退出登錄和退出處理器的文章就介紹到這了,更多相關Spring Security退出登錄和退出處理器內容請搜索html5模板網以前的文章希望大家以后多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!