国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

自定義SpringSecurity的身份驗證失敗怎么辦

發(fā)布時(shí)間:2021-09-27 17:50 來(lái)源:億速云 閱讀:0 作者:小新 欄目: 開(kāi)發(fā)技術(shù)

這篇文章給大家分享的是有關(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í),將立刻刪除涉嫌侵權內容。

午夜DJ在线观看高清在线视频WWW| 婷婷综合久久中文字幕蜜桃三电影| 无码Aⅴ在线观看| 亚洲日韩精品一区二区三区| 中文天堂在线最新版在线WWW| 欧美亚洲综合另类色妞网|