Spring Security實現(xiàn)退出登錄和退出處理器
在系統(tǒng)中一般都有退出登錄的操作。退出登錄后,Spring Security進行了以下操作:
- 清除認證狀態(tài)
- 銷毀HttpSession對象
- 跳轉(zhuǎn)到登錄頁面
配置退出登錄的路徑和退出后跳轉(zhuǎn)的路徑
//退出登錄配置 http.logout() .logoutUrl("/logout") .logoutSuccessUrl("/login.html") .clearAuthentication(true) .invalidateHttpSession(true);
在網(wǎng)頁中添加退出登錄超鏈接
<!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>
退出成功處理器
我們也可以自定義退出成功處理器,在退出后清理一些數(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);
測試
到此這篇關(guān)于Spring Security實現(xiàn)退出登錄和退出處理器的文章就介紹到這了,更多相關(guān)Spring Security退出登錄和退出處理器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 出現(xiàn)問題javax.servlet.http.HttpServlet was not found解決方法
這篇文章主要介紹了java 出現(xiàn)問題javax.servlet.http.HttpServlet was not found解決方法的相關(guān)資料,需要的朋友可以參考下2016-11-11使用MyBatis查詢千萬級數(shù)據(jù)量操作實現(xiàn)
這篇文章主要為大家介紹了如何使用MyBatis?查詢千萬數(shù)據(jù)量的操作過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05詳解springboot通過Async注解實現(xiàn)異步任務(wù)及回調(diào)的方法
這篇文章主要介紹了springboot通過Async注解實現(xiàn)異步任務(wù)及回調(diào),文中通過一個簡單示例來直觀的理解什么是同步調(diào)用,在單元測試用例中,注入?SyncTask?對象,并在測試用例中執(zhí)行?doTaskOne(),doTaskTwo(),doTaskThree()?三個方法,具體實現(xiàn)方式跟隨小編一起看看吧2022-05-05Java遠程執(zhí)行shell命令出現(xiàn)java: command not found問題及解決
這篇文章主要介紹了Java遠程執(zhí)行shell命令出現(xiàn)java: command not found問題及解決方案,具有很好的參考價值,希望對大家有所幫助。2023-07-07SpringMVC中RequestBody注解的List參數(shù)傳遞方式
這篇文章主要介紹了SpringMVC中RequestBody注解的List參數(shù)傳遞方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10