From 42b705371d288e6fd542107c0972da34ad6dce17 Mon Sep 17 00:00:00 2001 From: wayn <1669738430@qq.com> Date: Thu, 2 Nov 2023 00:05:47 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=95=86=E5=9F=8E):=20=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=91=80=E6=AC=A7=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/wayn/common/enums/ReturnCodeEnum.java | 11 +--- .../data/redis/constant/RedisKeyEnum.java | 2 + .../api/controller/LoginController.java | 60 ++++++++++++------- .../framework/security/RegistryObj.java | 4 +- 4 files changed, 44 insertions(+), 33 deletions(-) diff --git a/waynboot-common/src/main/java/com/wayn/common/enums/ReturnCodeEnum.java b/waynboot-common/src/main/java/com/wayn/common/enums/ReturnCodeEnum.java index 9b2ec39..2d889dd 100644 --- a/waynboot-common/src/main/java/com/wayn/common/enums/ReturnCodeEnum.java +++ b/waynboot-common/src/main/java/com/wayn/common/enums/ReturnCodeEnum.java @@ -1,8 +1,11 @@ package com.wayn.common.enums; +import lombok.Getter; + /** * 错误状态码enum */ +@Getter public enum ReturnCodeEnum { // 200,通用操作成功 3xx,资源重定向 4xx,客户端错误 500,通用操作失败 @@ -88,14 +91,6 @@ public enum ReturnCodeEnum { this.msg = msg; } - public int getCode() { - return code; - } - - public String getMsg() { - return msg; - } - public ReturnCodeEnum setMsg(String msg) { this.msg = msg; return this; diff --git a/waynboot-data/waynboot-data-redis/src/main/java/com/wayn/data/redis/constant/RedisKeyEnum.java b/waynboot-data/waynboot-data-redis/src/main/java/com/wayn/data/redis/constant/RedisKeyEnum.java index 8b6049b..5fba188 100644 --- a/waynboot-data/waynboot-data-redis/src/main/java/com/wayn/data/redis/constant/RedisKeyEnum.java +++ b/waynboot-data/waynboot-data-redis/src/main/java/com/wayn/data/redis/constant/RedisKeyEnum.java @@ -9,6 +9,8 @@ import lombok.Getter; @Getter public enum RedisKeyEnum { + CAPTCHA_KEY_CACHE(CacheConstants.CACHE_PREFIX + "captcha_key:", 300), + EMAIL_KEY_CACHE(CacheConstants.CACHE_PREFIX + "email_key:", 300), ES_SYNC_CACHE(CacheConstants.CACHE_PREFIX + "es_sync_cache", 3600), CART_LOCK(CacheConstants.CACHE_PREFIX + "cart_lock:", 3600), EMAIL_CONSUMER_MAP(CacheConstants.CACHE_PREFIX + "email_consumer_map", 60), diff --git a/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/controller/LoginController.java b/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/controller/LoginController.java index 554fa32..1eae257 100644 --- a/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/controller/LoginController.java +++ b/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/controller/LoginController.java @@ -1,5 +1,6 @@ package com.wayn.mobile.api.controller; +import cn.hutool.core.util.RandomUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.wayn.common.constant.SysConstants; @@ -13,6 +14,7 @@ import com.wayn.common.util.IdUtil; import com.wayn.common.util.R; import com.wayn.common.util.mail.MailUtil; import com.wayn.common.util.security.SecurityUtils; +import com.wayn.data.redis.constant.RedisKeyEnum; import com.wayn.data.redis.manager.RedisCache; import com.wayn.mobile.framework.security.LoginObj; import com.wayn.mobile.framework.security.RegistryObj; @@ -20,6 +22,7 @@ import com.wayn.mobile.framework.security.service.LoginService; import com.wf.captcha.SpecCaptcha; import lombok.AllArgsConstructor; import org.apache.commons.lang3.StringUtils; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.web.bind.annotation.*; import java.util.Collections; @@ -31,12 +34,10 @@ import java.util.concurrent.TimeUnit; public class LoginController { private LoginService loginService; - private RedisCache redisCache; - private IMemberService iMemberService; - private IMailConfigService mailConfigService; + private ThreadPoolTaskExecutor commonThreadPoolTaskExecutor; @PostMapping("/login") public R login(@RequestBody LoginObj loginObj) { @@ -57,14 +58,6 @@ public class LoginController { return R.error(ReturnCodeEnum.USER_PHONE_HAS_REGISTER_ERROR); } - // String redisCode = redisCache.getCacheObject(registryObj.getKey()); - // // 判断验证码 - // if (registryObj.getCode() == null || !redisCode.equals(registryObj.getCode().trim().toLowerCase())) { - // return R.error("验证码不正确"); - // } - // - // redisCache.deleteObject(registryObj.getKey()); - String redisEmailCode = redisCache.getCacheObject(registryObj.getEmailKey()); // 判断邮箱验证码 if (registryObj.getEmailCode() == null || !redisEmailCode.equals(registryObj.getEmailCode().trim().toLowerCase())) { @@ -87,28 +80,49 @@ public class LoginController { @ResponseBody @RequestMapping("/captcha") public R captcha() { + // 创建验证码对象,定义验证码图形的长、宽、以及字数 SpecCaptcha specCaptcha = new SpecCaptcha(80, 32, 4); + // 生成验证码 String verCode = specCaptcha.text().toLowerCase(); - String key = IdUtil.getUid(); + // 生成验证码唯一key + String key = RedisKeyEnum.CAPTCHA_KEY_CACHE.getKey(IdUtil.getUid()); // 存入redis并设置过期时间为30分钟 - redisCache.setCacheObject(key, verCode, SysConstants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES); + redisCache.setCacheObject(key, verCode, RedisKeyEnum.CAPTCHA_KEY_CACHE.getExpireSecond()); // 将key和base64返回给前端 return R.success().add("key", key).add("image", specCaptcha.toBase64()); } @PostMapping("/sendEmailCode") public R sendEmailCode(@RequestBody RegistryObj registryObj) { - SpecCaptcha specCaptcha = new SpecCaptcha(80, 32, 4); - String verCode = specCaptcha.text().toLowerCase(); - String key = IdUtil.getUid(); + // 判断图形验证码是否正确 + String captchaKey = registryObj.getCaptchaKey(); + String captchaCode = registryObj.getCaptchaCode(); + if (StringUtils.isBlank(captchaKey)) { + return R.error(ReturnCodeEnum.CUSTOM_ERROR.setMsg("验证码 key为空")); + } + if (StringUtils.isBlank(captchaCode)) { + return R.error(ReturnCodeEnum.CUSTOM_ERROR.setMsg("验证码 code为空")); + } + String redisCode = redisCache.getCacheObject(captchaKey); + // 判断验证码code + if (!redisCode.equals(captchaCode.trim().toLowerCase())) { + return R.error(ReturnCodeEnum.CUSTOM_ERROR.setMsg("验证码输入错误")); + } + // 生成邮箱验证码code + String verCode = RandomUtil.randomString(6); + // 生成邮箱验证码唯一key + String key = RedisKeyEnum.EMAIL_KEY_CACHE.getKey(IdUtil.getUid()); // 存入redis并设置过期时间为20分钟 - redisCache.setCacheObject(key, verCode, SysConstants.CAPTCHA_EXPIRATION * 10, TimeUnit.MINUTES); - EmailConfig emailConfig = mailConfigService.getById(1L); - SendMailVO sendMailVO = new SendMailVO(); - sendMailVO.setSubject("mall商城注册通知"); - sendMailVO.setContent("邮箱验证码:" + verCode); - sendMailVO.setTos(Collections.singletonList(registryObj.getEmail())); - MailUtil.sendMail(emailConfig, sendMailVO, false, false); + redisCache.setCacheObject(key, verCode, RedisKeyEnum.EMAIL_KEY_CACHE.getExpireSecond()); + commonThreadPoolTaskExecutor.execute(() -> { + EmailConfig emailConfig = mailConfigService.getById(1L); + SendMailVO sendMailVO = new SendMailVO(); + sendMailVO.setSubject("mall商城注册通知"); + sendMailVO.setContent("邮箱验证码:" + verCode); + sendMailVO.setTos(Collections.singletonList(registryObj.getEmail())); + MailUtil.sendMail(emailConfig, sendMailVO, false, false); + redisCache.deleteObject(captchaKey); + }); return R.success().add("key", key); } } diff --git a/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/security/RegistryObj.java b/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/security/RegistryObj.java index 0494b75..ad54853 100644 --- a/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/security/RegistryObj.java +++ b/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/security/RegistryObj.java @@ -33,7 +33,7 @@ public class RegistryObj { /** * 验证码 */ - private String code; + private String captchaCode; /** * 邮箱验证码 @@ -43,7 +43,7 @@ public class RegistryObj { /** * 验证码 key */ - private String key; + private String captchaKey; /** * 邮箱验证码 key