1. 程式人生 > >SpringSecurity系列之自定義登入驗證成功與失敗的結果處理

SpringSecurity系列之自定義登入驗證成功與失敗的結果處理

一、需要自定義登入結果的場景

在我之前的文章中,做過登入驗證流程的原始碼解析。其中比較重要的就是

  • 當我們登入成功的時候,是由AuthenticationSuccessHandler進行登入結果處理,預設跳轉到defaultSuccessUrl配置的路徑對應的資源頁面(一般是首頁index.html)。
  • 當我們登入失敗的時候,是由AuthenticationfailureHandler進行登入結果處理,預設跳轉到failureUrl配置的路徑對應的資源頁面(一般是登入頁login.html)。

但是在web應用開發過程中需求是千變萬化的,有時需要我們針對登入結果做個性化處理,比如:

  • 我們希望不同的人登陸之後,看到不同的首頁
  • 我們應用是前後端分離的,驗證響應結果是JSON格式資料,而不是頁面跳轉

以上的這些情況,使用Spring Security作為安全框架的時候,都需要我們使用本節學到的知識進行自定義的登入驗證結果處理。

二、自定義登陸成功的結果處理

為了滿足上面的需求,我們該如何去做呢?下面一小節我們來說明一下。AuthenticationSuccessHandler介面是Security提供的認證成功處理器介面,我們只需要去實現它即可。但是通常來說,我們不會直接去實現AuthenticationSuccessHandler介面,而是繼承SavedRequestAwareAuthenticationSuccessHandler 類,這個類會記住使用者上一次請求的資源路徑,比如:使用者請求books.html,沒有登陸所以被攔截到了登入頁,當你萬成登陸之後會自動跳轉到books.html,而不是主頁面。

@Component
public class MyAuthenticationSuccessHandler 
                        extends SavedRequestAwareAuthenticationSuccessHandler {

    //在application配置檔案中配置登陸的型別是JSON資料響應還是做頁面響應
    @Value("${spring.security.logintype}")
    private String loginType;

    private  static ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, 
                                        HttpServletResponse response, 
                                        Authentication authentication) 
                                        throws ServletException, IOException {

        if (loginType.equalsIgnoreCase("JSON")) {
            response.setContentType("application/json;charset=UTF-8");
            response.getWriter().write(objectMapper.writeValueAsString(AjaxResponse.success()));
        } else {
            // 會幫我們跳轉到上一次請求的頁面上
            super.onAuthenticationSuccess(request, response, authentication);
        }
    }
}
  • 在上面的自定義登陸成功處理中,既適應JSON前後端分離的應用登入結果處理,也適用於模板頁面跳轉應用的登入結果處理
  • ObjectMapper 是Spring Boot預設整合的JSON資料處理類庫Jackson中的類。
  • AjaxResponse是一個自定義的通用的JSON資料介面響應類。

三、自定義登入失敗的結果處理

這裡我們同樣沒有直接實現AuthenticationFailureHandler介面,而是繼承SimpleUrlAuthenticationFailureHandler 類。該類中預設實現了登入驗證失敗的跳轉邏輯,即登陸失敗之後回到登入頁面。我們可以利用這一點簡化我們的程式碼。

@Component
public class MyAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {

    //在application配置檔案中配置登陸的型別是JSON資料響應還是做頁面響應
    @Value("${spring.security.logintype}")
    private String loginType;

    private  static ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void onAuthenticationFailure(HttpServletRequest request,
                                        HttpServletResponse response, 
                                        AuthenticationException exception) 
                                        throws IOException, ServletException {

        if (loginType.equalsIgnoreCase("JSON")) {
            response.setContentType("application/json;charset=UTF-8");
            response.getWriter().write(
                    objectMapper.writeValueAsString(
                            AjaxResponse.error(
                                    new CustomException(
                                        CustomExceptionType.USER_INPUT_ERROR,
                                        "使用者名稱或密碼存在錯誤,請檢查後再次登入"))));
        } else {
            response.setContentType("text/html;charset=UTF-8");
            super.onAuthenticationFailure(request, response, exception);
        }

    }
}
  • 在上面的自定義登陸失敗處理中,既適應JSON前後端分離的應用登入失敗結果處理,也適用於模板頁面跳轉應用的登入失敗結果處理
  • 登陸失敗之後,將預設跳轉到預設的failureUrl,即登入介面。

四、配置SecurityConfig

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    private MyAuthenticationSuccessHandler myAuthenticationSuccessHandler;

    @Resource
    private MyAuthenticationFailureHandler myAuthenticationFailureHandler;

   @Override
   protected void configure(HttpSecurity http) throws Exception {
       http.csrf().disable() //禁用跨站csrf攻擊防禦,後面的章節會專門講解
           .formLogin()
           .successHandler(myAuthenticationSuccessHandler)
           .failureHandler(myAuthenticationFailureHandler)
           .defaultSuccessUrl("/index")//登入認證成功後預設轉跳的路徑
           .failureUrl("/login.html") //登入認證是被跳轉頁面
}
  • 將自定義的AuthenticationSuccessHandler和AuthenticationFailureHandler注入到Spring Security配置類中
  • 使用fromlogin模式,配置successHandler和failureHandler。
  • 並且配置defaultSuccessUrl和failureUrl

    期待您的關注

  • 博主最近新寫了一本書:《手摸手教您學習SpringBoot系列-16章97節》
  • 本文轉載註明出處(必須帶連線,不能只轉文字):字母哥部落格。