自定義Spring Security的身份驗證失敗處理方法
1.概述
在本快速教程中,我們將演示如何在Spring Boot應用程序中自定義Spring Security的身份驗證失敗處理。目標是使用表單登錄方法對用戶進行身份驗證。
2.認證和授權(Authentication and Authorization)
身份驗證和授權通常結合使用,因為它們在授予系統(tǒng)訪問權限時起著重要且同樣重要的作用。
但是,它們具有不同的含義,并在驗證請求時應用不同的約束:
身份驗證 - 在授權之前;它是關于驗證收到的憑證;我們驗證用戶名和密碼是否與我們的應用程序識別的用戶名和密碼相匹配
授權 - 用于驗證成功通過身份驗證的用戶是否有權訪問應用程序的某個功能
我們可以自定義身份驗證和授權失敗處理,但是,在此應用程序中,我們將專注于身份驗證失敗。
3. Spring Security的AuthenticationFailureHandler
Spring Security提供了一個默認處理身份驗證失敗的組件。
但是,我們發(fā)現(xiàn)于默認行為不足以滿足實際要求的情況是很常見的。
如果是這種情況,我們可以創(chuàng)建自己的組件并通過實現(xiàn)AuthenticationFailureHandler接口提供我們想要的自定義行為:
public class CustomAuthenticationFailureHandler
implements AuthenticationFailureHandler {
private ObjectMapper objectMapper = new ObjectMapper();
@Override
public void onAuthenticationFailure(
HttpServletRequest request,
HttpServletResponse response,
AuthenticationException exception)
throws IOException, ServletException {
response.setStatus(HttpStatus.UNAUTHORIZED.value());
Map<String, Object> data = new HashMap<>();
data.put(
"timestamp",
Calendar.getInstance().getTime());
data.put(
"exception",
exception.getMessage());
response.getOutputStream()
.println(objectMapper.writeValueAsString(data));
}
}
默認情況下,Spring使用包含錯誤信息的請求參數(shù)將用戶重定向回登錄頁面。
在此應用程序中,我們將返回401響應,其中包含有關錯誤的信息以及錯誤發(fā)生的時間戳。
- DelegatingAuthenticationFailureHandler將AuthenticationException子類委托給不同的AuthenticationFailureHandler,這意味著我們可以為AuthenticationException的不同實例創(chuàng)建不同的行為
- ExceptionMappingAuthenticationFailureHandler根據(jù)AuthenticationException的完整類名將用戶重定向到特定的URL
- 無論AuthenticationException的類型如何,F(xiàn)orwardAuthenticationFailureHandler都會將用戶轉發(fā)到指定的URL
- SimpleUrlAuthenticationFailureHandler是默認使用的組件,如果指定,它會將用戶重定向到failureUrl;否則,它只會返回401響應
現(xiàn)在我們已經(jīng)創(chuàng)建了自定義AuthenticationFailureHandler,讓我們配置我們的應用程序并覆蓋Spring的默認處理程序:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration
extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth
.inMemoryAuthentication()
.withUser("baeldung")
.password("baeldung")
.roles("USER");
}
@Override
protected void configure(HttpSecurity http)
throws Exception {
http
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.formLogin()
.failureHandler(customAuthenticationFailureHandler());
}
@Bean
public AuthenticationFailureHandler customAuthenticationFailureHandler() {
return new CustomAuthenticationFailureHandler();
}
}
注意failureHandler()調用,我們可以告訴Spring使用我們的自定義組件而不是使用默認組件。
4.結論
在此示例中,我們使用Spring的AuthenticationFailureHandler接口自定義了應用程序的身份驗證失敗處理程序。
github源碼:https://github.com/eugenp/tutorials/tree/master/spring-security-mvc-login
相關文章
Java實現(xiàn)AOP功能的封裝與配置的小框架實例代碼
這篇文章主要介紹了Java實現(xiàn)AOP功能的封裝與配置的小框架實例代碼,分享了相關代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02
Spring?Boot中@Validated注解不生效問題匯總大全
這篇文章主要給大家介紹了關于Spring?Boot中@Validated注解不生效問題匯總的相關資料,@Validated注解是Spring框架中的一個注解,用于在方法參數(shù)上添加參數(shù)校驗規(guī)則,需要的朋友可以參考下2023-07-07
詳解Java數(shù)組擴容縮容與拷貝的實現(xiàn)和原理
這篇文章主要帶大家學習數(shù)組的擴容、縮容及拷貝,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-05-05

