增加重置密码修改用户信息功能
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());
|
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
|
@Override
|
||||||
protected void exportData(HttpServletResponse response, List<UserDTO> data) {
|
protected void exportData(HttpServletResponse response, List<UserDTO> data) {
|
||||||
response.setContentType("application/vnd.ms-excel");
|
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 com.qqchen.deploy.backend.model.response.LoginResponse;
|
||||||
import org.mapstruct.Mapper;
|
import org.mapstruct.Mapper;
|
||||||
import org.mapstruct.Mapping;
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.MappingTarget;
|
||||||
|
import org.mapstruct.Mappings;
|
||||||
|
|
||||||
@Mapper(config = BaseConverter.class)
|
@Mapper(config = BaseConverter.class)
|
||||||
public interface UserConverter extends BaseConverter<User, UserDTO> {
|
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 = "token", ignore = true)
|
||||||
@Mapping(target = "id", ignore = true)
|
@Mapping(target = "id", ignore = true)
|
||||||
LoginResponse toLoginResponse(UserDTO userDTO);
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -15,7 +15,7 @@ import java.util.stream.Collectors;
|
|||||||
unmappedTargetPolicy = org.mapstruct.ReportingPolicy.IGNORE
|
unmappedTargetPolicy = org.mapstruct.ReportingPolicy.IGNORE
|
||||||
)
|
)
|
||||||
public interface BaseConverter<T extends Entity<?>, D extends BaseDTO> {
|
public interface BaseConverter<T extends Entity<?>, D extends BaseDTO> {
|
||||||
|
|
||||||
@Mappings({
|
@Mappings({
|
||||||
@Mapping(target = "id", source = "id"),
|
@Mapping(target = "id", source = "id"),
|
||||||
@Mapping(target = "createTime", source = "createTime"),
|
@Mapping(target = "createTime", source = "createTime"),
|
||||||
@ -26,7 +26,7 @@ public interface BaseConverter<T extends Entity<?>, D extends BaseDTO> {
|
|||||||
@Mapping(target = "deleted", source = "deleted")
|
@Mapping(target = "deleted", source = "deleted")
|
||||||
})
|
})
|
||||||
D toDto(T entity);
|
D toDto(T entity);
|
||||||
|
|
||||||
@Mappings({
|
@Mappings({
|
||||||
@Mapping(target = "id", source = "id"),
|
@Mapping(target = "id", source = "id"),
|
||||||
@Mapping(target = "createTime", source = "createTime"),
|
@Mapping(target = "createTime", source = "createTime"),
|
||||||
@ -37,22 +37,24 @@ public interface BaseConverter<T extends Entity<?>, D extends BaseDTO> {
|
|||||||
@Mapping(target = "deleted", source = "deleted")
|
@Mapping(target = "deleted", source = "deleted")
|
||||||
})
|
})
|
||||||
T toEntity(D dto);
|
T toEntity(D dto);
|
||||||
|
|
||||||
@Mappings({
|
@Mappings({
|
||||||
@Mapping(target = "id", ignore = true),
|
@Mapping(target = "id", ignore = true),
|
||||||
@Mapping(target = "createTime", ignore = true),
|
@Mapping(target = "createTime", ignore = true),
|
||||||
@Mapping(target = "createBy", ignore = true),
|
@Mapping(target = "createBy", ignore = true),
|
||||||
|
@Mapping(target = "updateTime", ignore = true),
|
||||||
|
@Mapping(target = "updateBy", ignore = true),
|
||||||
@Mapping(target = "version", ignore = true),
|
@Mapping(target = "version", ignore = true),
|
||||||
@Mapping(target = "deleted", ignore = true)
|
@Mapping(target = "deleted", ignore = true)
|
||||||
})
|
})
|
||||||
void updateEntity(@MappingTarget T entity, D dto);
|
void updateEntity(@MappingTarget T entity, D dto);
|
||||||
|
|
||||||
List<D> toDtoList(List<T> entityList);
|
List<D> toDtoList(List<T> entityList);
|
||||||
|
|
||||||
default Page<D> toDtoPage(Page<T> page) {
|
default Page<D> toDtoPage(Page<T> page) {
|
||||||
return page.map(this::toDto);
|
return page.map(this::toDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
default List<T> toEntityList(List<D> dtos) {
|
default List<T> toEntityList(List<D> dtos) {
|
||||||
if (dtos == null) return null;
|
if (dtos == null) return null;
|
||||||
return dtos.stream()
|
return dtos.stream()
|
||||||
|
|||||||
@ -21,7 +21,7 @@ public class BaseDTO implements Serializable {
|
|||||||
|
|
||||||
private Integer version;
|
private Integer version;
|
||||||
|
|
||||||
private Boolean deleted;
|
private Boolean deleted = false;
|
||||||
|
|
||||||
// 扩展字段,用于存储额外的属性
|
// 扩展字段,用于存储额外的属性
|
||||||
private Map<String, Object> extraData;
|
private Map<String, Object> extraData;
|
||||||
|
|||||||
@ -3,15 +3,30 @@ package com.qqchen.deploy.backend.framework.utils;
|
|||||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
|
||||||
public class PasswordGenerator {
|
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) {
|
public static void main(String[] args) {
|
||||||
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
|
|
||||||
String rawPassword = "admin123";
|
String rawPassword = "admin123";
|
||||||
String encodedPassword = encoder.encode(rawPassword);
|
String encodedPassword = encode(rawPassword);
|
||||||
System.out.println("Raw password: " + rawPassword);
|
System.out.println("Raw password: " + rawPassword);
|
||||||
System.out.println("Encoded password: " + encodedPassword);
|
System.out.println("Encoded password: " + encodedPassword);
|
||||||
|
System.out.println("Password matches: " + matches(rawPassword, encodedPassword));
|
||||||
// 验证密码
|
|
||||||
boolean matches = encoder.matches(rawPassword, encodedPassword);
|
|
||||||
System.out.println("Password matches: " + matches);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -10,8 +10,6 @@ import java.util.Set;
|
|||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
public class UserDTO extends BaseDTO {
|
public class UserDTO extends BaseDTO {
|
||||||
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
private String username;
|
private String username;
|
||||||
|
|
||||||
private String nickname;
|
private String nickname;
|
||||||
|
|||||||
@ -38,4 +38,8 @@ public interface IUserService extends IBaseService<User, UserDTO, Long> {
|
|||||||
*/
|
*/
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
LoginResponse getCurrentUserResponse();
|
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.SecurityUtils;
|
||||||
import com.qqchen.deploy.backend.framework.security.util.JwtTokenUtil;
|
import com.qqchen.deploy.backend.framework.security.util.JwtTokenUtil;
|
||||||
import com.qqchen.deploy.backend.framework.service.impl.BaseServiceImpl;
|
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.LoginRequest;
|
||||||
import com.qqchen.deploy.backend.model.request.UserRequest;
|
import com.qqchen.deploy.backend.model.request.UserRequest;
|
||||||
import com.qqchen.deploy.backend.model.response.LoginResponse;
|
import com.qqchen.deploy.backend.model.response.LoginResponse;
|
||||||
@ -148,4 +149,15 @@ public class UserServiceImpl extends BaseServiceImpl<User, UserDTO, Long> implem
|
|||||||
UserDTO userDTO = getCurrentUser();
|
UserDTO userDTO = getCurrentUser();
|
||||||
return userConverter.toLoginResponse(userDTO);
|
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