Commit 62e4af401601e0b86a78c2881df849e1bfdb1c77
1 parent
351ae449
feat:【system】用户被禁用时,删除其相关 token,关联 issue:https://gitee.com/yudaocode/yudao-ui-ad…
…min-vue3/issues/IB2EK6
Showing
4 changed files
with
41 additions
and
0 deletions
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/oauth2/OAuth2AccessTokenMapper.java
| @@ -32,4 +32,9 @@ public interface OAuth2AccessTokenMapper extends BaseMapperX<OAuth2AccessTokenDO | @@ -32,4 +32,9 @@ public interface OAuth2AccessTokenMapper extends BaseMapperX<OAuth2AccessTokenDO | ||
| 32 | .orderByDesc(OAuth2AccessTokenDO::getId)); | 32 | .orderByDesc(OAuth2AccessTokenDO::getId)); |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | + default List<OAuth2AccessTokenDO> selectListByUserIdAndUserType(Long userId, Integer userType) { | ||
| 36 | + return selectList(OAuth2AccessTokenDO::getUserId, userId, | ||
| 37 | + OAuth2AccessTokenDO::getUserType, userType); | ||
| 38 | + } | ||
| 39 | + | ||
| 35 | } | 40 | } |
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/oauth2/OAuth2TokenService.java
| @@ -70,6 +70,17 @@ public interface OAuth2TokenService { | @@ -70,6 +70,17 @@ public interface OAuth2TokenService { | ||
| 70 | OAuth2AccessTokenDO removeAccessToken(String accessToken); | 70 | OAuth2AccessTokenDO removeAccessToken(String accessToken); |
| 71 | 71 | ||
| 72 | /** | 72 | /** |
| 73 | + * 移除访问令牌 | ||
| 74 | + * 注意:该流程中,会移除相关的刷新令牌 | ||
| 75 | + * | ||
| 76 | + * 参考 DefaultTokenServices 的 revokeToken 方法 | ||
| 77 | + * | ||
| 78 | + * @param userId 用户编号 | ||
| 79 | + * @param userType 用户类型 | ||
| 80 | + */ | ||
| 81 | + void removeAccessToken(Long userId, Integer userType); | ||
| 82 | + | ||
| 83 | + /** | ||
| 73 | * 获得访问令牌分页 | 84 | * 获得访问令牌分页 |
| 74 | * | 85 | * |
| 75 | * @param reqVO 请求 | 86 | * @param reqVO 请求 |
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/oauth2/OAuth2TokenServiceImpl.java
| @@ -154,6 +154,21 @@ public class OAuth2TokenServiceImpl implements OAuth2TokenService { | @@ -154,6 +154,21 @@ public class OAuth2TokenServiceImpl implements OAuth2TokenService { | ||
| 154 | } | 154 | } |
| 155 | 155 | ||
| 156 | @Override | 156 | @Override |
| 157 | + public void removeAccessToken(Long userId, Integer userType) { | ||
| 158 | + List<OAuth2AccessTokenDO> accessTokens = oauth2AccessTokenMapper.selectListByUserIdAndUserType(userId, userType); | ||
| 159 | + if (CollUtil.isEmpty(accessTokens)) { | ||
| 160 | + return; | ||
| 161 | + } | ||
| 162 | + accessTokens.forEach(accessToken -> { | ||
| 163 | + // 删除访问令牌 | ||
| 164 | + oauth2AccessTokenMapper.deleteById(accessToken.getId()); | ||
| 165 | + oauth2AccessTokenRedisDAO.delete(accessToken.getAccessToken()); | ||
| 166 | + // 删除刷新令牌 | ||
| 167 | + oauth2RefreshTokenMapper.deleteByRefreshToken(accessToken.getRefreshToken()); | ||
| 168 | + }); | ||
| 169 | + } | ||
| 170 | + | ||
| 171 | + @Override | ||
| 157 | public PageResult<OAuth2AccessTokenDO> getAccessTokenPage(OAuth2AccessTokenPageReqVO reqVO) { | 172 | public PageResult<OAuth2AccessTokenDO> getAccessTokenPage(OAuth2AccessTokenPageReqVO reqVO) { |
| 158 | return oauth2AccessTokenMapper.selectPage(reqVO); | 173 | return oauth2AccessTokenMapper.selectPage(reqVO); |
| 159 | } | 174 | } |
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java
| @@ -5,6 +5,7 @@ import cn.hutool.core.collection.CollectionUtil; | @@ -5,6 +5,7 @@ import cn.hutool.core.collection.CollectionUtil; | ||
| 5 | import cn.hutool.core.util.ObjUtil; | 5 | import cn.hutool.core.util.ObjUtil; |
| 6 | import cn.hutool.core.util.StrUtil; | 6 | import cn.hutool.core.util.StrUtil; |
| 7 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | 7 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 8 | +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; | ||
| 8 | import cn.iocoder.yudao.framework.common.exception.ServiceException; | 9 | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| 9 | import cn.iocoder.yudao.framework.common.pojo.PageResult; | 10 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 10 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | 11 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| @@ -26,6 +27,7 @@ import cn.iocoder.yudao.module.system.dal.mysql.dept.UserPostMapper; | @@ -26,6 +27,7 @@ import cn.iocoder.yudao.module.system.dal.mysql.dept.UserPostMapper; | ||
| 26 | import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper; | 27 | import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper; |
| 27 | import cn.iocoder.yudao.module.system.service.dept.DeptService; | 28 | import cn.iocoder.yudao.module.system.service.dept.DeptService; |
| 28 | import cn.iocoder.yudao.module.system.service.dept.PostService; | 29 | import cn.iocoder.yudao.module.system.service.dept.PostService; |
| 30 | +import cn.iocoder.yudao.module.system.service.oauth2.OAuth2TokenService; | ||
| 29 | import cn.iocoder.yudao.module.system.service.permission.PermissionService; | 31 | import cn.iocoder.yudao.module.system.service.permission.PermissionService; |
| 30 | import cn.iocoder.yudao.module.system.service.tenant.TenantService; | 32 | import cn.iocoder.yudao.module.system.service.tenant.TenantService; |
| 31 | import com.google.common.annotations.VisibleForTesting; | 33 | import com.google.common.annotations.VisibleForTesting; |
| @@ -75,6 +77,9 @@ public class AdminUserServiceImpl implements AdminUserService { | @@ -75,6 +77,9 @@ public class AdminUserServiceImpl implements AdminUserService { | ||
| 75 | @Resource | 77 | @Resource |
| 76 | @Lazy // 延迟,避免循环依赖报错 | 78 | @Lazy // 延迟,避免循环依赖报错 |
| 77 | private TenantService tenantService; | 79 | private TenantService tenantService; |
| 80 | + @Resource | ||
| 81 | + @Lazy // 懒加载,避免循环依赖 | ||
| 82 | + private OAuth2TokenService oauth2TokenService; | ||
| 78 | 83 | ||
| 79 | @Resource | 84 | @Resource |
| 80 | private UserPostMapper userPostMapper; | 85 | private UserPostMapper userPostMapper; |
| @@ -227,6 +232,11 @@ public class AdminUserServiceImpl implements AdminUserService { | @@ -227,6 +232,11 @@ public class AdminUserServiceImpl implements AdminUserService { | ||
| 227 | updateObj.setId(id); | 232 | updateObj.setId(id); |
| 228 | updateObj.setStatus(status); | 233 | updateObj.setStatus(status); |
| 229 | userMapper.updateById(updateObj); | 234 | userMapper.updateById(updateObj); |
| 235 | + | ||
| 236 | + // 如果是禁用用户,则删除其 Token 信息 | ||
| 237 | + if (CommonStatusEnum.isDisable(status)) { | ||
| 238 | + oauth2TokenService.removeAccessToken(id, UserTypeEnum.ADMIN.getValue()); | ||
| 239 | + } | ||
| 230 | } | 240 | } |
| 231 | 241 | ||
| 232 | @Override | 242 | @Override |