SpringBoot2.0實戰(26)整合SpringSecurity之JSON格式交互

SpringBoot2.0實戰(26)整合SpringSecurity之JSON格式交互

在前端的文章中,我們實現了使用 SpringSecurity 實現登錄鑑權,並使用數據庫存儲用戶信息,實現登錄鑑權

登錄頁都是使用 SpringSecurity 提供的默認登錄,入參為 username 及 password,前端通過 form 表單提交請求,後臺使用 request.getParameter() 獲取填寫數據。

目前,在實際項目中,更多的企業選擇使用前後端分離的項目架構,前後端數據交互選擇使用 application/json 方式進行傳遞,同時,在實際的項目中,擁有更多形式的登錄,比如手機號/短信驗證碼,不能使用單一的 usernamepassword 結構進行處理,需要自行定義入參結構。

目標

本章將整合 SpringSecurity 實現使用自定義格式進行登錄,並使用 json 方式進行前後端交互。

準備工作

創建用戶表 user、角色表 role、用戶角色關係表 user_role

SpringBoot2.0實戰(26)整合SpringSecurity之JSON格式交互

操作步驟

添加依賴

引入 Spring Boot Starter 父工程

SpringBoot2.0實戰(26)整合SpringSecurity之JSON格式交互

添加 springSecurity 及 mybatisPlus 的依賴,添加後的整體依賴如下

SpringBoot2.0實戰(26)整合SpringSecurity之JSON格式交互

配置

配置一下數據源

SpringBoot2.0實戰(26)整合SpringSecurity之JSON格式交互

編碼

實體類

角色實體類 Role,實現權限接口 GrantedAuthority

SpringBoot2.0實戰(26)整合SpringSecurity之JSON格式交互

用戶實體類 user,實現權限接口 UserDetails,主要方法是 getAuthorities,用於獲取用戶的角色列表

SpringBoot2.0實戰(26)整合SpringSecurity之JSON格式交互

用戶角色關係實體

SpringBoot2.0實戰(26)整合SpringSecurity之JSON格式交互

Repository 層

分別為三個實體類添加 Mapper

SpringBoot2.0實戰(26)整合SpringSecurity之JSON格式交互

實現 UserDetailsService 接口

UserDetailsService 是 SpringSecurity 提供的登陸時用於根據用戶名獲取用戶信息的接口

SpringBoot2.0實戰(26)整合SpringSecurity之JSON格式交互

自定義登錄參數格式

SpringBoot2.0實戰(26)整合SpringSecurity之JSON格式交互

自定義登錄過濾器

繼承 SpringSecurity 提供的 AbstractAuthenticationProcessingFilter 類,實現 attemptAuthentication 方法,用於登錄校驗。

本例中,模擬前端使用 json 格式傳遞參數,所以通過 objectMapper.readValue 的方式從流中獲取入參,之後借用了用戶名密碼登錄的校驗,並返回權限對象

SpringBoot2.0實戰(26)整合SpringSecurity之JSON格式交互

自定義登陸成功後處理

實現 SpringSecurity 提供的 AuthenticationSuccessHandler 接口,使用 JSON 格式返回

SpringBoot2.0實戰(26)整合SpringSecurity之JSON格式交互

自定義登陸失敗後處理

實現 SpringSecurity 提供的 AuthenticationFailureHandler 接口,使用 JSON 格式返回

SpringBoot2.0實戰(26)整合SpringSecurity之JSON格式交互

自定義權限校驗失敗後處理

登陸成功之後,訪問接口之前 SpringSecurity 會進行鑑權,如果沒有訪問權限,需要對返回進行處理。實現 SpringSecurity 提供的 AccessDeniedHandler 接口,使用 JSON 格式返回

SpringBoot2.0實戰(26)整合SpringSecurity之JSON格式交互

自定義未登錄後處理

實現 SpringSecurity 提供的 AuthenticationEntryPoint 接口,使用 JSON 格式返回

SpringBoot2.0實戰(26)整合SpringSecurity之JSON格式交互

註冊

在 configure 方法中調用 addFilterAfter 方法,將自定義的 jsonAuthenticationFilter 註冊進 SpringSecurity 的過濾器鏈中。

SpringBoot2.0實戰(26)整合SpringSecurity之JSON格式交互

啟動類

SpringBoot2.0實戰(26)整合SpringSecurity之JSON格式交互

驗證結果

初始化數據

SpringBoot2.0實戰(26)整合SpringSecurity之JSON格式交互

源碼地址

本章源碼 : https://gitee.com/gongm_24/spring-boot-tutorial.git

擴展

前面的示例只是使用了一個自定義結構去接收前端表單數據,但是處理還是使用的 username/password 那一套,調用 getAuthenticationManager().authenticate() 去進行入參的校驗,AuthenticationManager 中維護一個 AuthenticationProvider 列表,每一個 AuthenticationProvider 會支持一個 Token 類型,username/password 則是使用的 UsernamePasswordAuthenticationToken,併為之提供了 AbstractUserDetailsAuthenticationProvider 進行登錄校驗,AbstractUserDetailsAuthenticationProvider 的實現類就是 DaoAuthenticationProvider,註冊的時候通過 auth.userDetailsService(userService) 方法調用,會將 DaoAuthenticationProvider 加入至 AuthenticationManager 中維護的 AuthenticationProvider 列表中。

如果需要完全自定義入參結構,按如下操作即可

  1. 自定義 AbstractAuthenticationToken 實現類
  2. 自定義 AuthenticationProvider 實現類,並支持自定義的 AbstractAuthenticationToken 實現類,具體實現可以參考 AbstractUserDetailsAuthenticationProvider
  3. 自定義 AuthenticationProcessingFilter 實現類,需要定義請求地址,接收入參,組裝 Token,調用 getAuthenticationManager().authenticate()
  4. 將自定義 AuthenticationProvider 實現類加入至 AuthenticationManager 中維護的 AuthenticationProvider 列表中。
  5. 將自定義 AuthenticationProcessingFilter 實現類加入至過濾器列表中,置於 UsernamePasswordAuthenticationFilter 之後。


分享到:


相關文章: