From 038b3661fbe208a8f55c96d8478de6bbc315ac84 Mon Sep 17 00:00:00 2001 From: bynt Date: Wed, 18 May 2022 14:32:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BC=82=E5=B8=B8=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E4=BF=A1=E6=81=AF=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/baiye/core/base/api/ResultCode.java | 2 +- .../ResourceAuthExceptionEntryPoint.java | 3 +- .../CustomWebRespExceptionTranslator.java | 3 +- .../baiye/service/impl/UserServiceImpl.java | 37 ++++++++++++------- 4 files changed, 28 insertions(+), 17 deletions(-) diff --git a/cdp-common/cdp-common-core/src/main/java/com/baiye/core/base/api/ResultCode.java b/cdp-common/cdp-common-core/src/main/java/com/baiye/core/base/api/ResultCode.java index a5de75e..dee31c6 100644 --- a/cdp-common/cdp-common-core/src/main/java/com/baiye/core/base/api/ResultCode.java +++ b/cdp-common/cdp-common-core/src/main/java/com/baiye/core/base/api/ResultCode.java @@ -85,7 +85,7 @@ public enum ResultCode implements IResultCode { /** * 用户账号过期 */ - USER_ACCOUNT_EXPIRED(11008, "用户账号过期"), + USER_ACCOUNT_EXPIRED(11008, "用户账号已过期"), /** * 没有该用户 */ diff --git a/cdp-common/cdp-common-security/src/main/java/com/baiye/component/ResourceAuthExceptionEntryPoint.java b/cdp-common/cdp-common-security/src/main/java/com/baiye/component/ResourceAuthExceptionEntryPoint.java index 4d046ed..4c0b028 100644 --- a/cdp-common/cdp-common-security/src/main/java/com/baiye/component/ResourceAuthExceptionEntryPoint.java +++ b/cdp-common/cdp-common-security/src/main/java/com/baiye/component/ResourceAuthExceptionEntryPoint.java @@ -1,5 +1,6 @@ package com.baiye.component; +import com.baiye.core.base.api.ResultCode; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; @@ -34,7 +35,7 @@ public class ResourceAuthExceptionEntryPoint implements AuthenticationEntryPoint result.put("code", HttpStatus.UNAUTHORIZED.value()); if (e!=null){ result.put("msg","error"); - result.put("data",e.getMessage()); + result.put("data", ResultCode.USER_ACCOUNT_EXPIRED.getMsg()); } response.setStatus(HttpStatus.UNAUTHORIZED.value()); PrintWriter writer = response.getWriter(); diff --git a/cdp-iaas/authorization-server/src/main/java/com/baiye/auth/translator/CustomWebRespExceptionTranslator.java b/cdp-iaas/authorization-server/src/main/java/com/baiye/auth/translator/CustomWebRespExceptionTranslator.java index 1d3327e..b95e023 100644 --- a/cdp-iaas/authorization-server/src/main/java/com/baiye/auth/translator/CustomWebRespExceptionTranslator.java +++ b/cdp-iaas/authorization-server/src/main/java/com/baiye/auth/translator/CustomWebRespExceptionTranslator.java @@ -49,7 +49,8 @@ public class CustomWebRespExceptionTranslator implements WebResponseExceptionTra return buildResponseEntity(ApiError.error(message)); } - if (StringUtils.containsIgnoreCase(e.getMessage(), "locked")) { + if (StringUtils.containsIgnoreCase(e.getMessage(), "locked") + || StringUtils.containsIgnoreCase(e.getMessage(), "User is disabled")) { message = "用户已被锁定,请联系管理员"; return buildResponseEntity(ApiError.error(message)); } diff --git a/cdp-manager/backstage-manger-server/backstage-manger-server-service/src/main/java/com/baiye/service/impl/UserServiceImpl.java b/cdp-manager/backstage-manger-server/backstage-manger-server-service/src/main/java/com/baiye/service/impl/UserServiceImpl.java index 21430ab..3f49e1b 100644 --- a/cdp-manager/backstage-manger-server/backstage-manger-server-service/src/main/java/com/baiye/service/impl/UserServiceImpl.java +++ b/cdp-manager/backstage-manger-server/backstage-manger-server-service/src/main/java/com/baiye/service/impl/UserServiceImpl.java @@ -35,7 +35,10 @@ import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CacheConfig; +import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.data.domain.Pageable; import org.springframework.security.authentication.BadCredentialsException; @@ -54,9 +57,8 @@ import java.util.*; import java.util.stream.Collectors; /** - * - * @description 用户业务实现类 * @author Enzo + * @description 用户业务实现类 * @create: 2020-08-11 16:17 */ @Service @@ -67,6 +69,7 @@ public class UserServiceImpl extends ServiceImpl implements IU private final UserMapper userMapper; + private final CacheManager cacheManager; private final FileProperties properties; private final UserMapStruct userMapStruct; @@ -155,21 +158,21 @@ public class UserServiceImpl extends ServiceImpl implements IU this.verifyUser(resources); resources.setDeptId(resources.getDept().getId()); if (this.save(resources)) { - if (CollUtil.isNotEmpty(resources.getJobs())){ + if (CollUtil.isNotEmpty(resources.getJobs())) { // 插入职位信息 // TODO: 2020/8/24 后续使用批量插入优化 resources.getJobs().forEach(job -> { - UserJobRelation ujr = new UserJobRelation(resources.getId(),job.getId()); + UserJobRelation ujr = new UserJobRelation(resources.getId(), job.getId()); this.userJobMapper.insert(ujr); }); } - if (CollUtil.isNotEmpty(resources.getRoles())){ + if (CollUtil.isNotEmpty(resources.getRoles())) { resources.getRoles().forEach(role -> { - UserRoleRelation urr = new UserRoleRelation(resources.getId(),role.getId()); + UserRoleRelation urr = new UserRoleRelation(resources.getId(), role.getId()); this.userRoleMapper.insert(urr); }); } - }else { + } else { log.error("插入数据失败:{}", resources); throw new CreateFailException("插入数据失败,请联系管理员"); } @@ -190,7 +193,7 @@ public class UserServiceImpl extends ServiceImpl implements IU // 验证通过后查看修改后的数据与原来的数据的用户名是否发生改变 if (!oldUser.getUsername().equals(resources.getUsername())) { // 删除缓存数据 - this.redisUtils.del( CacheKey.USER_NAME+ oldUser.getUsername()); + this.redisUtils.del(CacheKey.USER_NAME + oldUser.getUsername()); } // 如果用户被禁用,则清除用户登陆的信息 if (!resources.getEnabled()) { @@ -345,11 +348,11 @@ public class UserServiceImpl extends ServiceImpl implements IU } if (this.removeByIds(ids)) { // 删除关联的角色和职位信息 - ids.forEach(id->{ - this.userJobMapper.delete(Wrappers.lambdaUpdate().eq(UserJobRelation::getUserId,id)); - this.userRoleMapper.delete(Wrappers.lambdaUpdate().eq(UserRoleRelation::getUserId,id)); + ids.forEach(id -> { + this.userJobMapper.delete(Wrappers.lambdaUpdate().eq(UserJobRelation::getUserId, id)); + this.userRoleMapper.delete(Wrappers.lambdaUpdate().eq(UserRoleRelation::getUserId, id)); }); - }else { + } else { log.error("删除失败:{}", ids); throw new DeleteFailException("删除失败,请联系管理员"); } @@ -358,11 +361,13 @@ public class UserServiceImpl extends ServiceImpl implements IU /** * 修改用户账号密码 - * @param userDto + * + * @param userDto * @param encode */ @Override @Transactional(rollbackFor = Exception.class) + @CacheEvict(key = "'username:' + #userDto.username") public void updatePassword(UserDto userDto, String encode) { boolean update = this.update(Wrappers.lambdaUpdate().set(User::getPassword, encode) .set(User::getPwdResetTime, LocalDateTime.now(ZoneId.systemDefault())) @@ -374,7 +379,11 @@ public class UserServiceImpl extends ServiceImpl implements IU try { this.remoteAuthService.delete(Collections.singleton(userDto.getId())); } catch (Exception e) { - throw new RuntimeException(e); + throw new BadRequestException(ResultCode.ERROR.getMsg()); + } + Cache cache = this.cacheManager.getCache(CacheKey.USER_DETAILS); + if (cache != null) { + cache.evict(userDto.getUsername()); } }