- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- 自定義SpringSecurity的身份驗證失敗怎么辦
這篇文章給大家分享的是有關(guān)自定義SpringSecurity的身份驗證失敗怎么辦的內容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
1.概述
在本快速教程中,我們將演示如何在Spring Boot應用程序中自定義Spring Security的身份驗證失敗處理。目標是使用表單登錄方法對用戶(hù)進(jìn)行身份驗證。
2.認證和授權(Authentication and Authorization)
身份驗證和授權通常結合使用,因為它們在授予系統訪(fǎng)問(wèn)權限時(shí)起著(zhù)重要且同樣重要的作用。
但是,它們具有不同的含義,并在驗證請求時(shí)應用不同的約束:
身份驗證 - 在授權之前;它是關(guān)于驗證收到的憑證;我們驗證用戶(hù)名和密碼是否與我們的應用程序識別的用戶(hù)名和密碼相匹配授權 - 用于驗證成功通過(guò)身份驗證的用戶(hù)是否有權訪(fǎng)問(wèn)應用程序的某個(gè)功能
我們可以自定義身份驗證和授權失敗處理,但是,在此應用程序中,我們將專(zhuān)注于身份驗證失敗。
3. Spring Security的AuthenticationFailureHandler
Spring Security提供了一個(gè)默認處理身份驗證失敗的組件。
但是,我們發(fā)現于默認行為不足以滿(mǎn)足實(shí)際要求的情況是很常見(jiàn)的。
如果是這種情況,我們可以創(chuàng )建自己的組件并通過(guò)實(shí)現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使用包含錯誤信息的請求參數將用戶(hù)重定向回登錄頁(yè)面。
在此應用程序中,我們將返回401響應,其中包含有關(guān)錯誤的信息以及錯誤發(fā)生的時(shí)間戳。
DelegatingAuthenticationFailureHandler將AuthenticationException子類(lèi)委托給不同的AuthenticationFailureHandler,這意味著(zhù)我們可以為AuthenticationException的不同實(shí)例創(chuàng )建不同的行為 ExceptionMappingAuthenticationFailureHandler根據AuthenticationException的完整類(lèi)名將用戶(hù)重定向到特定的URL 無(wú)論AuthenticationException的類(lèi)型如何,ForwardAuthenticationFailureHandler都會(huì )將用戶(hù)轉發(fā)到指定的URL SimpleUrlAuthenticationFailureHandler是默認使用的組件,如果指定,它會(huì )將用戶(hù)重定向到failureUrl;否則,它只會(huì )返回401響應
現在我們已經(jīng)創(chuàng )建了自定義AuthenticationFailureHandler,讓我們配置我們的應用程序并覆蓋Spring的默認處理程序:
@Configuration@EnableWebSecuritypublic 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使用我們的自定義組件而不是使用默認組件。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站