修改异常返回信息结果

master
bynt 2 years ago
parent 0d3c221d0b
commit 038b3661fb

@ -85,7 +85,7 @@ public enum ResultCode implements IResultCode {
/**
*
*/
USER_ACCOUNT_EXPIRED(11008, "用户账号过期"),
USER_ACCOUNT_EXPIRED(11008, "用户账号过期"),
/**
*
*/

@ -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();

@ -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));
}

@ -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<UserMapper, User> 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<UserMapper, User> 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<UserMapper, User> 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<UserMapper, User> implements IU
}
if (this.removeByIds(ids)) {
// 删除关联的角色和职位信息
ids.forEach(id->{
this.userJobMapper.delete(Wrappers.<UserJobRelation>lambdaUpdate().eq(UserJobRelation::getUserId,id));
this.userRoleMapper.delete(Wrappers.<UserRoleRelation>lambdaUpdate().eq(UserRoleRelation::getUserId,id));
ids.forEach(id -> {
this.userJobMapper.delete(Wrappers.<UserJobRelation>lambdaUpdate().eq(UserJobRelation::getUserId, id));
this.userRoleMapper.delete(Wrappers.<UserRoleRelation>lambdaUpdate().eq(UserRoleRelation::getUserId, id));
});
}else {
} else {
log.error("删除失败:{}", ids);
throw new DeleteFailException("删除失败,请联系管理员");
}
@ -358,11 +361,13 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> 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.<User>lambdaUpdate().set(User::getPassword, encode)
.set(User::getPwdResetTime, LocalDateTime.now(ZoneId.systemDefault()))
@ -374,7 +379,11 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> 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());
}
}

Loading…
Cancel
Save