1. 会员用户的找回密码 API 的优化

This commit is contained in:
YunaiV 2023-08-19 23:12:34 +08:00
parent 5d4a7ffa3b
commit 95254341a1
11 changed files with 57 additions and 54 deletions

View File

@ -11,7 +11,7 @@ public interface ErrorCodeConstants {
// ========== 用户相关 1004001000============
ErrorCode USER_NOT_EXISTS = new ErrorCode(1004001000, "用户不存在");
ErrorCode USER_PASSWORD_FAILED = new ErrorCode(1004001001, "密码校验失败");
ErrorCode USER_MOBILE_NOT_EXISTS = new ErrorCode(1004001001, "手机号未注册用户");
ErrorCode USER_MOBILE_USED = new ErrorCode(1004001002, "修改手机失败,该手机号({})已经被使用");
// ========== AUTH 模块 1004003000 ==========

View File

@ -4,7 +4,6 @@ import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.framework.security.config.SecurityProperties;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.controller.app.auth.vo.*;
import cn.iocoder.yudao.module.member.service.auth.MemberAuthService;
@ -84,14 +83,6 @@ public class AppAuthController {
return success(true);
}
@PostMapping("/reset-password")
@Operation(summary = "重置密码", description = "用户忘记密码时使用")
@PreAuthenticated
public CommonResult<Boolean> resetPassword(@RequestBody @Valid AppAuthResetPasswordReqVO reqVO) {
authService.resetPassword(reqVO);
return success(true);
}
// ========== 社交登录相关 ==========
@GetMapping("/social-auth-redirect")

View File

@ -2,10 +2,7 @@ package cn.iocoder.yudao.module.member.controller.app.user;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserInfoRespVO;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdatePasswordReqVO;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdateReqVO;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdateMobileReqVO;
import cn.iocoder.yudao.module.member.controller.app.user.vo.*;
import cn.iocoder.yudao.module.member.convert.user.MemberUserConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
@ -63,5 +60,12 @@ public class AppMemberUserController {
return success(true);
}
@PutMapping("/reset-password")
@Operation(summary = "重置密码", description = "用户忘记密码时使用")
public CommonResult<Boolean> resetPassword(@RequestBody @Valid AppMemberUserResetPasswordReqVO reqVO) {
userService.resetUserPassword(reqVO);
return success(true);
}
}

View File

@ -1,4 +1,4 @@
package cn.iocoder.yudao.module.member.controller.app.auth.vo;
package cn.iocoder.yudao.module.member.controller.app.user.vo;
import cn.iocoder.yudao.framework.common.validation.Mobile;
import io.swagger.v3.oas.annotations.media.Schema;
@ -12,13 +12,12 @@ import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
// TODO 芋艿code review 相关逻辑
@Schema(description = "用户 APP - 重置密码 Request VO")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class AppAuthResetPasswordReqVO {
public class AppMemberUserResetPasswordReqVO {
@Schema(description = "新密码", requiredMode = Schema.RequiredMode.REQUIRED, example = "buzhidao")
@NotEmpty(message = "新密码不能为空")

View File

@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.member.convert.auth;
import cn.iocoder.yudao.module.member.controller.app.auth.vo.*;
import cn.iocoder.yudao.module.member.controller.app.social.vo.AppSocialUserUnbindReqVO;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserResetPasswordReqVO;
import cn.iocoder.yudao.module.system.api.oauth2.dto.OAuth2AccessTokenRespDTO;
import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeSendReqDTO;
import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeUseReqDTO;
@ -21,7 +22,7 @@ public interface AuthConvert {
SocialUserUnbindReqDTO convert(Long userId, Integer userType, AppSocialUserUnbindReqVO reqVO);
SmsCodeSendReqDTO convert(AppAuthSmsSendReqVO reqVO);
SmsCodeUseReqDTO convert(AppAuthResetPasswordReqVO reqVO, SmsSceneEnum scene, String usedIp);
SmsCodeUseReqDTO convert(AppMemberUserResetPasswordReqVO reqVO, SmsSceneEnum scene, String usedIp);
SmsCodeUseReqDTO convert(AppAuthSmsLoginReqVO reqVO, Integer scene, String usedIp);
AppAuthLoginRespVO convert(OAuth2AccessTokenRespDTO bean);

View File

@ -61,12 +61,6 @@ public interface MemberAuthService {
*/
String getSocialAuthorizeUrl(Integer type, String redirectUri);
/**
* 忘记密码
* @param userReqVO 用户请求实体类
*/
void resetPassword(AppAuthResetPasswordReqVO userReqVO);
/**
* 给用户发送短信验证码
*

View File

@ -27,7 +27,6 @@ import cn.iocoder.yudao.module.system.enums.oauth2.OAuth2ClientConstants;
import cn.iocoder.yudao.module.system.enums.sms.SmsSceneEnum;
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -61,8 +60,6 @@ public class MemberAuthServiceImpl implements MemberAuthService {
@Resource
private WxMaService wxMaService;
@Resource
private PasswordEncoder passwordEncoder;
@Resource
private MemberUserMapper userMapper;
@ -208,29 +205,22 @@ public class MemberAuthServiceImpl implements MemberAuthService {
createLogoutLog(accessTokenRespDTO.getUserId());
}
@Override
public void resetPassword(AppAuthResetPasswordReqVO reqVO) {
// 检验用户是否存在
MemberUserDO userDO = checkUserIfExists(reqVO.getMobile());
// 使用验证码
smsCodeApi.useSmsCode(AuthConvert.INSTANCE.convert(reqVO, SmsSceneEnum.MEMBER_FORGET_PASSWORD,
getClientIP()));
// 更新密码
userMapper.updateById(MemberUserDO.builder().id(userDO.getId())
.password(passwordEncoder.encode(reqVO.getPassword())).build());
}
@Override
public void sendSmsCode(Long userId, AppAuthSmsSendReqVO reqVO) {
// 如果是修改手机场景需要校验新手机号是否已经注册说明不能使用该手机了
// 情况 1如果是修改手机场景需要校验新手机号是否已经注册说明不能使用该手机了
if (Objects.equals(reqVO.getScene(), SmsSceneEnum.MEMBER_UPDATE_MOBILE.getScene())) {
MemberUserDO user = userMapper.selectByMobile(reqVO.getMobile());
MemberUserDO user = userService.getUserByMobile(reqVO.getMobile());
if (user != null && !Objects.equals(user.getId(), userId)) {
throw exception(AUTH_MOBILE_USED);
}
}
// 情况 2如果是重置密码场景需要校验手机号是存在的
if (Objects.equals(reqVO.getScene(), SmsSceneEnum.MEMBER_RESET_PASSWORD.getScene())) {
MemberUserDO user= userService.getUserByMobile(reqVO.getMobile());
if (user == null) {
throw exception(USER_MOBILE_NOT_EXISTS);
}
}
// 执行发送
smsCodeApi.sendSmsCode(AuthConvert.INSTANCE.convert(reqVO).setCreateIp(getClientIP()));
@ -248,14 +238,6 @@ public class MemberAuthServiceImpl implements MemberAuthService {
return AuthConvert.INSTANCE.convert(accessTokenDO);
}
public MemberUserDO checkUserIfExists(String mobile) {
MemberUserDO user = userMapper.selectByMobile(mobile);
if (user == null) {
throw exception(USER_NOT_EXISTS);
}
return user;
}
private void createLogoutLog(Long userId) {
LoginLogCreateReqDTO reqDTO = new LoginLogCreateReqDTO();
reqDTO.setLogType(LoginLogTypeEnum.LOGOUT_SELF.getType());

View File

@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.validation.Mobile;
import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserUpdateReqVO;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserResetPasswordReqVO;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdatePasswordReqVO;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdateReqVO;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdateMobileReqVO;
@ -94,6 +95,13 @@ public interface MemberUserService {
*/
void updateUserPassword(Long userId, AppMemberUserUpdatePasswordReqVO reqVO);
/**
* 会员忘记密码
*
* @param reqVO 请求信息
*/
void resetUserPassword(AppMemberUserResetPasswordReqVO reqVO);
/**
* 判断密码是否匹配
*

View File

@ -7,9 +7,11 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.infra.api.file.FileApi;
import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserUpdateReqVO;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserResetPasswordReqVO;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdatePasswordReqVO;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdateReqVO;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdateMobileReqVO;
import cn.iocoder.yudao.module.member.convert.auth.AuthConvert;
import cn.iocoder.yudao.module.member.convert.user.MemberUserConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.mysql.user.MemberUserMapper;
@ -141,6 +143,28 @@ public class MemberUserServiceImpl implements MemberUserService {
.password(passwordEncoder.encode(reqVO.getPassword())).build());
}
@Override
public void resetUserPassword(AppMemberUserResetPasswordReqVO reqVO) {
// 检验用户是否存在
MemberUserDO user = validateUserExists(reqVO.getMobile());
// 使用验证码
smsCodeApi.useSmsCode(AuthConvert.INSTANCE.convert(reqVO, SmsSceneEnum.MEMBER_RESET_PASSWORD,
getClientIP()));
// 更新密码
memberUserMapper.updateById(MemberUserDO.builder().id(user.getId())
.password(passwordEncoder.encode(reqVO.getPassword())).build());
}
private MemberUserDO validateUserExists(String mobile) {
MemberUserDO user = memberUserMapper.selectByMobile(mobile);
if (user == null) {
throw exception(USER_MOBILE_NOT_EXISTS);
}
return user;
}
@Override
public boolean isPasswordMatch(String rawPassword, String encodedPassword) {
return passwordEncoder.matches(rawPassword, encodedPassword);

View File

@ -5,7 +5,7 @@ import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils;
import cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbAndRedisUnitTest;
import cn.iocoder.yudao.module.member.controller.app.auth.vo.AppAuthResetPasswordReqVO;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserResetPasswordReqVO;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdatePasswordReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.mysql.user.MemberUserMapper;
@ -101,7 +101,7 @@ public class MemberAuthServiceTest extends BaseDbAndRedisUnitTest {
when(passwordEncoder.encode(password)).thenReturn(password);
// 更新用户密码
AppAuthResetPasswordReqVO reqVO = new AppAuthResetPasswordReqVO();
AppMemberUserResetPasswordReqVO reqVO = new AppMemberUserResetPasswordReqVO();
reqVO.setMobile(userDO.getMobile());
reqVO.setPassword(password);
reqVO.setCode(code);

View File

@ -19,7 +19,7 @@ public enum SmsSceneEnum implements IntArrayValuable {
MEMBER_LOGIN(1, "user-sms-login", "会员用户 - 手机号登陆"),
MEMBER_UPDATE_MOBILE(2, "user-update-mobile", "会员用户 - 修改手机"),
MEMBER_UPDATE_PASSWORD(3, "user-update-mobile", "会员用户 - 修改密码"),
MEMBER_FORGET_PASSWORD(4, "user-forget-password", "会员用户 - 忘记密码"),
MEMBER_RESET_PASSWORD(4, "user-reset-password", "会员用户 - 忘记密码"),
ADMIN_MEMBER_LOGIN(21, "admin-sms-login", "后台用户 - 手机号登录");