最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Security之LogoutSuccessHandler注銷成功操作方式

 更新時間:2024年08月01日 14:56:04   作者:杜小舟  
這篇文章主要介紹了Spring Security之LogoutSuccessHandler注銷成功操作方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

前言

LogoutSuccessHandler 接口定義了在用戶成功注銷后執(zhí)行的操作。

當用戶從應用程序中注銷時,這個處理器被觸發(fā)。

它允許我們開發(fā)者自定義注銷成功后的行為,例如重定向到特定頁面、顯示注銷確認信息、進行清理工作或其他自定義邏輯。

接下來先簡單介紹官方的處理器,再自己自定義一個處理器。

官方給的處理器

SimpleUrlLogoutSuccessHandler

注銷成功后重定向到一個URL地址。

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowCredentials(true);

        http
                // 退出登錄
                .logout()
                // 退出登錄成功后處理器
                .logoutSuccessHandler(logoutSuccessHandler());
    }

    @Bean
    public LogoutSuccessHandler logoutSuccessHandler() {
        SimpleUrlLogoutSuccessHandler logoutSuccessHandler = new SimpleUrlLogoutSuccessHandler();
        // 注銷成功后重定向的地址
        logoutSuccessHandler.setDefaultTargetUrl("/logout");
        return logoutSuccessHandler;
    }

ForwardLogoutSuccessHandler

注銷成功后轉發(fā)到一個URL地址。

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowCredentials(true);

        http
                // 退出登錄
                .logout()
                // 退出登錄成功后處理器
                .logoutSuccessHandler(logoutSuccessHandler());
    }

    @Bean
    public LogoutSuccessHandler logoutSuccessHandler() {
    	// 轉發(fā)地址
        return new ForwardLogoutSuccessHandler("/logout");
    }

HttpStatusReturningLogoutSuccessHandler

不做重定向也不做轉發(fā),而是返回一個指定的HTTP狀態(tài)碼。

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowCredentials(true);

        http
                // 退出登錄
                .logout()
                // 退出登錄成功后處理器
                .logoutSuccessHandler(logoutSuccessHandler());
    }
    
    @Bean
    public LogoutSuccessHandler logoutSuccessHandler() {
        // 也可以指定其他狀態(tài)碼
        return new HttpStatusReturningLogoutSuccessHandler(HttpStatus.OK);
    }

DelegatingLogoutSuccessHandler

DelegatingLogoutSuccessHandler 用于處理用戶注銷成功后根據不同的請求條件選擇并執(zhí)行相應的 LogoutSuccessHandler。

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowCredentials(true);

        http
                // 退出登錄
                .logout()
                // 退出登錄成功后處理器
                .logoutSuccessHandler(logoutSuccessHandler());
    }

    @Bean
    public LogoutSuccessHandler logoutSuccessHandler() {
        LinkedHashMap<RequestMatcher, LogoutSuccessHandler> matcherToHandler = new LinkedHashMap<>();
        // 配置不同的RequestMatcher和對應的LogoutSuccessHandler
        // 配置在 /admin/** 路徑下退出登錄匹配的 SimpleUrlLogoutSuccessHandler
        SimpleUrlLogoutSuccessHandler simpleUrlLogoutSuccessHandler = new SimpleUrlLogoutSuccessHandler();
        simpleUrlLogoutSuccessHandler.setDefaultTargetUrl("/admin-logout");
        matcherToHandler.put(new AntPathRequestMatcher("/admin/**"), simpleUrlLogoutSuccessHandler);

        // 配置在 /user/** 路徑下退出登錄匹配的 ForwardLogoutSuccessHandler
        matcherToHandler.put(new AntPathRequestMatcher("/user/**"), new ForwardLogoutSuccessHandler("/user-logout"));

        DelegatingLogoutSuccessHandler handler = new DelegatingLogoutSuccessHandler(matcherToHandler);
        // 配置默認的 ForwardLogoutSuccessHandler
        handler.setDefaultLogoutSuccessHandler(new ForwardLogoutSuccessHandler("/default-logout"));
        
        return handler;
    }

自定義處理器

package com.security.handler.logout;

import com.alibaba.fastjson2.JSON;
import com.security.controller.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Component
@Slf4j
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler {

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        log.info("退出登錄成功 ...");

        /**
         * 設置響應狀態(tài)值
         */
        response.setStatus(200);
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        String json = JSON.toJSONString(
                ResponseResult.builder()
                        .code(200)
                        .message("退出登錄成功!")
                        .build());

        // JSON信息
        response.getWriter().println(json);
    }
}
package com.security.config;

import com.security.handler.logout.LogoutSuccessHandlerImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.web.cors.CorsConfiguration;


@Configuration
@EnableWebSecurity
// 開啟限制訪問資源所需權限
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfigurationTest extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowCredentials(true);

        http
                // 退出登錄
                .logout()
                // 退出登錄成功后處理器
                .logoutSuccessHandler(logoutSuccessHandler());
    }

    @Bean
    public LogoutSuccessHandler logoutSuccessHandler() {
        return new LogoutSuccessHandlerImpl();
    }
    
}

總結

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。 

相關文章

  • mybatis test標簽如何判斷值是否相等

    mybatis test標簽如何判斷值是否相等

    這篇文章主要介紹了mybatis test標簽判斷值是否相等的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • SpringBoot 自定義+動態(tài)切換數據源教程

    SpringBoot 自定義+動態(tài)切換數據源教程

    這篇文章主要介紹了SpringBoot 自定義+動態(tài)切換數據源教程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Mybatis自定義typeHandle過程解析

    Mybatis自定義typeHandle過程解析

    這篇文章主要介紹了Mybatis自定義typeHandle過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • SpringBoot整合Kafka實現高可用消息隊列集群詳解

    SpringBoot整合Kafka實現高可用消息隊列集群詳解

    Apache?Kafka是一個分布式流處理平臺,這篇文章主要介紹了SpringBoot如何整合Kafka實現高可用消息隊列集群,文中的示例代碼講解詳細,感興趣的小伙伴可以了解下
    2026-01-01
  • javaweb前端向后端傳值的幾種方式總結(附代碼)

    javaweb前端向后端傳值的幾種方式總結(附代碼)

    javaweb是java開發(fā)中的一個方向,下面這篇文章主要給大家介紹了關于javaweb前端向后端傳值的幾種方式的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-03-03
  • Java程序包不存在的3種解決方法總結

    Java程序包不存在的3種解決方法總結

    包存在的,但是啟動項目的時候提示包不存在,所以解決下,這篇文章主要給大家介紹了關于Java程序包不存在的3種解決方法,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2023-01-01
  • 如何獲取?Spring?heapdump中的明文密碼

    如何獲取?Spring?heapdump中的明文密碼

    Actuator是Spring?Boot提供的應用系統(tǒng)監(jiān)控的開源框架,在攻防場景里經常會遇到Actuator配置不當的情況,攻擊者可以直接下載heapdump堆轉儲文件,本文介紹如何獲取?Spring?heapdump中的密碼明文,需要的朋友可以參考下
    2022-07-07
  • Spring的@CrossOrigin注解使用與CrossFilter對象自定義詳解

    Spring的@CrossOrigin注解使用與CrossFilter對象自定義詳解

    這篇文章主要介紹了Spring的@CrossOrigin注解使用與CrossFilter對象自定義詳解,跨域,指的是瀏覽器不能執(zhí)行其他網站的腳本,它是由瀏覽器的同源策略造成的,是瀏覽器施加的安全限制,所謂同源是指,域名,協(xié)議,端口均相同,需要的朋友可以參考下
    2023-12-12
  • Awaitility同步異步工具實戰(zhàn)示例詳解

    Awaitility同步異步工具實戰(zhàn)示例詳解

    這篇文章主要為大家介紹了Awaitility同步異步工具實戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • Java11?中基于嵌套關系的訪問控制優(yōu)化問題

    Java11?中基于嵌套關系的訪問控制優(yōu)化問題

    在?Java?語言中,類和接口可以相互嵌套,這種組合之間可以不受限制的彼此訪問,包括訪問彼此的構造函數、字段、方法,接下來通過本文給大家介紹Java11中基于嵌套關系的訪問控制優(yōu)化問題,感興趣的朋友一起看看吧
    2022-01-01

最新評論

平顶山市| 丰都县| 丹棱县| 卫辉市| 监利县| 桓仁| 麟游县| 栖霞市| 安丘市| 仙游县| 乌鲁木齐市| 山阴县| 太白县| 鲁甸县| 称多县| 紫金县| 游戏| 嵊州市| 安吉县| 凤山县| 阜南县| 宜兰县| 大厂| 罗甸县| 通渭县| 绩溪县| 白河县| 祁阳县| 上林县| 克什克腾旗| 九寨沟县| 南阳市| 嫩江县| 孝感市| 田林县| 松原市| 通渭县| 右玉县| 麟游县| 砀山县| 大化|