增加重置密码修改用户信息功能
This commit is contained in:
parent
3517f38282
commit
ada0b0316c
@ -40,6 +40,13 @@ public class UserApiController extends BaseController<User, UserDTO, Long, UserQ
|
||||
return Response.success(userService.getCurrentUserResponse());
|
||||
}
|
||||
|
||||
@Operation(summary = "重置用户密码")
|
||||
@PostMapping("/{id}/reset-password")
|
||||
public Response<Void> resetPassword(@PathVariable Long id, @RequestBody String newPassword) {
|
||||
userService.resetPassword(id, newPassword);
|
||||
return Response.success();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void exportData(HttpServletResponse response, List<UserDTO> data) {
|
||||
response.setContentType("application/vnd.ms-excel");
|
||||
|
||||
@ -6,6 +6,8 @@ import com.qqchen.deploy.backend.model.UserDTO;
|
||||
import com.qqchen.deploy.backend.model.response.LoginResponse;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.MappingTarget;
|
||||
import org.mapstruct.Mappings;
|
||||
|
||||
@Mapper(config = BaseConverter.class)
|
||||
public interface UserConverter extends BaseConverter<User, UserDTO> {
|
||||
@ -22,4 +24,18 @@ public interface UserConverter extends BaseConverter<User, UserDTO> {
|
||||
@Mapping(target = "token", ignore = true)
|
||||
@Mapping(target = "id", ignore = true)
|
||||
LoginResponse toLoginResponse(UserDTO userDTO);
|
||||
|
||||
@Override
|
||||
@Mappings({
|
||||
@Mapping(target = "id", ignore = true),
|
||||
@Mapping(target = "password", ignore = true),
|
||||
@Mapping(target = "createTime", ignore = true),
|
||||
@Mapping(target = "createBy", ignore = true),
|
||||
@Mapping(target = "updateTime", ignore = true),
|
||||
@Mapping(target = "updateBy", ignore = true),
|
||||
@Mapping(target = "version", ignore = true),
|
||||
@Mapping(target = "deleted", ignore = true)
|
||||
})
|
||||
void updateEntity(@MappingTarget User entity, UserDTO dto);
|
||||
|
||||
}
|
||||
@ -42,6 +42,8 @@ public interface BaseConverter<T extends Entity<?>, D extends BaseDTO> {
|
||||
@Mapping(target = "id", ignore = true),
|
||||
@Mapping(target = "createTime", ignore = true),
|
||||
@Mapping(target = "createBy", ignore = true),
|
||||
@Mapping(target = "updateTime", ignore = true),
|
||||
@Mapping(target = "updateBy", ignore = true),
|
||||
@Mapping(target = "version", ignore = true),
|
||||
@Mapping(target = "deleted", ignore = true)
|
||||
})
|
||||
|
||||
@ -21,7 +21,7 @@ public class BaseDTO implements Serializable {
|
||||
|
||||
private Integer version;
|
||||
|
||||
private Boolean deleted;
|
||||
private Boolean deleted = false;
|
||||
|
||||
// 扩展字段,用于存储额外的属性
|
||||
private Map<String, Object> extraData;
|
||||
|
||||
@ -3,15 +3,30 @@ package com.qqchen.deploy.backend.framework.utils;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
|
||||
public class PasswordGenerator {
|
||||
private static final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
|
||||
|
||||
/**
|
||||
* 加密密码
|
||||
*/
|
||||
public static String encode(String rawPassword) {
|
||||
return encoder.encode(rawPassword);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证密码
|
||||
*/
|
||||
public static boolean matches(String rawPassword, String encodedPassword) {
|
||||
return encoder.matches(rawPassword, encodedPassword);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成示例密码(仅用于测试)
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
|
||||
String rawPassword = "admin123";
|
||||
String encodedPassword = encoder.encode(rawPassword);
|
||||
String encodedPassword = encode(rawPassword);
|
||||
System.out.println("Raw password: " + rawPassword);
|
||||
System.out.println("Encoded password: " + encodedPassword);
|
||||
|
||||
// 验证密码
|
||||
boolean matches = encoder.matches(rawPassword, encodedPassword);
|
||||
System.out.println("Password matches: " + matches);
|
||||
System.out.println("Password matches: " + matches(rawPassword, encodedPassword));
|
||||
}
|
||||
}
|
||||
@ -10,8 +10,6 @@ import java.util.Set;
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class UserDTO extends BaseDTO {
|
||||
|
||||
private Long id;
|
||||
|
||||
private String username;
|
||||
|
||||
private String nickname;
|
||||
|
||||
@ -38,4 +38,8 @@ public interface IUserService extends IBaseService<User, UserDTO, Long> {
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
LoginResponse getCurrentUserResponse();
|
||||
|
||||
@Transactional(readOnly = false)
|
||||
@Audited(action = "RESET_PASSWORD", detail = "重置密码")
|
||||
void resetPassword(Long userId, String newPassword);
|
||||
}
|
||||
@ -8,6 +8,7 @@ import com.qqchen.deploy.backend.framework.exception.BusinessException;
|
||||
import com.qqchen.deploy.backend.framework.security.SecurityUtils;
|
||||
import com.qqchen.deploy.backend.framework.security.util.JwtTokenUtil;
|
||||
import com.qqchen.deploy.backend.framework.service.impl.BaseServiceImpl;
|
||||
import com.qqchen.deploy.backend.framework.utils.PasswordGenerator;
|
||||
import com.qqchen.deploy.backend.model.request.LoginRequest;
|
||||
import com.qqchen.deploy.backend.model.request.UserRequest;
|
||||
import com.qqchen.deploy.backend.model.response.LoginResponse;
|
||||
@ -148,4 +149,15 @@ public class UserServiceImpl extends BaseServiceImpl<User, UserDTO, Long> implem
|
||||
UserDTO userDTO = getCurrentUser();
|
||||
return userConverter.toLoginResponse(userDTO);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = false)
|
||||
@Audited(action = "RESET_PASSWORD", detail = "重置密码")
|
||||
public void resetPassword(Long userId, String newPassword) {
|
||||
User user = findEntityById(userId);
|
||||
String encodedPassword = PasswordGenerator.encode(newPassword);
|
||||
user.setPassword(encodedPassword);
|
||||
repository.save(user);
|
||||
log.info("用户 {} 密码已重置", user.getUsername());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user