欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Security實現(xiàn)退出登錄和退出處理器

 更新時間:2022年05月19日 15:19:30   作者:科比·布萊恩特【24】  
本文主要介紹了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)文章

最新評論