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