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();
}
}總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot整合Kafka實現高可用消息隊列集群詳解
Apache?Kafka是一個分布式流處理平臺,這篇文章主要介紹了SpringBoot如何整合Kafka實現高可用消息隊列集群,文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2026-01-01
Spring的@CrossOrigin注解使用與CrossFilter對象自定義詳解
這篇文章主要介紹了Spring的@CrossOrigin注解使用與CrossFilter對象自定義詳解,跨域,指的是瀏覽器不能執(zhí)行其他網站的腳本,它是由瀏覽器的同源策略造成的,是瀏覽器施加的安全限制,所謂同源是指,域名,協(xié)議,端口均相同,需要的朋友可以參考下2023-12-12

