在认证失败或者是授权失败的情况下也能和我们的接口一样返回相同结构的json
。
这样可以让前端能对响应进行统一的处理。要实现这个功能我们需要知道SpringSecurity
的异常处理机制。
例如:当我们登录时
如果登录失败,返回的JSON
如下
1 | { |
如果登录成功,返回的JSON
如下
1 | { |
为了统一JSON
的格式
在SpringSecurity
中,如果我们在认证或者授权的过程中出现了异常会被ExceptionTranslationFilter
捕获到。在ExceptionTranslationFilter
中会去判断是认证失败还是授权失败出现的异常。
注意:在认证授权的过程中发生的任何异常都会被抛出,也就是说在控制台是看不见的。当程序在认证授权过程中出问题时,需要DEBUG去查看,然后
try...catch
捕获异常,看看是什么原因报错。
如果是认证过程中出现的异常会被封装成
AuthenticationException
然后调用AuthenticationEntryPoint
对象的方法去进行异常处理。如果是授权过程中出现的异常会被封装成
AccessDeniedException
然后调用AccessDeniedHandler
对象的方法去进行异常处理。
自定义异常处理
如果我们需要自定义异常处理,我们只需要自定义AuthenticationEntryPoint
和AccessDeniedHandler
然后配置给SpringSecurity
即可。
创建一个包
handler
自定义实现类
实现
AuthenticationEntryPoint
接口【处理认证过程中】首先需要定义一个响应对象,
new ResponseResult(403, "权限不足");
认证失败响应码可以是401,或者是
HttpStatus.UNAUTHORIZED.value()
。将对象序列化为
JSON
字符串JSON.toJSONString()
使用
WebUtils
工具类,将字符串写到response
中
1
2
3
4
5
6
7
8
9
10
11
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
ResponseResult result = new ResponseResult(401, "认证失败请重新登录");
String json = JSON.toJSONString(result);
WebUtils.renderString(response,json);
}
}实现
AccessDeniedHandler
接口【处理授权过程中】过程和上面一样
权限不足响应码可以是403,或者是
HttpStatus.FORBIDDEN.value()
。1
2
3
4
5
6
7
8
9
10
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
ResponseResult result = new ResponseResult(403, "权限不足");
String jsonString = JSON.toJSONString(result);
WebUtils.renderString(httpServletResponse,jsonString);
}
}
配置
SpringSecurity
工具类1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class SecurityConfig extends WebSecurityConfigurerAdapter{
private AccessDeniedHandler accessDeniedHandler;
private AuthenticationEntryPoint authenticationEntryPoint;
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
protected void configure(HttpSecurity http) throws Exception {
//配置异常处理器。
http.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint)
.accessDeniedHandler(accessDeniedHandler);
}
}
WebUtils
工具类
1 | public class WebUtils |
还拿之前的举例
例如:当我们登录时
如果登录失败,返回的JSON
如下
1 | { |
如果登录成功,返回的JSON
如下
1 | { |
__END__