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

pull/2/head
YunaiV 2022-05-13 20:28:56 +08:00
parent 09c8a91b6a
commit 6c5f5e1ad4
2 changed files with 19 additions and 15 deletions

View File

@ -21,7 +21,7 @@ import javax.annotation.Resource;
import java.util.Calendar;
import java.util.List;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception0;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
/**
@ -58,13 +58,13 @@ public class OAuth2TokenServiceImpl implements OAuth2TokenService {
// 查询访问令牌
OAuth2RefreshTokenDO refreshTokenDO = oauth2RefreshTokenMapper.selectByRefreshToken(refreshToken);
if (refreshTokenDO == null) {
throw exception(GlobalErrorCodeConstants.BAD_REQUEST, "无效的刷新令牌");
throw exception0(GlobalErrorCodeConstants.BAD_REQUEST.getCode(), "无效的刷新令牌");
}
// 校验 Client 匹配
OAuth2ClientDO clientDO = oauth2ClientService.validOAuthClientFromCache(clientId);
if (ObjectUtil.notEqual(clientId, refreshTokenDO.getClientId())) {
throw exception(GlobalErrorCodeConstants.BAD_REQUEST, "刷新令牌的客户端编号不正确");
throw exception0(GlobalErrorCodeConstants.BAD_REQUEST.getCode(), "刷新令牌的客户端编号不正确");
}
// 移除相关的访问令牌
@ -77,7 +77,7 @@ public class OAuth2TokenServiceImpl implements OAuth2TokenService {
// 已过期的情况下,删除刷新令牌
if (DateUtils.isExpired(refreshTokenDO.getExpiresTime())) {
oauth2AccessTokenMapper.deleteById(refreshTokenDO.getId());
throw exception(GlobalErrorCodeConstants.UNAUTHORIZED, "刷新令牌已过期");
throw exception0(GlobalErrorCodeConstants.UNAUTHORIZED.getCode(), "刷新令牌已过期");
}
// 创建访问令牌
@ -105,10 +105,10 @@ public class OAuth2TokenServiceImpl implements OAuth2TokenService {
public OAuth2AccessTokenDO checkAccessToken(String accessToken) {
OAuth2AccessTokenDO accessTokenDO = getAccessToken(accessToken);
if (accessTokenDO == null) {
throw exception(GlobalErrorCodeConstants.UNAUTHORIZED, "访问令牌不存在");
throw exception0(GlobalErrorCodeConstants.UNAUTHORIZED.getCode(), "访问令牌不存在");
}
if (DateUtils.isExpired(accessTokenDO.getExpiresTime())) {
throw exception(GlobalErrorCodeConstants.UNAUTHORIZED, "访问令牌已过期");
throw exception0(GlobalErrorCodeConstants.UNAUTHORIZED.getCode(), "访问令牌已过期");
}
return accessTokenDO;
}

View File

@ -86,9 +86,10 @@ service.interceptors.response.use( async res => {
setToken(refreshTokenRes.data)
requestList.forEach(cb => cb())
return service(res.config)
} catch (e) {
// 2.2 刷新失败,则只能执行登出操作
// 为什么需要 catch 异常呢?刷新失败时,请求因为 Promise.reject 触发异常。
} catch (e) {// 为什么需要 catch 异常呢?刷新失败时,请求因为 Promise.reject 触发异常。
// 2.2 刷新失败,只回放队列的请求
requestList.forEach(cb => cb())
// 提示是否要登出。即不回放当前请求!不然会形成递归
return handleAuthorized();
} finally {
requestList = []
@ -98,12 +99,11 @@ service.interceptors.response.use( async res => {
// 添加到队列,等待刷新获取到新的令牌
return new Promise(resolve => {
requestList.push(() => {
config.headers['Authorization'] = 'Bearer ' + getAccessToken() // 让每个请求携带自定义token 请根据实际情况自行修改
resolve(service(config))
res.config.headers['Authorization'] = 'Bearer ' + getAccessToken() // 让每个请求携带自定义token 请根据实际情况自行修改
resolve(service(res.config))
})
})
}
return handleAuthorized();
} else if (code === 500) {
Message({
message: msg,
@ -123,9 +123,13 @@ service.interceptors.response.use( async res => {
})
return Promise.reject(new Error(msg))
} else if (code !== 200) {
Notification.error({
title: msg
})
if (msg === '无效的刷新令牌') { // hard coding忽略这个提示直接登出
console.log('无效的刷新令牌')
} else {
Notification.error({
title: msg
})
}
return Promise.reject('error')
} else {
return res.data