七仔的博客

七仔的博客GithubPages分博

0%

对Jeecg-Boot中Token失效返回格式的调整

前端在对接过程中发现token失效后返回直接返回500,而不是返回200然后在返回体中设置自定义返回码,需要后端调整为返回200ok,请求体中的code为自定义的。

对Jeecg-Boot中Token失效返回格式的调整

一、前言

使用 Jeecg-Boot 的Spring Boot后端模块开发,前端未使用Jeecg-Boot的配套前端。

二、问题

前端在对接过程中发现token失效后返回直接返回500,而不是返回200然后在返回体中设置自定义返回码,需要后端调整为返回200ok,请求体中的code为自定义的。

三、过程

发现请求中带失效token后端会报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
org.apache.shiro.authc.AuthenticationException: Token失效,请重新登录!
at org.jeecg.modules.shiro.authc.ShiroRealm.checkUserTokenIsEffect(ShiroRealm.java:129)
at org.jeecg.modules.shiro.authc.ShiroRealm.doGetAuthenticationInfo(ShiroRealm.java:101)
at org.apache.shiro.realm.AuthenticatingRealm.getAuthenticationInfo(AuthenticatingRealm.java:571)
at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doSingleRealmAuthentication(ModularRealmAuthenticator.java:180)
at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doAuthenticate(ModularRealmAuthenticator.java:267)
at org.apache.shiro.authc.AbstractAuthenticator.authenticate(AbstractAuthenticator.java:198)
at org.apache.shiro.mgt.AuthenticatingSecurityManager.authenticate(AuthenticatingSecurityManager.java:106)
at org.apache.shiro.mgt.DefaultSecurityManager.login(DefaultSecurityManager.java:274)
at org.apache.shiro.subject.support.DelegatingSubject.login(DelegatingSubject.java:260)
at org.jeecg.modules.shiro.authc.aop.JwtFilter.executeLogin(JwtFilter.java:57)
at org.jeecg.modules.shiro.authc.aop.JwtFilter.isAccessAllowed(JwtFilter.java:40)
at org.apache.shiro.web.filter.AccessControlFilter.onPreHandle(AccessControlFilter.java:162)
at org.apache.shiro.web.filter.PathMatchingFilter.isFilterChainContinued(PathMatchingFilter.java:203)
at org.apache.shiro.web.filter.PathMatchingFilter.preHandle(PathMatchingFilter.java:178)
at org.jeecg.modules.shiro.authc.aop.JwtFilter.preHandle(JwtFilter.java:78)

其中ShiroRealm和JwtFilter为jeecg-boot框架源码,其余为引用包代码,可以看到框架在ShiroRealm中抛出了AuthenticationException异常,之后一层层接收异常直到JwtFilter类的isAccessAllowed函数中处理异常,但是JwtFilter对异常的处理是再次抛出AuthenticationException,黑人问号.jpg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 执行登录认证
*
* @param request
* @param response
* @param mappedValue
* @return
*/
@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue){
try {
executeLogin(request, response);
return true;
} catch (Exception e) {
throw new AuthenticationException("Token失效,请重新登录", e);
}
}

但是我们可以发现同样在JwtFilter类中有一个preHandle函数,我们可以在preHandle函数中对

1
return super.preHandle(request, response);

进行异常处理,修改为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
try {
return super.preHandle(request, response);
}
catch (AuthenticationException e){
httpServletResponse.setStatus(HttpStatus.OK.value());
JSONObject data = new JSONObject();
data.put("code", 2333);
data.put("message", "Token失效,请重新登录");
data.put("success", false);
data.put("timestamp", new Date().getTime());
/**获取OutputStream输出流*/
OutputStream outputStream = response.getOutputStream();
/**设置json返回格式*/
((HttpServletResponse) response).setHeader("content-type", "application/json");
/**将字符转换成字节数组,指定以UTF-8编码进行转换*/
byte[] dataByteArr = data.toJSONString().getBytes(StandardCharsets.UTF_8);
/**使用OutputStream流向客户端输出字节数组*/
outputStream.write(dataByteArr);
return false;
}

经过postman测试为返回200ok,状态码2333

此为博主副博客,留言请去主博客,转载请注明出处:https://www.baby7blog.com/myBlog/68.html

欢迎关注我的其它发布渠道