Spring?Security自定義失敗處理器問題
在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,這樣可以讓前端能對響應(yīng)進(jìn)行統(tǒng)一的處理。要實(shí)現(xiàn)這個功能我們需要知道SpringSecurity的異常處理機(jī)制。
在SpringSecurity中,如果我們在認(rèn)證或者授權(quán)的過程中出現(xiàn)了異常會被ExceptionTranslationFilter捕獲到。在ExceptionTranslationFilter中會去判斷是認(rèn)證失敗還是授權(quán)失敗出現(xiàn)的異常。
- 如果是認(rèn)證過程中出現(xiàn)的異常會被封裝成AuthenticationException然后調(diào)用AuthenticationEntryPoint對象的方法去進(jìn)行異常處理。
- 如果是授權(quán)過程中出現(xiàn)的異常會被封裝成AccessDeniedException然后調(diào)用AccessDeniedHandler對象的方法去進(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)證失敗請重新登錄"); 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保護(hù) .csrf(csrf -> csrf.disable()) // 設(shè)置會話創(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(); }
注意事項
- 確保你的認(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)失敗時,你可能希望返回JSON而不是純文本響應(yīng),特別是在構(gòu)建RESTful API時。為此,你可以使用
@ResponseBody
注解或ResponseEntity
類來構(gòu)建JSON響應(yīng)。
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于springboot集成swagger及knife4j的增強(qiáng)問題
這篇文章主要介紹了springboot集成swagger以及knife4j的增強(qiáng),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03springboot使用redisTemplate操作lua腳本
本文主要介紹了springboot使用redisTemplate操作lua腳本,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Java多線程通信wait()和notify()代碼實(shí)例
這篇文章主要介紹了Java多線程通信wait()和notify()代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04Java中ByteArrayOutputStream亂碼問題解決
本文主要介紹了Java中ByteArrayOutputStream亂碼問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Spring從@Aspect到Advisor使用演示實(shí)例
這篇文章主要介紹了Spring從@Aspect到Advisor使用演示實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02IDEA中程序包Org.Springframework.Boot不存在問題及解決
這篇文章主要介紹了IDEA中程序包Org.Springframework.Boot不存在問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07