feat(user): 添加用户重置密码功能并优化相关服务
- 在 UserController 中添加重置密码接口- 在 UserMapper 中添加更新密码的方法 - 在 UserService 中实现重置密码的业务逻辑 - 新增 UserRestPasswordRequest 类用于重置密码请求 - 在 WebConfig 中添加跨域配置,支持前端开发环境
This commit is contained in:
parent
73eb1198f6
commit
fa92f20aca
@ -2,6 +2,7 @@ package com.example.copykamanotes.config;
|
|||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
@ -24,4 +25,14 @@ public class WebConfig implements WebMvcConfigurer {
|
|||||||
.excludePathPatterns("/api/users/login", "/api/users/register");
|
.excludePathPatterns("/api/users/login", "/api/users/register");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addCorsMappings(CorsRegistry registry) {
|
||||||
|
registry.addMapping("/**")
|
||||||
|
.allowedOrigins("http://localhost:5173", "http://127.0.0.1:5173") // 允许的域名
|
||||||
|
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH")// 允许的 HTTP 方法
|
||||||
|
.allowedHeaders("*")
|
||||||
|
.allowCredentials(true)
|
||||||
|
.maxAge(3600);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,14 +1,9 @@
|
|||||||
package com.example.copykamanotes.controller;
|
package com.example.copykamanotes.controller;
|
||||||
|
|
||||||
import com.example.copykamanotes.model.base.ApiResponse;
|
import com.example.copykamanotes.model.base.ApiResponse;
|
||||||
import com.example.copykamanotes.model.dto.user.LoginRequest;
|
import com.example.copykamanotes.model.dto.user.*;
|
||||||
import com.example.copykamanotes.model.dto.user.RegisterRequest;
|
|
||||||
import com.example.copykamanotes.model.dto.user.UpdateUserRequest;
|
|
||||||
import com.example.copykamanotes.model.dto.user.UserQueryParam;
|
|
||||||
import com.example.copykamanotes.model.entity.User;
|
import com.example.copykamanotes.model.entity.User;
|
||||||
import com.example.copykamanotes.model.vo.user.AvatarVO;
|
import com.example.copykamanotes.model.vo.user.*;
|
||||||
import com.example.copykamanotes.model.vo.user.LoginUserVO;
|
|
||||||
import com.example.copykamanotes.model.vo.user.RegisterVO;
|
|
||||||
import com.example.copykamanotes.service.UserService;
|
import com.example.copykamanotes.service.UserService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -76,4 +71,11 @@ public class UserController {
|
|||||||
) {
|
) {
|
||||||
return userService.getUserList(userQueryParam);
|
return userService.getUserList(userQueryParam);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/users/resetPassword")
|
||||||
|
public ApiResponse<UserVO> updateUserPassword(
|
||||||
|
@RequestBody UserRestPasswordRequest userRestPasswordRequest
|
||||||
|
) {
|
||||||
|
return userService.updateUserPassword(userRestPasswordRequest.getOldPassword(), userRestPasswordRequest.getNewPassword());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,6 +26,8 @@ public interface UserMapper {
|
|||||||
|
|
||||||
int update(User user);
|
int update(User user);
|
||||||
|
|
||||||
|
int updatePassword(@Param("userId") Long userId, @Param("password") String password);
|
||||||
|
|
||||||
List<User> findByIds(@Param("userIds") List<Long> userIds);
|
List<User> findByIds(@Param("userIds") List<Long> userIds);
|
||||||
|
|
||||||
int countByQueryParam(@Param("queryParams") UserQueryParam userQueryParam);
|
int countByQueryParam(@Param("queryParams") UserQueryParam userQueryParam);
|
||||||
|
|||||||
@ -0,0 +1,10 @@
|
|||||||
|
package com.example.copykamanotes.model.dto.user;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class UserRestPasswordRequest {
|
||||||
|
private String account;
|
||||||
|
private String oldPassword;
|
||||||
|
private String newPassword;
|
||||||
|
}
|
||||||
@ -66,6 +66,8 @@ public interface UserService {
|
|||||||
*/
|
*/
|
||||||
Map<Long, User> getUserMapByIds(List<Long> authorIds);
|
Map<Long, User> getUserMapByIds(List<Long> authorIds);
|
||||||
|
|
||||||
|
ApiResponse<UserVO> updateUserPassword(String oldPassword, String newPassword);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取用户列表
|
* 获取用户列表
|
||||||
* @param userQueryParam 查询参数
|
* @param userQueryParam 查询参数
|
||||||
|
|||||||
@ -32,6 +32,7 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Log4j2
|
@Log4j2
|
||||||
@ -233,4 +234,33 @@ public class UserServiceImpl implements UserService {
|
|||||||
return ApiResponseUtil.error("上传失败");
|
return ApiResponseUtil.error("上传失败");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ApiResponse<UserVO> updateUserPassword(String oldPassword, String newPassword) {
|
||||||
|
Long userId = requestScopeData.getUserId();
|
||||||
|
|
||||||
|
if (userId == null) {
|
||||||
|
return ApiResponseUtil.error("未登录");
|
||||||
|
}
|
||||||
|
|
||||||
|
User user = null;
|
||||||
|
user = userMapper.findById(userId);
|
||||||
|
if (user == null || !passwordEncoder.matches(oldPassword, user.getPassword())) {
|
||||||
|
return ApiResponseUtil.error("旧密码错误");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Objects.equals(oldPassword, newPassword)) {
|
||||||
|
return ApiResponseUtil.error("新密码不能与旧密码相同");
|
||||||
|
}
|
||||||
|
|
||||||
|
user.setPassword(passwordEncoder.encode(newPassword));
|
||||||
|
|
||||||
|
try {
|
||||||
|
userMapper.updatePassword(user.getUserId(), user.getPassword());
|
||||||
|
return ApiResponseUtil.success("更新密码成功");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("更新密码失败", e);
|
||||||
|
return ApiResponseUtil.error("更新密码失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -63,6 +63,12 @@
|
|||||||
where user_id = #{userId}
|
where user_id = #{userId}
|
||||||
</update>
|
</update>
|
||||||
|
|
||||||
|
<update id="updatePassword" parameterType="com.example.copykamanotes.model.entity.User">
|
||||||
|
UPDATE user
|
||||||
|
SET password = #{password}
|
||||||
|
WHERE user_id = #{userId}
|
||||||
|
</update>
|
||||||
|
|
||||||
<select id="findByIds" resultType="com.example.copykamanotes.model.entity.User">
|
<select id="findByIds" resultType="com.example.copykamanotes.model.entity.User">
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM user
|
FROM user
|
||||||
|
|||||||
Reference in New Issue
Block a user