mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-22 15:21:53 +08:00
【功能优化】SYSTEM:支持通过 refreshToken 认证,解决部分场景不方便刷新访问令牌场景
This commit is contained in:
parent
c2937bd087
commit
a9928fa227
@ -13,7 +13,7 @@ import java.util.Set;
|
|||||||
/**
|
/**
|
||||||
* 基于 MyBatis Plus 多租户的功能,实现 DB 层面的多租户的功能
|
* 基于 MyBatis Plus 多租户的功能,实现 DB 层面的多租户的功能
|
||||||
*
|
*
|
||||||
* @author
|
* @author 芋道源码
|
||||||
*/
|
*/
|
||||||
public class TenantDatabaseInterceptor implements TenantLineHandler {
|
public class TenantDatabaseInterceptor implements TenantLineHandler {
|
||||||
|
|
||||||
|
@ -109,19 +109,18 @@ public class OAuth2TokenServiceImpl implements OAuth2TokenService {
|
|||||||
|
|
||||||
// 获取不到,从 MySQL 中获取访问令牌
|
// 获取不到,从 MySQL 中获取访问令牌
|
||||||
accessTokenDO = oauth2AccessTokenMapper.selectByAccessToken(accessToken);
|
accessTokenDO = oauth2AccessTokenMapper.selectByAccessToken(accessToken);
|
||||||
if (accessTokenDO != null && DateUtils.isExpired(accessTokenDO.getExpiresTime())) {
|
if (accessTokenDO == null) {
|
||||||
accessTokenDO = null;
|
// 特殊:从 MySQL 中获取刷新令牌。原因:解决部分场景不方便刷新访问令牌场景
|
||||||
}
|
// 例如说,积木报表只允许传递 token,不允许传递 refresh_token,导致无法刷新访问令牌
|
||||||
// 特殊:从 MySQL 中获取刷新令牌。原因:解决部分场景不方便刷新访问令牌场景
|
// 再例如说,前端 WebSocket 的 token 直接跟在 url 上,无法传递 refresh_token
|
||||||
// 例如说,积木报表只允许传递 token,不允许传递 refresh_token,导致无法刷新访问令牌
|
OAuth2RefreshTokenDO refreshTokenDO = oauth2RefreshTokenMapper.selectByRefreshToken(accessToken);
|
||||||
// 再例如说,前端 WebSocket 的 token 直接跟在 url 上,无法传递 refresh_token
|
if (refreshTokenDO != null && !DateUtils.isExpired(refreshTokenDO.getExpiresTime())) {
|
||||||
OAuth2RefreshTokenDO refreshTokenDO = oauth2RefreshTokenMapper.selectByRefreshToken(accessToken);
|
accessTokenDO = convertToAccessToken(refreshTokenDO);
|
||||||
if (refreshTokenDO != null && !DateUtils.isExpired(refreshTokenDO.getExpiresTime())) {
|
}
|
||||||
accessTokenDO = convertToAccessToken(refreshTokenDO);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果在 MySQL 存在,则往 Redis 中写入
|
// 如果在 MySQL 存在,则往 Redis 中写入
|
||||||
if (accessTokenDO != null) {
|
if (accessTokenDO != null && !DateUtils.isExpired(accessTokenDO.getExpiresTime())) {
|
||||||
oauth2AccessTokenRedisDAO.set(accessTokenDO);
|
oauth2AccessTokenRedisDAO.set(accessTokenDO);
|
||||||
}
|
}
|
||||||
return accessTokenDO;
|
return accessTokenDO;
|
||||||
|
@ -158,10 +158,11 @@ public class OAuth2TokenServiceImplTest extends BaseDbAndRedisUnitTest {
|
|||||||
.setAccessTokenValiditySeconds(30);
|
.setAccessTokenValiditySeconds(30);
|
||||||
when(oauth2ClientService.validOAuthClientFromCache(eq(clientId))).thenReturn(clientDO);
|
when(oauth2ClientService.validOAuthClientFromCache(eq(clientId))).thenReturn(clientDO);
|
||||||
// mock 数据(访问令牌)
|
// mock 数据(访问令牌)
|
||||||
OAuth2RefreshTokenDO refreshTokenDO = randomPojo(OAuth2RefreshTokenDO.class)
|
OAuth2RefreshTokenDO refreshTokenDO = randomPojo(OAuth2RefreshTokenDO.class, o ->
|
||||||
.setRefreshToken(refreshToken).setClientId(clientId)
|
o.setRefreshToken(refreshToken).setClientId(clientId)
|
||||||
.setExpiresTime(LocalDateTime.now().plusDays(1))
|
.setExpiresTime(LocalDateTime.now().plusDays(1))
|
||||||
.setUserType(UserTypeEnum.ADMIN.getValue());
|
.setUserType(UserTypeEnum.ADMIN.getValue())
|
||||||
|
.setTenantId(TenantContextHolder.getTenantId()));
|
||||||
oauth2RefreshTokenMapper.insert(refreshTokenDO);
|
oauth2RefreshTokenMapper.insert(refreshTokenDO);
|
||||||
// mock 数据(访问令牌)
|
// mock 数据(访问令牌)
|
||||||
OAuth2AccessTokenDO accessTokenDO = randomPojo(OAuth2AccessTokenDO.class).setRefreshToken(refreshToken)
|
OAuth2AccessTokenDO accessTokenDO = randomPojo(OAuth2AccessTokenDO.class).setRefreshToken(refreshToken)
|
||||||
|
@ -473,7 +473,7 @@ CREATE TABLE IF NOT EXISTS "system_oauth2_access_token" (
|
|||||||
"updater" varchar DEFAULT '',
|
"updater" varchar DEFAULT '',
|
||||||
"update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
"update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||||
"deleted" bit NOT NULL DEFAULT FALSE,
|
"deleted" bit NOT NULL DEFAULT FALSE,
|
||||||
"tenant_id" bigint NOT NULL,
|
"tenant_id" bigint not null,
|
||||||
PRIMARY KEY ("id")
|
PRIMARY KEY ("id")
|
||||||
) COMMENT 'OAuth2 访问令牌';
|
) COMMENT 'OAuth2 访问令牌';
|
||||||
|
|
||||||
@ -491,6 +491,7 @@ CREATE TABLE IF NOT EXISTS "system_oauth2_refresh_token" (
|
|||||||
"updater" varchar DEFAULT '',
|
"updater" varchar DEFAULT '',
|
||||||
"update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
"update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||||
"deleted" bit NOT NULL DEFAULT FALSE,
|
"deleted" bit NOT NULL DEFAULT FALSE,
|
||||||
|
"tenant_id" bigint not null default '0',
|
||||||
PRIMARY KEY ("id")
|
PRIMARY KEY ("id")
|
||||||
) COMMENT 'OAuth2 刷新令牌';
|
) COMMENT 'OAuth2 刷新令牌';
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user