Spring Security自定義失敗處理器問題
Spring Security自定義失敗處理器
我們還希望在認證失敗或者是授權(quán)失敗的情況下也能和我們的接口一樣返回相同結(jié)構(gòu)的json,這樣可以讓前端能對響應(yīng)進行統(tǒng)一的處理。要實現(xiàn)這個功能我們需要知道SpringSecurity的異常處理機制。
在SpringSecurity中,如果我們在認證或者授權(quán)的過程中出現(xiàn)了異常會被ExceptionTranslationFilter捕獲到。在ExceptionTranslationFilter中會去判斷是認證失敗還是授權(quán)失敗出現(xiàn)的異常。
- 如果是認證過程中出現(xiàn)的異常會被封裝成AuthenticationException然后調(diào)用AuthenticationEntryPoint對象的方法去進行異常處理。
- 如果是授權(quán)過程中出現(xiàn)的異常會被封裝成AccessDeniedException然后調(diào)用AccessDeniedHandler對象的方法去進行異常處理。
所以如果我們需要自定義異常處理,我們只需要自定義AuthenticationEntryPoint和AccessDeniedHandler然后配置SpringSecurity即可。
在Spring Security中,你可以通過實現(xiàn)特定的接口來自定義認證失敗處理器(AuthenticationFailureHandler
)和授權(quán)失敗處理器(AccessDeniedHandler
)。以下是如何分別自定義它們的步驟:
一、自定義認證失敗處理器(AuthenticationFailureHandler)
- 1.1創(chuàng)建自定義的
AuthenticationFailureHandler
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.security.core.AuthenticationException; import org.springframework.security.web.authentication.AuthenticationFailureHandler; public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler { @Override public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { // 在這里處理認證失敗的情況 response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); response.getWriter().write("登錄失敗: " + exception.getMessage()); } }
- 1.2在Spring Security配置中使用你的自定義認證失敗處理器
@Override protected void configure(HttpSecurity http) throws Exception { http // ... 其他配置 ... .formLogin() .failureHandler(new CustomAuthenticationFailureHandler()) // 設(shè)置自定義認證失敗處理器 // ... 其他表單登錄配置 ... // ... 其他HTTP安全配置 ... ; }
二、自定義授權(quán)失敗處理器(AccessDeniedHandler)
- 2.1創(chuàng)建自定義的
AccessDeniedHandler
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.security.access.AccessDeniedException; import org.springframework.security.web.access.AccessDeniedHandler; public class CustomAccessDeniedHandler implements AccessDeniedHandler { @Override public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException { // 在這里處理授權(quán)失敗的情況 response.setStatus(HttpServletResponse.SC_FORBIDDEN); response.getWriter().write("權(quán)限不足: " + accessDeniedException.getMessage()); } }
- 2.2在Spring Security配置中使用你的自定義授權(quán)失敗處理器
@Override protected void configure(HttpSecurity http) throws Exception { http // ... 其他配置 ... .exceptionHandling() .accessDeniedHandler(new CustomAccessDeniedHandler()) // 設(shè)置自定義授權(quán)失敗處理器 // ... 其他異常處理配置 ... // ... 其他HTTP安全配置 ... ; }
三、實戰(zhàn)
我們還希望在認證失敗或者是授權(quán)失敗的情況下也能和我們的接口一樣返回相同結(jié)構(gòu)的json,這樣可以讓前端能對響應(yīng)進行統(tǒng)一的處理。要實現(xiàn)這個功能我們需要知道SpringSecurity的異常處理機制。
在SpringSecurity中,如果我們在認證或者授權(quán)的過程中出現(xiàn)了異常會被ExceptionTranslationFilter捕獲到。在ExceptionTranslationFilter中會去判斷是認證失敗還是授權(quán)失敗出現(xiàn)的異常。
- 如果是認證過程中出現(xiàn)的異常會被封裝成AuthenticationException然后調(diào)用AuthenticationEntryPoint對象的方法去進行異常處理。
- 如果是授權(quán)過程中出現(xiàn)的異常會被封裝成AccessDeniedException然后調(diào)用AccessDeniedHandler對象的方法去進行異常處理。
所以如果我們需要自定義異常處理,我們只需要自定義AuthenticationEntryPoint和AccessDeniedHandler然后配置SpringSecurity即可。
(com.sangeng.handler)
- 自定義認證失敗處理器:
@Component public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint { @Override public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { ResponseResult result = new ResponseResult(HttpStatus.UNAUTHORIZED.value(), "認證失敗請重新登錄"); String json = JSON.toJSONString(result); WebUtils.renderString(response, json); } }
- 自定義授權(quán)失敗處理器:
@Component public class AccessDeniedHandlerImpl implements AccessDeniedHandler { @Override public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException { ResponseResult result = new ResponseResult(HttpStatus.FORBIDDEN.value(), "權(quán)限不足"); String json = JSON.toJSONString(result); WebUtils.renderString(response, json); } }
- 修改配置類:
@Configuration //配置類 @EnableWebSecurity // 開啟Spring Security的功能 代替了 implements WebSecurityConfigurerAdapter public class SecurityConfig { @Autowired AuthenticationConfiguration authenticationConfiguration;//獲取AuthenticationManager @Autowired JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter; @Autowired AccessDeniedHandlerImpl accessDeniedHandler; @Autowired AuthenticationEntryPointImpl authenticationEntryPoint; @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Bean public AuthenticationManager authenticationManagerBean() throws Exception { return authenticationConfiguration.getAuthenticationManager(); } /** * 配置Spring Security的過濾鏈。 * * @param http 用于構(gòu)建安全配置的HttpSecurity對象。 * @return 返回配置好的SecurityFilterChain對象。 * @throws Exception 如果配置過程中發(fā)生錯誤,則拋出異常。 */ @Bean SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http // 禁用CSRF保護 .csrf(csrf -> csrf.disable()) // 設(shè)置會話創(chuàng)建策略為無狀態(tài) .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) // 配置授權(quán)規(guī)則 指定user/login路徑.允許匿名訪問(未登錄可訪問已登陸不能訪問). 其他路徑需要身份認證 .authorizeHttpRequests(auth -> auth.requestMatchers("/user/login").anonymous().anyRequest().authenticated()) //開啟跨域訪問 .cors(AbstractHttpConfigurer::disable) // 添加JWT認證過濾器 .addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class) // 配置異常處理 .exceptionHandling(exception -> exception.accessDeniedHandler(accessDeniedHandler).authenticationEntryPoint(authenticationEntryPoint)); // 構(gòu)建并返回安全過濾鏈 return http.build(); }
注意事項
- 確保你的認證和授權(quán)失敗處理器都正確實現(xiàn)了相應(yīng)的接口,并且正確設(shè)置了它們的狀態(tài)碼和響應(yīng)內(nèi)容。
- 你可以將你的自定義處理器作為Spring Bean注入到你的配置中,而不是直接在配置方法中創(chuàng)建新的實例。這樣可以更好地管理你的Spring應(yīng)用程序中的bean。
- 在處理認證和授權(quán)失敗時,你可能希望返回JSON而不是純文本響應(yīng),特別是在構(gòu)建RESTful API時。為此,你可以使用
@ResponseBody
注解或ResponseEntity
類來構(gòu)建JSON響應(yīng)。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于Jackson實現(xiàn)API接口數(shù)據(jù)脫敏的示例詳解
用戶的一些敏感數(shù)據(jù),例如手機號、郵箱、身份證等信息,在數(shù)據(jù)庫以明文存儲,但在接口返回數(shù)據(jù)給瀏覽器(或三方客戶端)時,希望對這些敏感數(shù)據(jù)進行脫敏,所以本文就給大家介紹以惡如何利用Jackson實現(xiàn)API接口數(shù)據(jù)脫敏,需要的朋友可以參考下2023-08-08Java數(shù)據(jù)結(jié)構(gòu)之隊列(動力節(jié)點Java學(xué)院整理)
隊列(Queue)是只允許在一端進行插入,而在另一端進行刪除的運算受限的線性表。 這篇文章詳細給大家介紹了java數(shù)據(jù)結(jié)構(gòu)之隊列,感興趣的朋友跟隨小編一起學(xué)習(xí)吧2017-04-04詳解SpringBoot+Dubbo集成ELK實戰(zhàn)
這篇文章主要介紹了詳解SpringBoot+Dubbo集成ELK實戰(zhàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10基于Servlet實現(xiàn)技術(shù)問答網(wǎng)站系統(tǒng)
這篇文章主要為大家詳細介紹了基于Servlet實現(xiàn)技術(shù)問答網(wǎng)站系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04Java實現(xiàn)的簡單字符串反轉(zhuǎn)操作示例
這篇文章主要介紹了Java實現(xiàn)的簡單字符串反轉(zhuǎn)操作,結(jié)合實例形式分別描述了java遍歷逆序輸出以及使用StringBuffer類的reverse()方法兩種字符串反轉(zhuǎn)操作技巧,需要的朋友可以參考下2018-08-08