完善短信验证功能

This commit is contained in:
haotian 2025-05-12 15:32:09 +08:00
parent 90496c0e35
commit 7162420008
2 changed files with 48 additions and 28 deletions

View File

@ -6,6 +6,8 @@ import com.ruoyi.common.constant.ScheduleConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.user.CaptchaDisableException;
import com.ruoyi.common.exception.user.CaptchaException;
import com.ruoyi.common.utils.*;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.ip.IpUtils;
@ -172,33 +174,13 @@ public class AppSystemController extends BaseController {
@PostMapping("/verifyUpdatePasswordSms")
public AjaxResult verifyUpdatePasswordSms(@RequestBody PhoneSMSDto phoneSMSDto) {
String username = phoneSMSDto.getUsername();
// String phone = phoneSMSDto.getUsername();
String username = SecurityUtils.getUsername();
String code = phoneSMSDto.getCode();
appvalidateCaptcha(username, code, username);
return success();
boolean captchaEnabled = configService.selectCaptchaEnabled();
if (captchaEnabled) {
String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + username;
String captcha = redisCache.getCacheObject(verifyKey);
// System.out.println(captcha);
// 删除缓存中的验证码
if (captcha == null) {
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
return error("验证码过期");
}
if (!code.equalsIgnoreCase(captcha))
{
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
// throw new CaptchaException();
return error("验证码错误");
}
// redisCache.deleteObject(verifyKey);
return success();
}
return error("服务未开放");
// return error("服务未开放");
}
@ -2421,6 +2403,44 @@ public class AppSystemController extends BaseController {
return error("获取订单详情失败");
}
private void appvalidateCaptcha(String username, String code, String phone) {
boolean captchaEnabled = configService.selectCaptchaEnabled();
if (captchaEnabled) {
String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + phone;
String captcha = redisCache.getCacheObject(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);
}
}
}
}

View File

@ -197,8 +197,8 @@ public class CaptchaController {
String countKey = CacheConstants.CAPTCHA_CODE_KEY_COUNT + phone;
// 生成短信验证码
Random random = new Random();
// String testCode = String.format("%04d", random.nextInt(10000)); // 0000-9999
String testCode = "1234"; // 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);
@ -206,8 +206,8 @@ public class CaptchaController {
redisCache.setCacheObject(countKey, 0, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
// 发送验证码
// boolean flag = BaiduSMS.sendSMS(phone, testCode, "5");
boolean flag = true;
boolean flag = BaiduSMS.sendSMS(phone, testCode, "5");
// boolean flag = true;
if (flag) {
log.info("发送短信验证码成功:"+phone);
System.out.println("发送短信验证码成功");