From 6a7761313e21182d91096fc1fbe7953c976644a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E5=A4=A9?= <2982176321@qq.com> Date: Wed, 17 Nov 2021 15:12:59 +0800 Subject: [PATCH] =?UTF-8?q?[updaate]=20=E6=8B=86=E5=88=86=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=AF=86=E7=A0=81=E4=B8=8E=E9=87=8D=E7=BD=AE=E5=AF=86?= =?UTF-8?q?=E7=A0=81=E8=AF=B7=E6=B1=82=E5=AE=9E=E4=BD=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/auth/SysAuthController.java | 5 ++-- .../auth/vo/MbrAuthResetPasswordReqVO.java | 21 ++----------- .../auth/vo/MbrAuthUpdatePasswordReqVO.java | 30 +++++++++++++++++++ .../system/service/auth/SysAuthService.java | 2 +- .../service/auth/impl/SysAuthServiceImpl.java | 4 +-- .../service/MbrUserServiceImplTest.java | 1 - .../system/service/SysAuthServiceTest.java | 16 +++++----- 7 files changed, 47 insertions(+), 32 deletions(-) create mode 100644 yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthUpdatePasswordReqVO.java diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java index a4a4efb87..eb6e142ee 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java @@ -80,7 +80,8 @@ public class SysAuthController { @PostMapping("/reset-password") @ApiOperation(value = "重置密码", notes = "用户忘记密码时使用") - public CommonResult resetPassword(@RequestBody @Validated(MbrAuthResetPasswordReqVO.resetPasswordValidView.class) MbrAuthResetPasswordReqVO reqVO) { + @PreAuthenticated + public CommonResult resetPassword(@RequestBody @Valid MbrAuthResetPasswordReqVO reqVO) { authService.resetPassword(reqVO); return success(true); } @@ -88,7 +89,7 @@ public class SysAuthController { @PostMapping("/update-password") @ApiOperation(value = "修改用户密码",notes = "用户修改密码时使用") @PreAuthenticated - public CommonResult updatePassword(@RequestBody @Validated(MbrAuthResetPasswordReqVO.updatePasswordValidView.class) MbrAuthResetPasswordReqVO reqVO) { + public CommonResult updatePassword(@RequestBody @Valid MbrAuthUpdatePasswordReqVO reqVO) { authService.updatePassword(getLoginUserId(), reqVO); return success(true); } diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthResetPasswordReqVO.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthResetPasswordReqVO.java index f092eaf48..92fd8445f 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthResetPasswordReqVO.java +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthResetPasswordReqVO.java @@ -19,30 +19,13 @@ import javax.validation.constraints.Pattern; @Builder public class MbrAuthResetPasswordReqVO { - /** - * 修改密码校验规则 - */ - public interface updatePasswordValidView { - } - - /** - * 忘记密码校验规则 - */ - public interface resetPasswordValidView { - } - - @ApiModelProperty(value = "用户旧密码", required = true, example = "123456") - @NotBlank(message = "旧密码不能为空",groups = updatePasswordValidView.class) - @Length(min = 4, max = 16, message = "密码长度为 4-16 位") - private String oldPassword; - @ApiModelProperty(value = "新密码", required = true, example = "buzhidao") - @NotEmpty(message = "新密码不能为空",groups = {updatePasswordValidView.class,resetPasswordValidView.class}) + @NotEmpty(message = "新密码不能为空") @Length(min = 4, max = 16, message = "密码长度为 4-16 位") private String password; @ApiModelProperty(value = "手机验证码", required = true, example = "1024") - @NotEmpty(message = "手机验证码不能为空",groups = resetPasswordValidView.class) + @NotEmpty(message = "手机验证码不能为空") @Length(min = 4, max = 6, message = "手机验证码长度为 4-6 位") @Pattern(regexp = "^[0-9]+$", message = "手机验证码必须都是数字") private String code; diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthUpdatePasswordReqVO.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthUpdatePasswordReqVO.java new file mode 100644 index 000000000..b5cc0c785 --- /dev/null +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthUpdatePasswordReqVO.java @@ -0,0 +1,30 @@ +package cn.iocoder.yudao.userserver.modules.system.controller.auth.vo; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.hibernate.validator.constraints.Length; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotEmpty; + +@ApiModel("修改密码 Request VO") +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +public class MbrAuthUpdatePasswordReqVO { + + @ApiModelProperty(value = "用户旧密码", required = true, example = "123456") + @NotBlank(message = "旧密码不能为空") + @Length(min = 4, max = 16, message = "密码长度为 4-16 位") + private String oldPassword; + + @ApiModelProperty(value = "新密码", required = true, example = "buzhidao") + @NotEmpty(message = "新密码不能为空") + @Length(min = 4, max = 16, message = "密码长度为 4-16 位") + private String password; +} diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/SysAuthService.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/SysAuthService.java index d84d17558..33664d351 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/SysAuthService.java +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/SysAuthService.java @@ -68,7 +68,7 @@ public interface SysAuthService extends SecurityAuthFrameworkService { * @param userId 用户id * @param userReqVO 用户请求实体类 */ - void updatePassword(Long userId, MbrAuthResetPasswordReqVO userReqVO); + void updatePassword(Long userId, @Valid MbrAuthUpdatePasswordReqVO userReqVO); /** * 忘记密码 diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysAuthServiceImpl.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysAuthServiceImpl.java index bd8b57198..eb05f7086 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysAuthServiceImpl.java +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysAuthServiceImpl.java @@ -25,7 +25,6 @@ import cn.iocoder.yudao.userserver.modules.system.service.sms.SysSmsCodeService; import com.google.common.annotations.VisibleForTesting; import lombok.extern.slf4j.Slf4j; import me.zhyd.oauth.model.AuthUser; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.security.authentication.AuthenticationManager; @@ -41,6 +40,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; +import javax.validation.Valid; import java.util.List; import java.util.Objects; @@ -280,7 +280,7 @@ public class SysAuthServiceImpl implements SysAuthService { } @Override - public void updatePassword(Long userId, MbrAuthResetPasswordReqVO reqVO) { + public void updatePassword(Long userId, @Valid MbrAuthUpdatePasswordReqVO reqVO) { // 检验旧密码 MbrUserDO userDO = checkOldPassword(userId, reqVO.getOldPassword()); diff --git a/yudao-user-server/src/test/java/cn/iocoder/yudao/userserver/modules/member/service/MbrUserServiceImplTest.java b/yudao-user-server/src/test/java/cn/iocoder/yudao/userserver/modules/member/service/MbrUserServiceImplTest.java index dadb27f7d..0ed1e77e6 100644 --- a/yudao-user-server/src/test/java/cn/iocoder/yudao/userserver/modules/member/service/MbrUserServiceImplTest.java +++ b/yudao-user-server/src/test/java/cn/iocoder/yudao/userserver/modules/member/service/MbrUserServiceImplTest.java @@ -120,7 +120,6 @@ public class MbrUserServiceImplTest extends BaseDbAndRedisUnitTest { // 验证旧手机验证码是否正确 sysSmsCodeService.useSmsCode(oldMobile,SysSmsSceneEnum.CHANGE_MOBILE_BY_SMS.getScene(),"123","1.1.1.1"); - // 验证新手机 SysAuthSendSmsReqVO smsReqVO = new SysAuthSendSmsReqVO(); smsReqVO.setMobile(oldMobile); diff --git a/yudao-user-server/src/test/java/cn/iocoder/yudao/userserver/modules/system/service/SysAuthServiceTest.java b/yudao-user-server/src/test/java/cn/iocoder/yudao/userserver/modules/system/service/SysAuthServiceTest.java index 2c2d73c76..65fe3031c 100644 --- a/yudao-user-server/src/test/java/cn/iocoder/yudao/userserver/modules/system/service/SysAuthServiceTest.java +++ b/yudao-user-server/src/test/java/cn/iocoder/yudao/userserver/modules/system/service/SysAuthServiceTest.java @@ -16,6 +16,7 @@ import cn.iocoder.yudao.userserver.modules.member.dal.mysql.user.MbrUserMapper; import cn.iocoder.yudao.userserver.modules.member.service.user.MbrUserService; import cn.iocoder.yudao.userserver.modules.member.service.user.impl.MbrUserServiceImpl; import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.MbrAuthResetPasswordReqVO; +import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.MbrAuthUpdatePasswordReqVO; import cn.iocoder.yudao.userserver.modules.system.service.auth.SysAuthService; import cn.iocoder.yudao.userserver.modules.system.service.auth.impl.SysAuthServiceImpl; import cn.iocoder.yudao.userserver.modules.system.service.sms.SysSmsCodeService; @@ -78,9 +79,10 @@ public class SysAuthServiceTest extends BaseDbAndRedisUnitTest { String newPassword = randomString(); // 请求实体 - MbrAuthResetPasswordReqVO reqVO = new MbrAuthResetPasswordReqVO(); - reqVO.setOldPassword(userDO.getPassword()); - reqVO.setPassword(newPassword); + MbrAuthUpdatePasswordReqVO reqVO = MbrAuthUpdatePasswordReqVO.builder() + .oldPassword(userDO.getPassword()) + .password(newPassword) + .build(); // 测试桩 // 这两个相等是为了返回ture这个结果 @@ -103,10 +105,10 @@ public class SysAuthServiceTest extends BaseDbAndRedisUnitTest { // 随机验证码 String code = randomNumbers(4); - MbrAuthResetPasswordReqVO reqVO = new MbrAuthResetPasswordReqVO(); - reqVO.setPassword(password); - reqVO.setCode(code); - + MbrAuthResetPasswordReqVO reqVO = MbrAuthResetPasswordReqVO.builder() + .password(password) + .code(code) + .build(); // 放入code+手机号 stringRedisTemplate.opsForValue().set(code,userDO.getMobile(),10, TimeUnit.MINUTES);