在系統(tǒng)中一般都有退出登錄的操作。退出登錄后,Spring Security進(jìn)行了以下操作:
- 清除認(rèn)證狀態(tài)
- 銷毀HttpSession對(duì)象
- 跳轉(zhuǎn)到登錄頁(yè)面
配置退出登錄的路徑和退出后跳轉(zhuǎn)的路徑
//退出登錄配置
http.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/login.html")
.clearAuthentication(true)
.invalidateHttpSession(true);
在網(wǎng)頁(yè)中添加退出登錄超鏈接
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
<meta charset="UTF-8">
<title>主頁(yè)面</title>
</head>
<body>
<h1>主頁(yè)面</h1>
<a href="/logout" rel="external nofollow" >退出登錄</a>
</body>
</html>
退出成功處理器
我們也可以自定義退出成功處理器,在退出后清理一些數(shù)據(jù),寫法如下:
自定義退出成功處理器
/**
* @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("清楚一些數(shù)據(jù)");
response.sendRedirect("/login.html");
}
}
配置退出成功處理器
//退出登錄配置
http.logout()
.logoutUrl("/logout")
// .logoutSuccessUrl("/login.html")
.logoutSuccessHandler(new LogoutSuccessHandler())
.clearAuthentication(true)
.invalidateHttpSession(true);
測(cè)試
到此這篇關(guān)于Spring Security實(shí)現(xiàn)退出登錄和退出處理器的文章就介紹到這了,更多相關(guān)Spring Security退出登錄和退出處理器內(nèi)容請(qǐng)搜索html5模板網(wǎng)以前的文章希望大家以后多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!