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

 更新时间:2024年08月01日 14:56:04   作者:杜小舟  
这篇文章主要介绍了Spring Security之LogoutSuccessHandler注销成功操作方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

前言

LogoutSuccessHandler 接口定义了在用户成功注销后执行的操作。

当用户从应用程序中注销时,这个处理器被触发。

它允许我们开发者自定义注销成功后的行为,例如重定向到特定页面、显示注销确认信息、进行清理工作或其他自定义逻辑。

接下来先简单介绍官方的处理器,再自己自定义一个处理器。

官方给的处理器

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

注销成功后转发到一个URL地址。

    @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 ForwardLogoutSuccessHandler("/logout");
    }

HttpStatusReturningLogoutSuccessHandler

不做重定向也不做转发,而是返回一个指定的HTTP状态码。

    @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 HttpStatusReturningLogoutSuccessHandler(HttpStatus.OK);
    }

DelegatingLogoutSuccessHandler

DelegatingLogoutSuccessHandler 用于处理用户注销成功后根据不同的请求条件选择并执行相应的 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("退出登录成功 ...");

        /**
         * 设置响应状态值
         */
        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 自定义+动态切换数据源教程

    SpringBoot 自定义+动态切换数据源教程

    这篇文章主要介绍了SpringBoot 自定义+动态切换数据源教程,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    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开发中的一个方向,下面这篇文章主要给大家介绍了关于javaweb前端向后端传值的几种方式的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-03-03
  • Java程序包不存在的3种解决方法总结

    Java程序包不存在的3种解决方法总结

    包存在的,但是启动项目的时候提示包不存在,所以解决下,这篇文章主要给大家介绍了关于Java程序包不存在的3种解决方法,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2023-01-01
  • 如何获取 Spring heapdump中的明文密码

    如何获取 Spring heapdump中的明文密码

    Actuator是Spring Boot提供的应用系统监控的开源框架,在攻防场景里经常会遇到Actuator配置不当的情况,攻击者可以直接下载heapdump堆转储文件,本文介绍如何获取 Spring heapdump中的密码明文,需要的朋友可以参考下
    2022-07-07
  • Spring的@CrossOrigin注解使用与CrossFilter对象自定义详解

    Spring的@CrossOrigin注解使用与CrossFilter对象自定义详解

    这篇文章主要介绍了Spring的@CrossOrigin注解使用与CrossFilter对象自定义详解,跨域,指的是浏览器不能执行其他网站的脚本,它是由浏览器的同源策略造成的,是浏览器施加的安全限制,所谓同源是指,域名,协议,端口均相同,需要的朋友可以参考下
    2023-12-12
  • Awaitility同步异步工具实战示例详解

    Awaitility同步异步工具实战示例详解

    这篇文章主要为大家介绍了Awaitility同步异步工具实战示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • Java11 中基于嵌套关系的访问控制优化问题

    Java11 中基于嵌套关系的访问控制优化问题

    在 Java 语言中,类和接口可以相互嵌套,这种组合之间可以不受限制的彼此访问,包括访问彼此的构造函数、字段、方法,接下来通过本文给大家介绍Java11中基于嵌套关系的访问控制优化问题,感兴趣的朋友一起看看吧
    2022-01-01

最新评论