【改进】账号类型校验
This commit is contained in:
@@ -17,7 +17,7 @@ public class SendCodeRequest {
|
||||
private String account;
|
||||
|
||||
/**
|
||||
* 验证码类型:register-注册, login-登录, reset-重置密码
|
||||
* 验证码类型:register-注册, login-登录, reset-重置密码, forget-忘记密码
|
||||
*/
|
||||
@NotBlank(message = "验证码类型不能为空")
|
||||
private String type;
|
||||
|
||||
@@ -1,16 +1,12 @@
|
||||
package com.corewing.app.service.impl;
|
||||
|
||||
import com.corewing.app.service.VerifyCodeService;
|
||||
import com.corewing.app.util.EmailUtil;
|
||||
import com.corewing.app.util.I18nUtil;
|
||||
import com.corewing.app.util.RedisUtil;
|
||||
import com.corewing.app.util.SmsBaoUtil;
|
||||
import com.corewing.app.util.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 验证码服务实现类
|
||||
@@ -33,16 +29,6 @@ public class VerifyCodeServiceImpl implements VerifyCodeService {
|
||||
*/
|
||||
private static final int CODE_LENGTH = 6;
|
||||
|
||||
/**
|
||||
* 手机号正则
|
||||
*/
|
||||
private static final Pattern PHONE_PATTERN = Pattern.compile("^1[3-9]\\d{9}$");
|
||||
|
||||
/**
|
||||
* 邮箱正则
|
||||
*/
|
||||
private static final Pattern EMAIL_PATTERN = Pattern.compile("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$");
|
||||
|
||||
public VerifyCodeServiceImpl(RedisUtil redisUtil, SmsBaoUtil smsBaoUtil, EmailUtil emailUtil) {
|
||||
this.redisUtil = redisUtil;
|
||||
this.smsBaoUtil = smsBaoUtil;
|
||||
@@ -59,11 +45,7 @@ public class VerifyCodeServiceImpl implements VerifyCodeService {
|
||||
throw new RuntimeException(I18nUtil.getMessage("error.verify.type.empty"));
|
||||
}
|
||||
|
||||
// 判断是手机号还是邮箱
|
||||
boolean isPhone = PHONE_PATTERN.matcher(account).matches();
|
||||
boolean isEmail = EMAIL_PATTERN.matcher(account).matches();
|
||||
|
||||
if (!isPhone && !isEmail) {
|
||||
if (!AccountUtil.isPhone(account) && !AccountUtil.isEmail(account)) {
|
||||
throw new RuntimeException(I18nUtil.getMessage("error.account.format.invalid"));
|
||||
}
|
||||
|
||||
@@ -77,7 +59,7 @@ public class VerifyCodeServiceImpl implements VerifyCodeService {
|
||||
log.info("验证码已生成: account={}, type={}, code={}", account, type, code);
|
||||
|
||||
// 发送验证码
|
||||
if (isPhone) {
|
||||
if (AccountUtil.isPhone(account)) {
|
||||
// 发送短信验证码
|
||||
boolean success = smsBaoUtil.sendVerifyCode(account, code);
|
||||
if (!success) {
|
||||
|
||||
Reference in New Issue
Block a user