完善 refresh token 失效时,无法自动跳转回首页的问题,同时优化相关的提示
parent
09c8a91b6a
commit
6c5f5e1ad4
|
@ -21,7 +21,7 @@ import javax.annotation.Resource;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.List;
|
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;
|
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);
|
OAuth2RefreshTokenDO refreshTokenDO = oauth2RefreshTokenMapper.selectByRefreshToken(refreshToken);
|
||||||
if (refreshTokenDO == null) {
|
if (refreshTokenDO == null) {
|
||||||
throw exception(GlobalErrorCodeConstants.BAD_REQUEST, "无效的刷新令牌");
|
throw exception0(GlobalErrorCodeConstants.BAD_REQUEST.getCode(), "无效的刷新令牌");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 校验 Client 匹配
|
// 校验 Client 匹配
|
||||||
OAuth2ClientDO clientDO = oauth2ClientService.validOAuthClientFromCache(clientId);
|
OAuth2ClientDO clientDO = oauth2ClientService.validOAuthClientFromCache(clientId);
|
||||||
if (ObjectUtil.notEqual(clientId, refreshTokenDO.getClientId())) {
|
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())) {
|
if (DateUtils.isExpired(refreshTokenDO.getExpiresTime())) {
|
||||||
oauth2AccessTokenMapper.deleteById(refreshTokenDO.getId());
|
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) {
|
public OAuth2AccessTokenDO checkAccessToken(String accessToken) {
|
||||||
OAuth2AccessTokenDO accessTokenDO = getAccessToken(accessToken);
|
OAuth2AccessTokenDO accessTokenDO = getAccessToken(accessToken);
|
||||||
if (accessTokenDO == null) {
|
if (accessTokenDO == null) {
|
||||||
throw exception(GlobalErrorCodeConstants.UNAUTHORIZED, "访问令牌不存在");
|
throw exception0(GlobalErrorCodeConstants.UNAUTHORIZED.getCode(), "访问令牌不存在");
|
||||||
}
|
}
|
||||||
if (DateUtils.isExpired(accessTokenDO.getExpiresTime())) {
|
if (DateUtils.isExpired(accessTokenDO.getExpiresTime())) {
|
||||||
throw exception(GlobalErrorCodeConstants.UNAUTHORIZED, "访问令牌已过期");
|
throw exception0(GlobalErrorCodeConstants.UNAUTHORIZED.getCode(), "访问令牌已过期");
|
||||||
}
|
}
|
||||||
return accessTokenDO;
|
return accessTokenDO;
|
||||||
}
|
}
|
||||||
|
|
|
@ -86,9 +86,10 @@ service.interceptors.response.use( async res => {
|
||||||
setToken(refreshTokenRes.data)
|
setToken(refreshTokenRes.data)
|
||||||
requestList.forEach(cb => cb())
|
requestList.forEach(cb => cb())
|
||||||
return service(res.config)
|
return service(res.config)
|
||||||
} catch (e) {
|
} catch (e) {// 为什么需要 catch 异常呢?刷新失败时,请求因为 Promise.reject 触发异常。
|
||||||
// 2.2 刷新失败,则只能执行登出操作
|
// 2.2 刷新失败,只回放队列的请求
|
||||||
// 为什么需要 catch 异常呢?刷新失败时,请求因为 Promise.reject 触发异常。
|
requestList.forEach(cb => cb())
|
||||||
|
// 提示是否要登出。即不回放当前请求!不然会形成递归
|
||||||
return handleAuthorized();
|
return handleAuthorized();
|
||||||
} finally {
|
} finally {
|
||||||
requestList = []
|
requestList = []
|
||||||
|
@ -98,12 +99,11 @@ service.interceptors.response.use( async res => {
|
||||||
// 添加到队列,等待刷新获取到新的令牌
|
// 添加到队列,等待刷新获取到新的令牌
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
requestList.push(() => {
|
requestList.push(() => {
|
||||||
config.headers['Authorization'] = 'Bearer ' + getAccessToken() // 让每个请求携带自定义token 请根据实际情况自行修改
|
res.config.headers['Authorization'] = 'Bearer ' + getAccessToken() // 让每个请求携带自定义token 请根据实际情况自行修改
|
||||||
resolve(service(config))
|
resolve(service(res.config))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return handleAuthorized();
|
|
||||||
} else if (code === 500) {
|
} else if (code === 500) {
|
||||||
Message({
|
Message({
|
||||||
message: msg,
|
message: msg,
|
||||||
|
@ -123,9 +123,13 @@ service.interceptors.response.use( async res => {
|
||||||
})
|
})
|
||||||
return Promise.reject(new Error(msg))
|
return Promise.reject(new Error(msg))
|
||||||
} else if (code !== 200) {
|
} else if (code !== 200) {
|
||||||
Notification.error({
|
if (msg === '无效的刷新令牌') { // hard coding:忽略这个提示,直接登出
|
||||||
title: msg
|
console.log('无效的刷新令牌')
|
||||||
})
|
} else {
|
||||||
|
Notification.error({
|
||||||
|
title: msg
|
||||||
|
})
|
||||||
|
}
|
||||||
return Promise.reject('error')
|
return Promise.reject('error')
|
||||||
} else {
|
} else {
|
||||||
return res.data
|
return res.data
|
||||||
|
|
Loading…
Reference in New Issue