当前位置:网站首页>Custom failure handling
Custom failure handling
2022-06-09 10:20:00 【Leon_ Jinhai_ Sun】
We also hope that in case of authentication failure or authorization failure, we can return the same structure as our interface json, This allows the front end to handle the response uniformly . To achieve this function, we need to know SpringSecurity Exception handling mechanism of .
stay SpringSecurity in , If an exception occurs in the authentication process ExceptionTranslationFilter Capture to . stay ExceptionTranslationFilter It will judge whether the authentication fails or the authorization fails .
If an exception occurs during the authentication process, it will be encapsulated as AuthenticationException And then call AuthenticationEntryPoint Object to handle exceptions .
If it is an exception in the authorization process, it will be encapsulated as AccessDeniedException And then call AccessDeniedHandler Object to handle exceptions .
So if we need to customize exception handling , We just need to customize AuthenticationEntryPoint and AccessDeniedHandler Then configure it to SpringSecurity that will do .
① Custom implementation class
@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(), " Insufficient authority ");
String json = JSON.toJSONString(result);
WebUtils.renderString(response,json);
}
}@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(), " Authentication failed. Please login again ");
String json = JSON.toJSONString(result);
WebUtils.renderString(response,json);
}
}
② Configure to SpringSecurity
Inject the corresponding processor first
@Autowired
private AuthenticationEntryPoint authenticationEntryPoint;
@Autowired
private AccessDeniedHandler accessDeniedHandler; And then we can use it HttpSecurity Object to configure .
http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint).
accessDeniedHandler(accessDeniedHandler);边栏推荐
- 从数据库查询权限信息
- Composition of IT resources accessible anywhere, anytime
- Interview question 10.03 Search rotation array
- Openstack explanation (XX) -- neutron node principle
- Why do we want to be young when we grow up?
- 机器学习笔记 - 使用Keras和深度学习进行乳腺癌分类
- 損失 3 億美元後,IBM 宣布退出俄羅斯!
- Openstack explanation (13) -- Grace keystone setup and startup
- 31. 下一个排列
- Sword finger offer II 061 And the minimum k-number pair large top heap method
猜你喜欢

Machine learning notes - Interpretation of u-net papers

106. construct binary tree from middle order and post order traversal sequence
![[genius_platform software platform development] lesson 36: definition of maximum value macro of built-in data type](/img/f1/ca57934507bb0758b8bb0a52606a10.jpg)
[genius_platform software platform development] lesson 36: definition of maximum value macro of built-in data type

循环神经网络理论——【torch学习笔记】

失业潮?元宇宙开拓全新的就业机会

Unsupportedoperationexception exception resolution

Cve-2019-0192 Apache Solr remote deserialization Code Execution Vulnerability harm

Introduction to scientific research: how can you improve your scientific research ability when your tutor is completely free for nursing?
![[genius_platform software platform development] lecture 37: network card hybrid mode and raw socket](/img/bf/880fbf4122b66723b6e17c6d9d97c9.jpg)
[genius_platform software platform development] lecture 37: network card hybrid mode and raw socket

損失 3 億美元後,IBM 宣布退出俄羅斯!
随机推荐
登出成功处理器
損失 3 億美元後,IBM 宣布退出俄羅斯!
机器学习笔记 - R语言学习入门系列一
Bidding solution for battery charging and discharging equipment
n-grams语言模型——【torch学习笔记】
screen越看越好看
Web版SSH客戶端Sshwifty
电池充放电设备招投标解决方案
Today in history: PHP is publicly released; IPhone 4 comes out; The birth of the father of the World Wide Web
基于云的 LDAP 如何解救传统 LDAP?
2289. 使数组按非递减顺序排列
77.5% of the world's websites are using PHP, the "best language in the world"!
Authentication successful processor
山东济南的名人颜廷利东方著名哲学家及其思想,中国需要这样的思想家
什么样的数字藏品平台才是好平台?
Openstack explanation (XX) -- neutron node principle
15 must know MySQL index failure scenarios, stop stepping on the pit!
Implementing recurrent neural networks from scratch -- [torch learning notes]
Machine learning housing rental price forecasting: exploratory data analysis + Feature Engineering + modeling + reporting
序列模型——【torch学习笔记】