【更新】注册逻辑优化
This commit is contained in:
parent
0ea8d27694
commit
d3704ad147
|
|
@ -0,0 +1,9 @@
|
|||
package com.bonus.zlpt.system.api.constants;
|
||||
|
||||
import lombok.Data;
|
||||
@Data
|
||||
public class RedisConstants {
|
||||
|
||||
|
||||
public static final String REDIS_PHONE_CODE_KEY = "phone:code:";
|
||||
}
|
||||
|
|
@ -74,7 +74,7 @@ public class TokenController
|
|||
public R<?> register(@RequestBody RegisterBody registerBody)
|
||||
{
|
||||
// 用户注册
|
||||
sysLoginService.register(registerBody.getUsername(), registerBody.getPassword());
|
||||
sysLoginService.register(registerBody.getPhonenumber(),registerBody.getCode(),registerBody.getUsername(), registerBody.getPassword());
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,23 @@
|
|||
package com.bonus.zlpt.auth.form;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* 用户注册对象
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class RegisterBody extends LoginBody
|
||||
{
|
||||
|
||||
//用户手机号
|
||||
private String phonenumber;
|
||||
|
||||
|
||||
//验证码
|
||||
private String code;
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.bonus.zlpt.auth.service;
|
||||
|
||||
import com.bonus.zlpt.common.core.utils.DateUtils;
|
||||
import com.bonus.zlpt.system.api.constants.RedisConstants;
|
||||
import io.micrometer.core.instrument.util.TimeUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
|
@ -22,6 +23,8 @@ import com.bonus.zlpt.system.api.model.LoginUser;
|
|||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 登录校验方法
|
||||
|
|
@ -119,13 +122,27 @@ public class SysLoginService
|
|||
/**
|
||||
* 注册
|
||||
*/
|
||||
public void register(String username, String password)
|
||||
public void register(String phonenumber,String code,String username, String password)
|
||||
{
|
||||
// 用户名或密码为空 错误
|
||||
if (StringUtils.isAnyBlank(username, password))
|
||||
|
||||
|
||||
checkCode(phonenumber,code);
|
||||
|
||||
if (StringUtils.isAnyBlank(phonenumber,code,username, password))
|
||||
{
|
||||
throw new ServiceException("用户/密码必须填写");
|
||||
}
|
||||
|
||||
String mobileRegEx = "^1[3,4,5,6,7,8,9][0-9]{9}$";
|
||||
Pattern pattern = Pattern.compile(mobileRegEx);
|
||||
Matcher matcher = pattern.matcher(phonenumber);
|
||||
|
||||
if (!matcher.matches()) {
|
||||
throw new ServiceException("手机号格式错误");
|
||||
}
|
||||
|
||||
// 用户名或密码为空 错误
|
||||
|
||||
if (username.length() < UserConstants.USERNAME_MIN_LENGTH
|
||||
|| username.length() > UserConstants.USERNAME_MAX_LENGTH)
|
||||
{
|
||||
|
|
@ -172,4 +189,28 @@ public class SysLoginService
|
|||
|
||||
|
||||
|
||||
/**
|
||||
* 判断验证码是否存在Redis中
|
||||
*
|
||||
* @param phone 手机号码
|
||||
* @param code 验证码
|
||||
* @return 校验结果
|
||||
* @throws ServiceException 异常信息
|
||||
*/
|
||||
public boolean checkCode(String phone, String code) {
|
||||
|
||||
String redisCode = redisService.getCacheObject(RedisConstants.REDIS_PHONE_CODE_KEY + phone);
|
||||
if (StringUtils.isEmpty(redisCode)) {
|
||||
throw new ServiceException("验证码失效");
|
||||
}
|
||||
if (!StringUtils.equals(redisCode.split("_")[0], code)) {
|
||||
throw new ServiceException("验证码错误");
|
||||
} else {
|
||||
redisService.deleteObject(RedisConstants.REDIS_PHONE_CODE_KEY + phone);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,16 @@
|
|||
package com.bonus.zlpt.system.service.impl;
|
||||
|
||||
import com.bonus.zlpt.common.core.exception.ServiceException;
|
||||
import com.bonus.zlpt.common.core.utils.StringUtils;
|
||||
import com.bonus.zlpt.common.redis.service.RedisService;
|
||||
import com.bonus.zlpt.system.api.constants.RedisConstants;
|
||||
import com.bonus.zlpt.system.config.SmsComponent;
|
||||
import com.bonus.zlpt.system.service.ISmsService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Description: 短信发送接口实现
|
||||
|
|
@ -21,6 +25,9 @@ public class ISmsServiceImpl implements ISmsService {
|
|||
@Resource
|
||||
SmsComponent smsComponent;
|
||||
|
||||
@Resource
|
||||
private RedisService redisCache;
|
||||
|
||||
/**
|
||||
* @param phone 给手机号发送验证码
|
||||
* @param leastTime 短信有效时间
|
||||
|
|
@ -29,23 +36,22 @@ public class ISmsServiceImpl implements ISmsService {
|
|||
*/
|
||||
@Override
|
||||
public String sendCode(String phone, int leastTime) {
|
||||
|
||||
if (phone == null || phone.isEmpty()) {
|
||||
throw new ServiceException("手机号为空");
|
||||
}
|
||||
//从redis获取有效时间 如果相差小于1分钟不可以重新发送
|
||||
long expire = redisCache.getExpire(RedisConstants.REDIS_PHONE_CODE_KEY + phone);
|
||||
|
||||
// 判断是否已经发送过 (需要从Redis缓存中获取记录来进行判断,此处暂时去除)
|
||||
// if (!StringUtils.isEmpty(redisCode)) {
|
||||
// long time = Long.parseLong(redisCode.split("_")[1]);
|
||||
// if (System.currentTimeMillis() - time < leastTime) {
|
||||
// throw new ServiceException("发送频率过高");
|
||||
// }
|
||||
// }
|
||||
if (redisCache.hasKey(RedisConstants.REDIS_PHONE_CODE_KEY+phone) && (expire != -1 && leastTime - expire < 60000)) {
|
||||
|
||||
throw new ServiceException("发送频率过高");
|
||||
}
|
||||
|
||||
String code = getSixBitCode(); // 生成新的验证码
|
||||
//存储 phone -> code到Redis中
|
||||
// redisCache.setCacheObject(Constants.HEAD + phone, code + "_" + System.currentTimeMillis(), smsComponent.getTimeout(), TimeUnit.MINUTES);
|
||||
int minute = leastTime / 60 / 1000; //分钟
|
||||
smsComponent.sendCode(phone, code, Integer.toString(minute));
|
||||
redisCache.setCacheObject(RedisConstants.REDIS_PHONE_CODE_KEY + phone, code, Long.parseLong(leastTime+""), TimeUnit.MILLISECONDS);
|
||||
smsComponent.sendCode(phone, code,"");
|
||||
return "已发送验证码 " + phone;
|
||||
}
|
||||
|
||||
|
|
@ -73,15 +79,16 @@ public class ISmsServiceImpl implements ISmsService {
|
|||
*/
|
||||
@Override
|
||||
public boolean checkCode(String phone, String code) {
|
||||
// String redisCode = redisCache.getCacheObject(Constants.HEAD + phone);
|
||||
// if (StringUtils.isEmpty(redisCode)) {
|
||||
// throw new ServiceException("验证码失效");
|
||||
// }
|
||||
// if (!StringUtils.equals(redisCode.split("_")[0], code)) {
|
||||
// throw new ServiceException("验证码错误");
|
||||
// } else {
|
||||
// redisCache.deleteObject(Constants.HEAD + phone);
|
||||
|
||||
String redisCode = redisCache.getCacheObject(RedisConstants.REDIS_PHONE_CODE_KEY + phone);
|
||||
if (StringUtils.isEmpty(redisCode)) {
|
||||
throw new ServiceException("验证码失效");
|
||||
}
|
||||
if (!StringUtils.equals(redisCode.split("_")[0], code)) {
|
||||
throw new ServiceException("验证码错误");
|
||||
} else {
|
||||
redisCache.deleteObject(RedisConstants.REDIS_PHONE_CODE_KEY + phone);
|
||||
return true;
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue