refactor(auth):重构认证模块并增强验证机制
- 重命名并调整 DTO 类结构,将 LoginRequest 和 RegisterRequest 迁移至 request 包- 引入 AuthLoginRequest 和 AuthRegisterRequest 并添加字段验证注解 - 更新 AuthController 使用新的 DTO 并增加 @Valid 参数校验 - 修改 AuthService 接口及实现类,接收 User 实体而非 RegisterRequest - 添加全局异常处理器 GlobalExceptionHandler 处理参数校验和业务异常 - 新增 ErrorCode 枚举统一管理错误码和消息 - 引入 UserConverter 组件用于 DTO 到实体的转换 - 增强用户注册与登录逻辑,完善异常处理和错误提示 - 移除旧的 BadCredentialsException 捕获逻辑 - 调整 pom.xml 添加 spring-boot-starter-validation 和相关依赖 - 更新 User 实体类,添加完整字段映射和角色关联配置 - 新增 UserInfoService 及其实现,提供用户管理和密码设置功能 -优化 UserMapper 查询方法,支持联表查询用户及其角色信息 - 删除无用的 HLSController 控制器 - 完善 ImageController 文件上传逻辑并更新响应结构 - 添加用户名和密码格式验证工具类 PasswordValidate 和 UsernameValidate
This commit is contained in:
@@ -50,5 +50,12 @@
|
||||
<artifactId>spring-web</artifactId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-crypto</artifactId>
|
||||
<version>6.5.6</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -1,5 +1,6 @@
|
||||
package cn.nopj.chaos_api.service.impl;
|
||||
|
||||
import cn.nopj.chaos_api.dto.response.FileUploadResponse;
|
||||
import cn.nopj.chaos_api.service.ImageService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -10,8 +11,8 @@ import java.io.InputStream;
|
||||
@Service
|
||||
public class ImageServiceImpl implements ImageService {
|
||||
@Override
|
||||
public String uploadImage(String fileName, InputStream content) {
|
||||
public FileUploadResponse uploadImage(String fileName, InputStream content) {
|
||||
//todo 完成上传图片功能
|
||||
return "";
|
||||
return new FileUploadResponse();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package cn.nopj.chaos_api.service.impl;
|
||||
|
||||
import cn.nopj.chaos_api.common.constants.ErrorCode;
|
||||
import cn.nopj.chaos_api.common.exceotion.BizException;
|
||||
import cn.nopj.chaos_api.domain.entity.User;
|
||||
import cn.nopj.chaos_api.dto.response.RoleResponse;
|
||||
import cn.nopj.chaos_api.dto.response.UserinfoResponse;
|
||||
import cn.nopj.chaos_api.mapper.UserMapper;
|
||||
import cn.nopj.chaos_api.service.UserInfoService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class UserInfoServiceImpl implements UserInfoService {
|
||||
@Autowired
|
||||
private PasswordEncoder passwordEncoder;
|
||||
@Autowired
|
||||
UserMapper userMapper;
|
||||
@Override
|
||||
public void setUserPassword(Long Id, String password) {
|
||||
|
||||
log.info("重置用户id: {}的密码", Id);
|
||||
User user = userMapper.selectById(Id);
|
||||
//查询用户是否存在
|
||||
|
||||
if (user == null) {
|
||||
throw new BizException(ErrorCode.USER_NOT_EXISTS);
|
||||
}
|
||||
user.setPassword(passwordEncoder.encode(password));
|
||||
int i = userMapper.updateById(user);
|
||||
log.info("重置用户id: {}的密码结果: {}", Id, i);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserinfoResponse> getAllUsers() {
|
||||
List<User> allUserWithRoles = userMapper.findAllUserWithRoles();
|
||||
|
||||
if (allUserWithRoles == null){
|
||||
throw new RuntimeException("获取所有用户信息失败");
|
||||
}
|
||||
|
||||
return allUserWithRoles.stream().map(user -> {
|
||||
UserinfoResponse userinfoResponse = new UserinfoResponse();
|
||||
userinfoResponse.setId(user.getId());
|
||||
userinfoResponse.setUsername(user.getUsername());
|
||||
userinfoResponse.setRoles(
|
||||
user.getRoles().stream().map(role -> {
|
||||
RoleResponse roleResponse = new RoleResponse();
|
||||
roleResponse.setId(role.getId());
|
||||
roleResponse.setName(role.getName());
|
||||
return roleResponse;
|
||||
}).toList()
|
||||
);
|
||||
return userinfoResponse;
|
||||
}).toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUserPassword(String userId, String password) {
|
||||
try{
|
||||
Long Id = Long.parseLong(userId);
|
||||
setUserPassword(Id, password);
|
||||
}catch (NumberFormatException e) {
|
||||
throw new BizException(ErrorCode.USER_ID_INVALID);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user