自定義Spring Security的身份驗證失敗處理方法
1.概述
在本快速教程中,我們將演示如何在Spring Boot應(yīng)用程序中自定義Spring Security的身份驗證失敗處理。目標是使用表單登錄方法對用戶進行身份驗證。
2.認證和授權(quán)(Authentication and Authorization)
身份驗證和授權(quán)通常結(jié)合使用,因為它們在授予系統(tǒng)訪問權(quán)限時起著重要且同樣重要的作用。
但是,它們具有不同的含義,并在驗證請求時應(yīng)用不同的約束:
身份驗證 - 在授權(quán)之前;它是關(guān)于驗證收到的憑證;我們驗證用戶名和密碼是否與我們的應(yīng)用程序識別的用戶名和密碼相匹配
授權(quán) - 用于驗證成功通過身份驗證的用戶是否有權(quán)訪問應(yīng)用程序的某個功能
我們可以自定義身份驗證和授權(quán)失敗處理,但是,在此應(yī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ù)將用戶重定向回登錄頁面。
在此應(yīng)用程序中,我們將返回401響應(yīng),其中包含有關(guān)錯誤的信息以及錯誤發(fā)生的時間戳。
- DelegatingAuthenticationFailureHandler將AuthenticationException子類委托給不同的AuthenticationFailureHandler,這意味著我們可以為AuthenticationException的不同實例創(chuàng)建不同的行為
- ExceptionMappingAuthenticationFailureHandler根據(jù)AuthenticationException的完整類名將用戶重定向到特定的URL
- 無論AuthenticationException的類型如何,F(xiàn)orwardAuthenticationFailureHandler都會將用戶轉(zhuǎn)發(fā)到指定的URL
- SimpleUrlAuthenticationFailureHandler是默認使用的組件,如果指定,它會將用戶重定向到failureUrl;否則,它只會返回401響應(yīng)
現(xiàn)在我們已經(jīng)創(chuàng)建了自定義AuthenticationFailureHandler,讓我們配置我們的應(yīng)用程序并覆蓋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()調(diào)用,我們可以告訴Spring使用我們的自定義組件而不是使用默認組件。
4.結(jié)論
在此示例中,我們使用Spring的AuthenticationFailureHandler接口自定義了應(yīng)用程序的身份驗證失敗處理程序。
github源碼:https://github.com/eugenp/tutorials/tree/master/spring-security-mvc-login
相關(guān)文章
FileUtils擴展readURLtoString讀取url內(nèi)容
這篇文章主要介紹了FileUtils擴展readURLtoString使用其支持讀取URL內(nèi)容為String,支持帶POST傳大量參數(shù),大家參考使用吧2014-01-01SpringBoot使用DevTools實現(xiàn)后端熱部署的過程詳解
在Spring Boot項目中,Spring Boot官方提供你了Devtools熱部署模塊,通過maven的方式導(dǎo)入就能使用,本文主要SpringBoot通過DevTools實現(xiàn)熱部署,感興趣的朋友一起看看吧2023-11-11Spring Boot RabbitMQ 延遲消息實現(xiàn)完整版示例
本篇文章主要介紹了Spring Boot RabbitMQ 延遲消息實現(xiàn)完整版示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05Java中數(shù)組array和列表list相互轉(zhuǎn)換
這篇文章主要介紹了Java中數(shù)組array和列表list相互轉(zhuǎn)換,在Java中,可以將數(shù)組(array)和列表(list)相互轉(zhuǎn)換,但需要注意一些細節(jié)和限制,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-09-09