Commit 6c5f5e1ad4a5c1a1a14a9af1108ee65d5697775d

Authored by YunaiV
1 parent 09c8a91b

完善 refresh token 失效时,无法自动跳转回首页的问题,同时优化相关的提示

yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/OAuth2TokenServiceImpl.java
... ... @@ -21,7 +21,7 @@ import javax.annotation.Resource;
21 21 import java.util.Calendar;
22 22 import java.util.List;
23 23  
24   -import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
  24 +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception0;
25 25 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
26 26  
27 27 /**
... ... @@ -58,13 +58,13 @@ public class OAuth2TokenServiceImpl implements OAuth2TokenService {
58 58 // 查询访问令牌
59 59 OAuth2RefreshTokenDO refreshTokenDO = oauth2RefreshTokenMapper.selectByRefreshToken(refreshToken);
60 60 if (refreshTokenDO == null) {
61   - throw exception(GlobalErrorCodeConstants.BAD_REQUEST, "无效的刷新令牌");
  61 + throw exception0(GlobalErrorCodeConstants.BAD_REQUEST.getCode(), "无效的刷新令牌");
62 62 }
63 63  
64 64 // 校验 Client 匹配
65 65 OAuth2ClientDO clientDO = oauth2ClientService.validOAuthClientFromCache(clientId);
66 66 if (ObjectUtil.notEqual(clientId, refreshTokenDO.getClientId())) {
67   - throw exception(GlobalErrorCodeConstants.BAD_REQUEST, "刷新令牌的客户端编号不正确");
  67 + throw exception0(GlobalErrorCodeConstants.BAD_REQUEST.getCode(), "刷新令牌的客户端编号不正确");
68 68 }
69 69  
70 70 // 移除相关的访问令牌
... ... @@ -77,7 +77,7 @@ public class OAuth2TokenServiceImpl implements OAuth2TokenService {
77 77 // 已过期的情况下,删除刷新令牌
78 78 if (DateUtils.isExpired(refreshTokenDO.getExpiresTime())) {
79 79 oauth2AccessTokenMapper.deleteById(refreshTokenDO.getId());
80   - throw exception(GlobalErrorCodeConstants.UNAUTHORIZED, "刷新令牌已过期");
  80 + throw exception0(GlobalErrorCodeConstants.UNAUTHORIZED.getCode(), "刷新令牌已过期");
81 81 }
82 82  
83 83 // 创建访问令牌
... ... @@ -105,10 +105,10 @@ public class OAuth2TokenServiceImpl implements OAuth2TokenService {
105 105 public OAuth2AccessTokenDO checkAccessToken(String accessToken) {
106 106 OAuth2AccessTokenDO accessTokenDO = getAccessToken(accessToken);
107 107 if (accessTokenDO == null) {
108   - throw exception(GlobalErrorCodeConstants.UNAUTHORIZED, "访问令牌不存在");
  108 + throw exception0(GlobalErrorCodeConstants.UNAUTHORIZED.getCode(), "访问令牌不存在");
109 109 }
110 110 if (DateUtils.isExpired(accessTokenDO.getExpiresTime())) {
111   - throw exception(GlobalErrorCodeConstants.UNAUTHORIZED, "访问令牌已过期");
  111 + throw exception0(GlobalErrorCodeConstants.UNAUTHORIZED.getCode(), "访问令牌已过期");
112 112 }
113 113 return accessTokenDO;
114 114 }
... ...
yudao-ui-admin/src/utils/request.js
... ... @@ -86,9 +86,10 @@ service.interceptors.response.use( async res => {
86 86 setToken(refreshTokenRes.data)
87 87 requestList.forEach(cb => cb())
88 88 return service(res.config)
89   - } catch (e) {
90   - // 2.2 刷新失败,则只能执行登出操作
91   - // 为什么需要 catch 异常呢?刷新失败时,请求因为 Promise.reject 触发异常。
  89 + } catch (e) {// 为什么需要 catch 异常呢?刷新失败时,请求因为 Promise.reject 触发异常。
  90 + // 2.2 刷新失败,只回放队列的请求
  91 + requestList.forEach(cb => cb())
  92 + // 提示是否要登出。即不回放当前请求!不然会形成递归
92 93 return handleAuthorized();
93 94 } finally {
94 95 requestList = []
... ... @@ -98,12 +99,11 @@ service.interceptors.response.use( async res => {
98 99 // 添加到队列,等待刷新获取到新的令牌
99 100 return new Promise(resolve => {
100 101 requestList.push(() => {
101   - config.headers['Authorization'] = 'Bearer ' + getAccessToken() // 让每个请求携带自定义token 请根据实际情况自行修改
102   - resolve(service(config))
  102 + res.config.headers['Authorization'] = 'Bearer ' + getAccessToken() // 让每个请求携带自定义token 请根据实际情况自行修改
  103 + resolve(service(res.config))
103 104 })
104 105 })
105 106 }
106   - return handleAuthorized();
107 107 } else if (code === 500) {
108 108 Message({
109 109 message: msg,
... ... @@ -123,9 +123,13 @@ service.interceptors.response.use( async res => {
123 123 })
124 124 return Promise.reject(new Error(msg))
125 125 } else if (code !== 200) {
126   - Notification.error({
127   - title: msg
128   - })
  126 + if (msg === '无效的刷新令牌') { // hard coding:忽略这个提示,直接登出
  127 + console.log('无效的刷新令牌')
  128 + } else {
  129 + Notification.error({
  130 + title: msg
  131 + })
  132 + }
129 133 return Promise.reject('error')
130 134 } else {
131 135 return res.data
... ...