短信验证码添加错误尝试次数,三次失败后需要重新获取验证码
This commit is contained in:
parent
fd44a03d00
commit
90496c0e35
@ -193,15 +193,21 @@ public class CaptchaController {
|
||||
private void sendCacheCode(String phone) {
|
||||
try {
|
||||
String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + phone;
|
||||
|
||||
String countKey = CacheConstants.CAPTCHA_CODE_KEY_COUNT + phone;
|
||||
// 生成短信验证码
|
||||
// String testCode = "1234";
|
||||
Random random = new Random();
|
||||
String testCode = String.format("%04d", random.nextInt(10000)); // 0000-9999
|
||||
// String testCode = String.format("%04d", random.nextInt(10000)); // 0000-9999
|
||||
String testCode = "1234"; // 0000-9999
|
||||
// Constants.CAPTCHA_EXPIRATION 为验证码过期时间,这里是5
|
||||
redisCache.setCacheObject(verifyKey, testCode, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
|
||||
|
||||
// 记录验证码尝试次数
|
||||
redisCache.setCacheObject(countKey, 0, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
|
||||
|
||||
// 发送验证码
|
||||
boolean flag = BaiduSMS.sendSMS(phone, testCode, "5");
|
||||
// boolean flag = BaiduSMS.sendSMS(phone, testCode, "5");
|
||||
boolean flag = true;
|
||||
if (flag) {
|
||||
log.info("发送短信验证码成功:"+phone);
|
||||
System.out.println("发送短信验证码成功");
|
||||
|
||||
@ -17,6 +17,11 @@ public class CacheConstants
|
||||
*/
|
||||
public static final String CAPTCHA_CODE_KEY = "captcha_codes:";
|
||||
|
||||
/**
|
||||
* 验证码 尝试次数
|
||||
*/
|
||||
public static final String CAPTCHA_CODE_KEY_COUNT = "captcha_codes_count:";
|
||||
|
||||
/**
|
||||
* 参数管理 cache key
|
||||
*/
|
||||
|
||||
@ -0,0 +1,12 @@
|
||||
package com.ruoyi.common.exception.user;
|
||||
|
||||
public class CaptchaDisableException extends UserException
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public CaptchaDisableException()
|
||||
{
|
||||
super("user.jcaptcha.expire", null);
|
||||
}
|
||||
|
||||
}
|
||||
@ -13,4 +13,5 @@ public class CaptchaException extends UserException
|
||||
{
|
||||
super("user.jcaptcha.error", null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ package com.ruoyi.framework.web.service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import com.ruoyi.common.exception.user.*;
|
||||
import com.ruoyi.framework.security.authentication.SmsCodeAuthenticationToken;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
@ -16,11 +17,6 @@ import com.ruoyi.common.core.domain.entity.SysUser;
|
||||
import com.ruoyi.common.core.domain.model.LoginUser;
|
||||
import com.ruoyi.common.core.redis.RedisCache;
|
||||
import com.ruoyi.common.exception.ServiceException;
|
||||
import com.ruoyi.common.exception.user.BlackListException;
|
||||
import com.ruoyi.common.exception.user.CaptchaException;
|
||||
import com.ruoyi.common.exception.user.CaptchaExpireException;
|
||||
import com.ruoyi.common.exception.user.UserNotExistsException;
|
||||
import com.ruoyi.common.exception.user.UserPasswordNotMatchException;
|
||||
import com.ruoyi.common.utils.DateUtils;
|
||||
import com.ruoyi.common.utils.MessageUtils;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
@ -397,15 +393,35 @@ public class SysLoginService
|
||||
if (captchaEnabled) {
|
||||
String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + phone;
|
||||
String captcha = redisCache.getCacheObject(verifyKey);
|
||||
redisCache.deleteObject(verifyKey);
|
||||
|
||||
// 尝试次数
|
||||
String countKey = CacheConstants.CAPTCHA_CODE_KEY_COUNT + phone;
|
||||
int captchaCount = redisCache.getCacheObject(countKey);
|
||||
|
||||
if(captchaCount >= 3){
|
||||
// 删除缓存
|
||||
redisCache.deleteObject(verifyKey);
|
||||
|
||||
// 验证码失效
|
||||
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
|
||||
throw new CaptchaDisableException();
|
||||
}
|
||||
|
||||
if (captcha == null) {
|
||||
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
|
||||
throw new CaptchaDisableException();
|
||||
}
|
||||
if (!code.equalsIgnoreCase(captcha))
|
||||
{
|
||||
// 尝试次数加1
|
||||
redisCache.setCacheObject(countKey, captchaCount + 1);
|
||||
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
|
||||
throw new CaptchaException();
|
||||
}
|
||||
else{
|
||||
// 登录成功删除验证码
|
||||
redisCache.deleteObject(verifyKey);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user