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

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

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

前言

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

當(dāng)用戶從應(yīng)用程序中注銷時(shí),這個(gè)處理器被觸發(fā)。

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

接下來先簡(jiǎn)單介紹官方的處理器,再自己自定義一個(gè)處理器。

官方給的處理器

SimpleUrlLogoutSuccessHandler

注銷成功后重定向到一個(gè)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

注銷成功后轉(zhuǎn)發(fā)到一個(gè)URL地址。

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

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

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

HttpStatusReturningLogoutSuccessHandler

不做重定向也不做轉(zhuǎn)發(fā),而是返回一個(gè)指定的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 用于處理用戶注銷成功后根據(jù)不同的請(qǐng)求條件選擇并執(zhí)行相應(yīng)的 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和對(duì)應(yīng)的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);
        // 配置默認(rèn)的 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("退出登錄成功 ...");

        /**
         * 設(shè)置響應(yīng)狀態(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
// 開啟限制訪問資源所需權(quán)限
@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();
    }
    
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。 

相關(guān)文章

  • SpringBoot整合Web之CORS支持與配置類和 XML配置及注冊(cè)攔截器

    SpringBoot整合Web之CORS支持與配置類和 XML配置及注冊(cè)攔截器

    這篇文章主要介紹了SpringBoot整合Web開發(fā)中CORS支持與配置類和 XML配置及注冊(cè)攔截器的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Java判斷ip是否為IPV4或IPV6地址的多種方式

    Java判斷ip是否為IPV4或IPV6地址的多種方式

    本文主要介紹了Java判斷ip是否為IPV4或IPV6地址的多種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • Java中的Native方法

    Java中的Native方法

    這篇文章主要介紹了Java中的Native方法,在本文中,我們將看到j(luò)ava中本機(jī)native方法的介紹。我們將看到它的基本語法及其工作原理。將有java代碼示例展示native本機(jī)方法的使用,下面來看看文章的具體介紹
    2021-12-12
  • Java使用Instant時(shí)輸出的時(shí)間比預(yù)期少了八個(gè)小時(shí)

    Java使用Instant時(shí)輸出的時(shí)間比預(yù)期少了八個(gè)小時(shí)

    在Java中,LocalDateTime表示沒有時(shí)區(qū)信息的日期和時(shí)間,而Instant表示基于UTC的時(shí)間點(diǎn),本文主要介紹了Java使用Instant時(shí)輸出的時(shí)間比預(yù)期少了八個(gè)小時(shí)的問題解決,感興趣的可以了解一下
    2024-09-09
  • SpringBoot2底層注解@ConfigurationProperties配置綁定

    SpringBoot2底層注解@ConfigurationProperties配置綁定

    這篇文章主要介紹了SpringBoot2底層注解@ConfigurationProperties配置綁定,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • java實(shí)現(xiàn)獲取用戶的MAC地址

    java實(shí)現(xiàn)獲取用戶的MAC地址

    本文給大家匯總介紹了下使用java實(shí)現(xiàn)獲取客戶端用戶的MAC地址的方法,當(dāng)然最后一種更全面一些,有需要的小伙伴們可以根據(jù)需求自由選擇。
    2015-10-10
  • Java List 用法詳解及實(shí)例分析

    Java List 用法詳解及實(shí)例分析

    這篇文章主要介紹了Java List 用法詳解及實(shí)例分析的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • 淺談java安全編碼指南之堆污染

    淺談java安全編碼指南之堆污染

    什么是堆污染呢?是指當(dāng)參數(shù)化類型變量引用的對(duì)象不是該參數(shù)化類型的對(duì)象時(shí)而發(fā)生的。我們知道在JDK5中,引入了泛型的概念,在創(chuàng)建集合類的時(shí)候,指定該集合類中應(yīng)該存儲(chǔ)的對(duì)象類型。如果在指定類型的集合中,引用了不同的類型,那么這種情況就叫做堆污染。
    2021-06-06
  • Java Session會(huì)話追蹤原理深入分析

    Java Session會(huì)話追蹤原理深入分析

    web開發(fā)階段我們主要是瀏覽器和服務(wù)器之間來進(jìn)行交互。瀏覽器和服務(wù)器之間的交互就像人和人之間進(jìn)行交流一樣,但是對(duì)于機(jī)器來說,在一次請(qǐng)求之間只是會(huì)攜帶著本次請(qǐng)求的數(shù)據(jù)的,但是可能多次請(qǐng)求之間是會(huì)有聯(lián)系的,所以提供了會(huì)話機(jī)制
    2022-11-11
  • 如何通過Java實(shí)現(xiàn)修改視頻分辨率

    如何通過Java實(shí)現(xiàn)修改視頻分辨率

    Java除了可以修改圖片的分辨率,還可以實(shí)現(xiàn)修改視頻的分辨率,這篇文章就將帶大家學(xué)習(xí)如果編寫這一工具類,感興趣的同學(xué)可以了解一下
    2021-12-12

最新評(píng)論