1. 概述
在本簡短教程中,我們將深入瞭解如何在 Spring Security 中禁用登出重定向。
首先,我們將對 Spring Security 中登出流程進行簡要背景介紹。然後,我們將通過一個實際示例,説明如何在成功登出後避免用户重定向。
2. 在 Spring Security 中註銷
簡而言之,Spring Security 通過 logout() DSL 方法提供對註銷機制的原生支持。基本上,Spring Security 在用户訪問默認註銷 URL 時觸發註銷,該 URL 為 /logout。
值得一提的是,默認註銷 URL 的值為 /j_spring_security_logout,在 Spring Security 4 之前。
Spring Security 提供了將用户重定向到特定 URL 的可能性,但在某些情況下,我們可能希望避免這種行為。
那麼,不廢話,讓我們看看如何在 Spring Security 中實現禁用註銷重定向的邏輯。
3. 禁用 Spring Security 註銷重定向
默認情況下,Spring Security 會將用户重定向到 /login?logout 頁面進行註銷。因此,本節將重點介紹如何防止用户在註銷後重定向到登錄頁面。
請注意,我們可以藉助 .logoutSuccessUrl() DSL 方法來覆蓋默認重定向 URL。
主要目的是展示如何在調用 /logout URL 時,避免重定向,尤其是在從 REST 客户端調用時。
事實上,LogoutSuccessHandler 接口提供了一種靈活的方式來在註銷過程成功執行時執行自定義邏輯。
因此,我們將 使用自定義 LogoutSuccessHandler 以返回僅 200 狀態碼。這樣就不會將我們重定向到任何頁面。
現在,讓我們實現必要的 Spring Security 配置,以禁用註銷重定向:
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(authz -> authz.requestMatchers("/login")
.permitAll()
.anyRequest()
.authenticated())
.logout(logout -> logout.permitAll()
.logoutSuccessHandler((request, response, authentication) -> {
response.setStatus(HttpServletResponse.SC_OK);
}));
return http.build();
}
}
從上述配置中可以看出,logoutSuccessHandler() 方法。正如我們所見,我們使用 lambda 表達式來定義我們的自定義註銷成功處理程序。
請記住,我們還可以創建一個簡單的 LogoutSuccessHandler 接口的實現類,並使用 DSL 將其傳遞到 logoutSuccessHandler() 方法中。
4. 測試
現在我們已經將所有組件整合在一起,讓我們測試 /logout 端點以確認一切按預期工作。
請注意,我們將使用 MockMvc 發送 /logout 請求,用於測試。
首先,讓我們創建一個簡單的測試類並將其注入其中:
public class LogoutApplicationUnitTest {
@Autowired
private MockMvc mockMvc;
// test case
}
現在,讓我們編寫一個方法來測試我們的 /logout 端點:
@Test
public void whenLogout_thenDisableRedirect() throws Exception {
this.mockMvc.perform(post("/logout").with(csrf()))
.andExpect(status().isOk())
.andExpect(jsonPath("$").doesNotExist())
.andExpect(unauthenticated())
.andReturn();
}
最後,讓我們分解我們的測試代碼:
- .perform(post(“/logout”)) 調用 /logout 端點,作為簡單的 POST 請求
- .with(csrf()) 將預期的 _csrf 參數添加到查詢
- .status() 返回 HTTP 響應的狀態碼
- .jsonPath() 允許訪問 HTTP 響應的 Body
5. 結論
總而言之,我們已經解釋並説明了如何解決 Spring Security 和 Spring Boot 中禁用註銷重定向的問題。