Commit 21fbe91e39f029174377699110d149d1d2e684c1
1 parent
5376ed69
update 用户中心密码操作修改逻辑
Showing
7 changed files
with
64 additions
and
77 deletions
yudao-admin-server/src/main/resources/application-local.yaml
| @@ -44,16 +44,16 @@ spring: | @@ -44,16 +44,16 @@ spring: | ||
| 44 | datasource: | 44 | datasource: |
| 45 | master: | 45 | master: |
| 46 | name: ruoyi-vue-pro | 46 | name: ruoyi-vue-pro |
| 47 | - url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.master.name}?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT | ||
| 48 | - driver-class-name: com.mysql.jdbc.Driver | ||
| 49 | - username: root | ||
| 50 | - password: 123456 | ||
| 51 | - slave: # 模拟从库,可根据自己需要修改 | ||
| 52 | - name: ruoyi-vue-pro | ||
| 53 | - url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.slave.name}?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT | 47 | + url: jdbc:mysql://127.0.0.1:3306/ruoyi-vue-pro?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT |
| 54 | driver-class-name: com.mysql.jdbc.Driver | 48 | driver-class-name: com.mysql.jdbc.Driver |
| 55 | username: root | 49 | username: root |
| 56 | password: 123456 | 50 | password: 123456 |
| 51 | +# slave: # 模拟从库,可根据自己需要修改 | ||
| 52 | +# name: ruoyi-vue-pro | ||
| 53 | +# url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.slave.name}?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT | ||
| 54 | +# driver-class-name: com.mysql.jdbc.Driver | ||
| 55 | +# username: root | ||
| 56 | +# password: 123456 | ||
| 57 | 57 | ||
| 58 | 58 | ||
| 59 | activiti: | 59 | activiti: |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/service/user/impl/MbrUserServiceImpl.java
| @@ -4,20 +4,20 @@ import cn.hutool.core.io.IoUtil; | @@ -4,20 +4,20 @@ import cn.hutool.core.io.IoUtil; | ||
| 4 | import cn.hutool.core.util.IdUtil; | 4 | import cn.hutool.core.util.IdUtil; |
| 5 | import cn.iocoder.yudao.coreservice.modules.infra.service.file.InfFileCoreService; | 5 | import cn.iocoder.yudao.coreservice.modules.infra.service.file.InfFileCoreService; |
| 6 | import cn.iocoder.yudao.coreservice.modules.member.dal.dataobject.user.MbrUserDO; | 6 | import cn.iocoder.yudao.coreservice.modules.member.dal.dataobject.user.MbrUserDO; |
| 7 | -import cn.iocoder.yudao.userserver.modules.member.controller.user.vo.MbrUserInfoRespVO; | ||
| 8 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | 7 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 8 | +import cn.iocoder.yudao.userserver.modules.member.controller.user.vo.MbrUserInfoRespVO; | ||
| 9 | import cn.iocoder.yudao.userserver.modules.member.controller.user.vo.MbrUserUpdateMobileReqVO; | 9 | import cn.iocoder.yudao.userserver.modules.member.controller.user.vo.MbrUserUpdateMobileReqVO; |
| 10 | import cn.iocoder.yudao.userserver.modules.member.convert.user.UserProfileConvert; | 10 | import cn.iocoder.yudao.userserver.modules.member.convert.user.UserProfileConvert; |
| 11 | import cn.iocoder.yudao.userserver.modules.member.dal.mysql.user.MbrUserMapper; | 11 | import cn.iocoder.yudao.userserver.modules.member.dal.mysql.user.MbrUserMapper; |
| 12 | import cn.iocoder.yudao.userserver.modules.member.service.user.MbrUserService; | 12 | import cn.iocoder.yudao.userserver.modules.member.service.user.MbrUserService; |
| 13 | import cn.iocoder.yudao.userserver.modules.system.dal.dataobject.sms.SysSmsCodeDO; | 13 | import cn.iocoder.yudao.userserver.modules.system.dal.dataobject.sms.SysSmsCodeDO; |
| 14 | import cn.iocoder.yudao.userserver.modules.system.enums.sms.SysSmsSceneEnum; | 14 | import cn.iocoder.yudao.userserver.modules.system.enums.sms.SysSmsSceneEnum; |
| 15 | -import cn.iocoder.yudao.userserver.modules.system.service.auth.SysAuthService; | ||
| 16 | import cn.iocoder.yudao.userserver.modules.system.service.sms.SysSmsCodeService; | 15 | import cn.iocoder.yudao.userserver.modules.system.service.sms.SysSmsCodeService; |
| 17 | import com.google.common.annotations.VisibleForTesting; | 16 | import com.google.common.annotations.VisibleForTesting; |
| 18 | import lombok.extern.slf4j.Slf4j; | 17 | import lombok.extern.slf4j.Slf4j; |
| 19 | import org.springframework.security.crypto.password.PasswordEncoder; | 18 | import org.springframework.security.crypto.password.PasswordEncoder; |
| 20 | import org.springframework.stereotype.Service; | 19 | import org.springframework.stereotype.Service; |
| 20 | +import org.springframework.transaction.annotation.Transactional; | ||
| 21 | 21 | ||
| 22 | import javax.annotation.Resource; | 22 | import javax.annotation.Resource; |
| 23 | import javax.validation.Valid; | 23 | import javax.validation.Valid; |
| @@ -28,7 +28,6 @@ import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionU | @@ -28,7 +28,6 @@ import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionU | ||
| 28 | import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP; | 28 | import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP; |
| 29 | import static cn.iocoder.yudao.userserver.modules.member.enums.MbrErrorCodeConstants.USER_NOT_EXISTS; | 29 | import static cn.iocoder.yudao.userserver.modules.member.enums.MbrErrorCodeConstants.USER_NOT_EXISTS; |
| 30 | import static cn.iocoder.yudao.userserver.modules.system.enums.SysErrorCodeConstants.USER_SMS_CODE_IS_UNUSED; | 30 | import static cn.iocoder.yudao.userserver.modules.system.enums.SysErrorCodeConstants.USER_SMS_CODE_IS_UNUSED; |
| 31 | -import static cn.iocoder.yudao.userserver.modules.system.enums.SysErrorCodeConstants.USER_SMS_CODE_NOT_CORRECT; | ||
| 32 | 31 | ||
| 33 | /** | 32 | /** |
| 34 | * User Service 实现类 | 33 | * User Service 实现类 |
| @@ -50,9 +49,6 @@ public class MbrUserServiceImpl implements MbrUserService { | @@ -50,9 +49,6 @@ public class MbrUserServiceImpl implements MbrUserService { | ||
| 50 | private PasswordEncoder passwordEncoder; | 49 | private PasswordEncoder passwordEncoder; |
| 51 | 50 | ||
| 52 | @Resource | 51 | @Resource |
| 53 | - private SysAuthService sysAuthService; | ||
| 54 | - | ||
| 55 | - @Resource | ||
| 56 | private SysSmsCodeService smsCodeService; | 52 | private SysSmsCodeService smsCodeService; |
| 57 | 53 | ||
| 58 | 54 | ||
| @@ -130,24 +126,23 @@ public class MbrUserServiceImpl implements MbrUserService { | @@ -130,24 +126,23 @@ public class MbrUserServiceImpl implements MbrUserService { | ||
| 130 | } | 126 | } |
| 131 | 127 | ||
| 132 | @Override | 128 | @Override |
| 129 | + @Transactional(rollbackFor = Exception.class) | ||
| 133 | public void updateMobile(Long userId, MbrUserUpdateMobileReqVO reqVO) { | 130 | public void updateMobile(Long userId, MbrUserUpdateMobileReqVO reqVO) { |
| 134 | // 检测用户是否存在 | 131 | // 检测用户是否存在 |
| 135 | checkUserExists(userId); | 132 | checkUserExists(userId); |
| 136 | - // 校验验证码,并标记为已使用 | ||
| 137 | - smsCodeService.useSmsCode(reqVO.getMobile(), SysSmsSceneEnum.CHANGE_MOBILE_BY_SMS.getScene(), reqVO.getCode(),getClientIP()); | ||
| 138 | - | ||
| 139 | - // 检测新手机和旧手机的验证码是否在30分钟内 | ||
| 140 | - SysSmsCodeDO smsOldCodeDO = smsCodeService.checkCodeIsExpired(reqVO.getOldMobile(), reqVO.getOldCode(), SysSmsSceneEnum.CHANGE_MOBILE_BY_SMS.getScene()); | ||
| 141 | - SysSmsCodeDO smsNewCodeDO = smsCodeService.checkCodeIsExpired(reqVO.getMobile(), reqVO.getCode(), SysSmsSceneEnum.CHANGE_MOBILE_BY_SMS.getScene()); | ||
| 142 | 133 | ||
| 143 | - // 判断新旧code是否未被使用,如果是,抛出异常 | ||
| 144 | - if (Boolean.FALSE.equals(smsOldCodeDO.getUsed()) || Boolean.FALSE.equals(smsNewCodeDO.getUsed())){ | 134 | + // 校验旧手机和旧验证码 |
| 135 | + SysSmsCodeDO sysSmsCodeDO = smsCodeService.checkCodeIsExpired(reqVO.getOldMobile(), reqVO.getOldCode(), SysSmsSceneEnum.CHANGE_MOBILE_BY_SMS.getScene()); | ||
| 136 | + // 判断旧code是否未被使用,如果是,抛出异常 | ||
| 137 | + if (Boolean.FALSE.equals(sysSmsCodeDO.getUsed())){ | ||
| 145 | throw exception(USER_SMS_CODE_IS_UNUSED); | 138 | throw exception(USER_SMS_CODE_IS_UNUSED); |
| 146 | } | 139 | } |
| 147 | 140 | ||
| 141 | + // 使用新验证码 | ||
| 142 | + smsCodeService.useSmsCode(reqVO.getMobile(), SysSmsSceneEnum.CHANGE_MOBILE_BY_SMS.getScene(), reqVO.getCode(),getClientIP()); | ||
| 143 | + | ||
| 148 | // 更新用户手机 | 144 | // 更新用户手机 |
| 149 | - MbrUserDO userDO = MbrUserDO.builder().build(); | ||
| 150 | - userDO.setMobile(reqVO.getMobile()); | 145 | + MbrUserDO userDO = MbrUserDO.builder().id(userId).mobile(reqVO.getMobile()).build(); |
| 151 | userMapper.updateById(userDO); | 146 | userMapper.updateById(userDO); |
| 152 | } | 147 | } |
| 153 | 148 |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/enums/SysErrorCodeConstants.java
| @@ -24,7 +24,7 @@ public interface SysErrorCodeConstants { | @@ -24,7 +24,7 @@ public interface SysErrorCodeConstants { | ||
| 24 | ErrorCode USER_SMS_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY = new ErrorCode(1005001004, "超过每日短信发送数量"); | 24 | ErrorCode USER_SMS_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY = new ErrorCode(1005001004, "超过每日短信发送数量"); |
| 25 | ErrorCode USER_SMS_CODE_SEND_TOO_FAST = new ErrorCode(1005001005, "短信发送过于频率"); | 25 | ErrorCode USER_SMS_CODE_SEND_TOO_FAST = new ErrorCode(1005001005, "短信发送过于频率"); |
| 26 | ErrorCode USER_SMS_CODE_IS_EXISTS = new ErrorCode(1005001006, "手机号已被使用"); | 26 | ErrorCode USER_SMS_CODE_IS_EXISTS = new ErrorCode(1005001006, "手机号已被使用"); |
| 27 | - ErrorCode USER_SMS_CODE_IS_UNUSED = new ErrorCode(1005001006, "手机号未被使用"); | 27 | + ErrorCode USER_SMS_CODE_IS_UNUSED = new ErrorCode(1005001006, "验证码未被使用"); |
| 28 | 28 | ||
| 29 | // ========== 用户模块 1005002000 ========== | 29 | // ========== 用户模块 1005002000 ========== |
| 30 | ErrorCode USER_NOT_EXISTS = new ErrorCode(1005002001, "用户不存在"); | 30 | ErrorCode USER_NOT_EXISTS = new ErrorCode(1005002001, "用户不存在"); |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/sms/SysSmsCodeService.java
| @@ -2,9 +2,7 @@ package cn.iocoder.yudao.userserver.modules.system.service.sms; | @@ -2,9 +2,7 @@ package cn.iocoder.yudao.userserver.modules.system.service.sms; | ||
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.common.exception.ServiceException; | 3 | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| 4 | import cn.iocoder.yudao.framework.common.validation.Mobile; | 4 | import cn.iocoder.yudao.framework.common.validation.Mobile; |
| 5 | -import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.SysAuthCheckCodeReqVO; | ||
| 6 | import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.SysAuthSendSmsReqVO; | 5 | import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.SysAuthSendSmsReqVO; |
| 7 | -import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.SysAuthSmsLoginReqVO; | ||
| 8 | import cn.iocoder.yudao.userserver.modules.system.dal.dataobject.sms.SysSmsCodeDO; | 6 | import cn.iocoder.yudao.userserver.modules.system.dal.dataobject.sms.SysSmsCodeDO; |
| 9 | import cn.iocoder.yudao.userserver.modules.system.enums.sms.SysSmsSceneEnum; | 7 | import cn.iocoder.yudao.userserver.modules.system.enums.sms.SysSmsSceneEnum; |
| 10 | 8 | ||
| @@ -18,14 +16,15 @@ public interface SysSmsCodeService { | @@ -18,14 +16,15 @@ public interface SysSmsCodeService { | ||
| 18 | /** | 16 | /** |
| 19 | * 创建短信验证码,并进行发送 | 17 | * 创建短信验证码,并进行发送 |
| 20 | * | 18 | * |
| 21 | - * @param mobile 手机号 | ||
| 22 | - * @param scene 发送场景 {@link SysSmsSceneEnum} | 19 | + * @param mobile 手机号 |
| 20 | + * @param scene 发送场景 {@link SysSmsSceneEnum} | ||
| 23 | * @param createIp 发送 IP | 21 | * @param createIp 发送 IP |
| 24 | */ | 22 | */ |
| 25 | void sendSmsCode(@Mobile String mobile, Integer scene, String createIp); | 23 | void sendSmsCode(@Mobile String mobile, Integer scene, String createIp); |
| 26 | 24 | ||
| 27 | /** | 25 | /** |
| 28 | * 发送短信验证码,并检测手机号是否已被注册 | 26 | * 发送短信验证码,并检测手机号是否已被注册 |
| 27 | + * | ||
| 29 | * @param reqVO 请求实体 | 28 | * @param reqVO 请求实体 |
| 30 | */ | 29 | */ |
| 31 | void sendSmsNewCode(SysAuthSendSmsReqVO reqVO); | 30 | void sendSmsNewCode(SysAuthSendSmsReqVO reqVO); |
| @@ -36,25 +35,26 @@ public interface SysSmsCodeService { | @@ -36,25 +35,26 @@ public interface SysSmsCodeService { | ||
| 36 | * 如果错误,则抛出 {@link ServiceException} 异常 | 35 | * 如果错误,则抛出 {@link ServiceException} 异常 |
| 37 | * | 36 | * |
| 38 | * @param mobile 手机号 | 37 | * @param mobile 手机号 |
| 39 | - * @param scene 发送场景 | ||
| 40 | - * @param code 验证码 | 38 | + * @param scene 发送场景 |
| 39 | + * @param code 验证码 | ||
| 41 | * @param usedIp 使用 IP | 40 | * @param usedIp 使用 IP |
| 42 | */ | 41 | */ |
| 43 | void useSmsCode(@Mobile String mobile, Integer scene, String code, String usedIp); | 42 | void useSmsCode(@Mobile String mobile, Integer scene, String code, String usedIp); |
| 44 | 43 | ||
| 45 | /** | 44 | /** |
| 46 | * 根据用户id发送验证码 | 45 | * 根据用户id发送验证码 |
| 46 | + * | ||
| 47 | * @param userId 用户id | 47 | * @param userId 用户id |
| 48 | */ | 48 | */ |
| 49 | void sendSmsCodeLogin(Long userId); | 49 | void sendSmsCodeLogin(Long userId); |
| 50 | 50 | ||
| 51 | + | ||
| 51 | /** | 52 | /** |
| 52 | - * 检测手机验证码是否有效 | ||
| 53 | - * @param mobile 手机号 | 53 | + * 检查验证码是否有效 |
| 54 | + * @param mobile 手机 | ||
| 54 | * @param code 验证码 | 55 | * @param code 验证码 |
| 55 | - * @param scene 发送场景 {@link SysSmsSceneEnum} | ||
| 56 | - * @return SysSmsCodeDO 手机验证码 | 56 | + * @param scene 使用场景 |
| 57 | + * @return 验证码记录 | ||
| 57 | */ | 58 | */ |
| 58 | SysSmsCodeDO checkCodeIsExpired(String mobile, String code, Integer scene); | 59 | SysSmsCodeDO checkCodeIsExpired(String mobile, String code, Integer scene); |
| 59 | - | ||
| 60 | } | 60 | } |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/sms/impl/SysSmsCodeServiceImpl.java
| @@ -4,7 +4,6 @@ import cn.hutool.core.map.MapUtil; | @@ -4,7 +4,6 @@ import cn.hutool.core.map.MapUtil; | ||
| 4 | import cn.iocoder.yudao.coreservice.modules.member.dal.dataobject.user.MbrUserDO; | 4 | import cn.iocoder.yudao.coreservice.modules.member.dal.dataobject.user.MbrUserDO; |
| 5 | import cn.iocoder.yudao.coreservice.modules.system.service.sms.SysSmsCoreService; | 5 | import cn.iocoder.yudao.coreservice.modules.system.service.sms.SysSmsCoreService; |
| 6 | import cn.iocoder.yudao.userserver.modules.member.service.user.MbrUserService; | 6 | import cn.iocoder.yudao.userserver.modules.member.service.user.MbrUserService; |
| 7 | -import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.SysAuthCheckCodeReqVO; | ||
| 8 | import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.SysAuthSendSmsReqVO; | 7 | import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.SysAuthSendSmsReqVO; |
| 9 | import cn.iocoder.yudao.userserver.modules.system.dal.dataobject.sms.SysSmsCodeDO; | 8 | import cn.iocoder.yudao.userserver.modules.system.dal.dataobject.sms.SysSmsCodeDO; |
| 10 | import cn.iocoder.yudao.userserver.modules.system.dal.mysql.sms.SysSmsCodeMapper; | 9 | import cn.iocoder.yudao.userserver.modules.system.dal.mysql.sms.SysSmsCodeMapper; |
| @@ -92,21 +91,14 @@ public class SysSmsCodeServiceImpl implements SysSmsCodeService { | @@ -92,21 +91,14 @@ public class SysSmsCodeServiceImpl implements SysSmsCodeService { | ||
| 92 | 91 | ||
| 93 | @Override | 92 | @Override |
| 94 | public void useSmsCode(String mobile, Integer scene, String code, String usedIp) { | 93 | public void useSmsCode(String mobile, Integer scene, String code, String usedIp) { |
| 95 | - // 校验验证码 | ||
| 96 | - SysSmsCodeDO lastSmsCode = smsCodeMapper.selectLastByMobile(mobile, null,scene); | ||
| 97 | - if (lastSmsCode == null) { // 若验证码不存在,抛出异常 | ||
| 98 | - throw exception(USER_SMS_CODE_NOT_FOUND); | ||
| 99 | - } | ||
| 100 | - if (System.currentTimeMillis() - lastSmsCode.getCreateTime().getTime() | ||
| 101 | - >= smsCodeProperties.getExpireTimes().toMillis()) { // 验证码已过期 | ||
| 102 | - throw exception(USER_SMS_CODE_EXPIRED); | ||
| 103 | - } | ||
| 104 | - if (lastSmsCode.getUsed()) { // 验证码已使用 | 94 | + |
| 95 | + // 检测验证码是否有效 | ||
| 96 | + SysSmsCodeDO lastSmsCode = this.checkCodeIsExpired(mobile, code, scene); | ||
| 97 | + | ||
| 98 | + // 判断验证码是否已被使用 | ||
| 99 | + if (Boolean.TRUE.equals(lastSmsCode.getUsed())) { | ||
| 105 | throw exception(USER_SMS_CODE_USED); | 100 | throw exception(USER_SMS_CODE_USED); |
| 106 | } | 101 | } |
| 107 | - if (!lastSmsCode.getCode().equals(code)) { | ||
| 108 | - throw exception(USER_SMS_CODE_NOT_CORRECT); | ||
| 109 | - } | ||
| 110 | 102 | ||
| 111 | // 使用验证码 | 103 | // 使用验证码 |
| 112 | smsCodeMapper.updateById(SysSmsCodeDO.builder().id(lastSmsCode.getId()) | 104 | smsCodeMapper.updateById(SysSmsCodeDO.builder().id(lastSmsCode.getId()) |
| @@ -125,19 +117,20 @@ public class SysSmsCodeServiceImpl implements SysSmsCodeService { | @@ -125,19 +117,20 @@ public class SysSmsCodeServiceImpl implements SysSmsCodeService { | ||
| 125 | 117 | ||
| 126 | @Override | 118 | @Override |
| 127 | public SysSmsCodeDO checkCodeIsExpired(String mobile, String code, Integer scene) { | 119 | public SysSmsCodeDO checkCodeIsExpired(String mobile, String code, Integer scene) { |
| 128 | - SysSmsCodeDO lastSmsCode = smsCodeMapper.selectLastByMobile(mobile, code, scene); | 120 | + // 校验验证码 |
| 121 | + SysSmsCodeDO lastSmsCode = smsCodeMapper.selectLastByMobile(mobile,code,scene); | ||
| 129 | 122 | ||
| 130 | - // 检测验证码是否存在 | ||
| 131 | - if (lastSmsCode == null){ | ||
| 132 | - throw exception(USER_SMS_CODE_EXPIRED); | 123 | + // 若验证码不存在,抛出异常 |
| 124 | + if (lastSmsCode == null) { | ||
| 125 | + throw exception(USER_SMS_CODE_NOT_FOUND); | ||
| 133 | } | 126 | } |
| 134 | - | ||
| 135 | - // 检测验证码是否过期 | ||
| 136 | if (System.currentTimeMillis() - lastSmsCode.getCreateTime().getTime() | 127 | if (System.currentTimeMillis() - lastSmsCode.getCreateTime().getTime() |
| 137 | - >= smsCodeProperties.getExpireTimes().toMillis()) { | 128 | + >= smsCodeProperties.getExpireTimes().toMillis()) { // 验证码已过期 |
| 138 | throw exception(USER_SMS_CODE_EXPIRED); | 129 | throw exception(USER_SMS_CODE_EXPIRED); |
| 139 | } | 130 | } |
| 131 | + | ||
| 140 | return lastSmsCode; | 132 | return lastSmsCode; |
| 133 | + | ||
| 141 | } | 134 | } |
| 142 | 135 | ||
| 143 | } | 136 | } |
yudao-user-server/src/test/java/cn/iocoder/yudao/userserver/modules/member/service/MbrUserServiceImplTest.java
| 1 | package cn.iocoder.yudao.userserver.modules.member.service; | 1 | package cn.iocoder.yudao.userserver.modules.member.service; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.util.RandomUtil; | ||
| 3 | import cn.iocoder.yudao.coreservice.modules.infra.service.file.InfFileCoreService; | 4 | import cn.iocoder.yudao.coreservice.modules.infra.service.file.InfFileCoreService; |
| 4 | import cn.iocoder.yudao.coreservice.modules.member.dal.dataobject.user.MbrUserDO; | 5 | import cn.iocoder.yudao.coreservice.modules.member.dal.dataobject.user.MbrUserDO; |
| 5 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | 6 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| @@ -10,8 +11,7 @@ import cn.iocoder.yudao.userserver.modules.member.controller.user.vo.MbrUserInfo | @@ -10,8 +11,7 @@ import cn.iocoder.yudao.userserver.modules.member.controller.user.vo.MbrUserInfo | ||
| 10 | import cn.iocoder.yudao.userserver.modules.member.controller.user.vo.MbrUserUpdateMobileReqVO; | 11 | import cn.iocoder.yudao.userserver.modules.member.controller.user.vo.MbrUserUpdateMobileReqVO; |
| 11 | import cn.iocoder.yudao.userserver.modules.member.dal.mysql.user.MbrUserMapper; | 12 | import cn.iocoder.yudao.userserver.modules.member.dal.mysql.user.MbrUserMapper; |
| 12 | import cn.iocoder.yudao.userserver.modules.member.service.user.impl.MbrUserServiceImpl; | 13 | import cn.iocoder.yudao.userserver.modules.member.service.user.impl.MbrUserServiceImpl; |
| 13 | -import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.SysAuthSendSmsReqVO; | ||
| 14 | -import cn.iocoder.yudao.userserver.modules.system.enums.sms.SysSmsSceneEnum; | 14 | +import cn.iocoder.yudao.userserver.modules.system.dal.dataobject.sms.SysSmsCodeDO; |
| 15 | import cn.iocoder.yudao.userserver.modules.system.service.auth.impl.SysAuthServiceImpl; | 15 | import cn.iocoder.yudao.userserver.modules.system.service.auth.impl.SysAuthServiceImpl; |
| 16 | import cn.iocoder.yudao.userserver.modules.system.service.sms.SysSmsCodeService; | 16 | import cn.iocoder.yudao.userserver.modules.system.service.sms.SysSmsCodeService; |
| 17 | import org.junit.jupiter.api.Test; | 17 | import org.junit.jupiter.api.Test; |
| @@ -27,6 +27,7 @@ import java.util.function.Consumer; | @@ -27,6 +27,7 @@ import java.util.function.Consumer; | ||
| 27 | import static cn.hutool.core.util.RandomUtil.*; | 27 | import static cn.hutool.core.util.RandomUtil.*; |
| 28 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; | 28 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; |
| 29 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString; | 29 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString; |
| 30 | +import static cn.iocoder.yudao.userserver.modules.system.enums.sms.SysSmsSceneEnum.CHANGE_MOBILE_BY_SMS; | ||
| 30 | import static org.junit.jupiter.api.Assertions.assertEquals; | 31 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 31 | import static org.mockito.Mockito.*; | 32 | import static org.mockito.Mockito.*; |
| 32 | 33 | ||
| @@ -115,23 +116,24 @@ public class MbrUserServiceImplTest extends BaseDbAndRedisUnitTest { | @@ -115,23 +116,24 @@ public class MbrUserServiceImplTest extends BaseDbAndRedisUnitTest { | ||
| 115 | userDO.setMobile(oldMobile); | 116 | userDO.setMobile(oldMobile); |
| 116 | userMapper.insert(userDO); | 117 | userMapper.insert(userDO); |
| 117 | 118 | ||
| 118 | - // 验证旧手机 | ||
| 119 | - sysSmsCodeService.sendSmsCodeLogin(userDO.getId()); | ||
| 120 | 119 | ||
| 121 | - // 验证旧手机验证码是否正确 | ||
| 122 | - sysSmsCodeService.useSmsCode(oldMobile,SysSmsSceneEnum.CHANGE_MOBILE_BY_SMS.getScene(),"123","1.1.1.1"); | ||
| 123 | - // 验证新手机 | ||
| 124 | - SysAuthSendSmsReqVO smsReqVO = new SysAuthSendSmsReqVO(); | ||
| 125 | - smsReqVO.setMobile(oldMobile); | ||
| 126 | - smsReqVO.setScene(SysSmsSceneEnum.CHANGE_MOBILE_BY_SMS.getScene()); | ||
| 127 | - sysSmsCodeService.sendSmsNewCode(smsReqVO); | 120 | + // 旧手机和旧验证码 |
| 121 | + SysSmsCodeDO codeDO = new SysSmsCodeDO(); | ||
| 122 | + String oldCode = RandomUtil.randomString(4); | ||
| 123 | + codeDO.setMobile(userDO.getMobile()); | ||
| 124 | + codeDO.setCode(oldCode); | ||
| 125 | + codeDO.setScene(CHANGE_MOBILE_BY_SMS.getScene()); | ||
| 126 | + codeDO.setUsed(Boolean.FALSE); | ||
| 127 | + when(sysSmsCodeService.checkCodeIsExpired(codeDO.getMobile(),codeDO.getCode(),codeDO.getScene())).thenReturn(codeDO); | ||
| 128 | 128 | ||
| 129 | // 更新手机号 | 129 | // 更新手机号 |
| 130 | String newMobile = randomNumbers(11); | 130 | String newMobile = randomNumbers(11); |
| 131 | - String code = randomNumbers(4); | 131 | + String newCode = randomNumbers(4); |
| 132 | MbrUserUpdateMobileReqVO reqVO = new MbrUserUpdateMobileReqVO(); | 132 | MbrUserUpdateMobileReqVO reqVO = new MbrUserUpdateMobileReqVO(); |
| 133 | reqVO.setMobile(newMobile); | 133 | reqVO.setMobile(newMobile); |
| 134 | - reqVO.setCode(code); | 134 | + reqVO.setCode(newCode); |
| 135 | + reqVO.setOldMobile(oldMobile); | ||
| 136 | + reqVO.setOldCode(oldCode); | ||
| 135 | mbrUserService.updateMobile(userDO.getId(),reqVO); | 137 | mbrUserService.updateMobile(userDO.getId(),reqVO); |
| 136 | 138 | ||
| 137 | assertEquals(mbrUserService.getUser(userDO.getId()).getMobile(),newMobile); | 139 | assertEquals(mbrUserService.getUser(userDO.getId()).getMobile(),newMobile); |
yudao-user-server/src/test/java/cn/iocoder/yudao/userserver/modules/system/service/SysAuthServiceTest.java
| @@ -23,7 +23,6 @@ import org.springframework.security.authentication.AuthenticationManager; | @@ -23,7 +23,6 @@ import org.springframework.security.authentication.AuthenticationManager; | ||
| 23 | import org.springframework.security.crypto.password.PasswordEncoder; | 23 | import org.springframework.security.crypto.password.PasswordEncoder; |
| 24 | 24 | ||
| 25 | import javax.annotation.Resource; | 25 | import javax.annotation.Resource; |
| 26 | -import java.util.concurrent.TimeUnit; | ||
| 27 | import java.util.function.Consumer; | 26 | import java.util.function.Consumer; |
| 28 | 27 | ||
| 29 | import static cn.hutool.core.util.RandomUtil.randomEle; | 28 | import static cn.hutool.core.util.RandomUtil.randomEle; |
| @@ -99,17 +98,15 @@ public class SysAuthServiceTest extends BaseDbAndRedisUnitTest { | @@ -99,17 +98,15 @@ public class SysAuthServiceTest extends BaseDbAndRedisUnitTest { | ||
| 99 | // 随机验证码 | 98 | // 随机验证码 |
| 100 | String code = randomNumbers(4); | 99 | String code = randomNumbers(4); |
| 101 | 100 | ||
| 102 | - MbrAuthResetPasswordReqVO reqVO = MbrAuthResetPasswordReqVO.builder() | ||
| 103 | - .password(password) | ||
| 104 | - .code(code) | ||
| 105 | - .build(); | ||
| 106 | - // 放入code+手机号 | ||
| 107 | - stringRedisTemplate.opsForValue().set(code,userDO.getMobile(),10, TimeUnit.MINUTES); | ||
| 108 | - | ||
| 109 | // mock | 101 | // mock |
| 110 | when(passwordEncoder.encode(password)).thenReturn(password); | 102 | when(passwordEncoder.encode(password)).thenReturn(password); |
| 111 | 103 | ||
| 112 | // 更新用户密码 | 104 | // 更新用户密码 |
| 105 | + MbrAuthResetPasswordReqVO reqVO = new MbrAuthResetPasswordReqVO(); | ||
| 106 | + reqVO.setMobile(userDO.getMobile()); | ||
| 107 | + reqVO.setPassword(password); | ||
| 108 | + reqVO.setCode(code); | ||
| 109 | + | ||
| 113 | authService.resetPassword(reqVO); | 110 | authService.resetPassword(reqVO); |
| 114 | assertEquals(mbrUserMapper.selectById(userDO.getId()).getPassword(),password); | 111 | assertEquals(mbrUserMapper.selectById(userDO.getId()).getPassword(),password); |
| 115 | } | 112 | } |