refactor: 验证码字段调整
This commit is contained in:
@@ -26,15 +26,15 @@ public interface SecurityConstants {
|
||||
/**
|
||||
* 验证码缓存前缀
|
||||
*/
|
||||
String VERIFY_CODE_CACHE_PREFIX = "AUTH:VERIFY_CODE:";
|
||||
String CAPTCHA_CODE_CACHE_PREFIX = "captcha_code:";
|
||||
|
||||
/**
|
||||
* 用户权限集合缓存前缀
|
||||
*/
|
||||
String USER_PERMS_CACHE_PREFIX = "AUTH:USER_PERMS:";
|
||||
String USER_PERMS_CACHE_PREFIX = "user_perms:";
|
||||
|
||||
/**
|
||||
* 黑名单Token缓存前缀
|
||||
*/
|
||||
String BLACK_TOKEN_CACHE_PREFIX = "AUTH:BLACK_TOKEN:";
|
||||
String BLACK_TOKEN_CACHE_PREFIX = "blacklist_token:";
|
||||
}
|
||||
|
||||
@@ -27,15 +27,15 @@ public class VerifyCodeFilter extends OncePerRequestFilter {
|
||||
|
||||
private static final AntPathRequestMatcher LOGIN_PATH_REQUEST_MATCHER = new AntPathRequestMatcher(SecurityConstants.LOGIN_PATH, "POST");
|
||||
|
||||
public static final String VERIFY_CODE_PARAM_KEY = "verifyCode";
|
||||
public static final String VERIFY_CODE_KEY_PARAM_KEY = "verifyCodeKey";
|
||||
public static final String CAPTCHA_CODE_PARAM_NAME = "captchaCode";
|
||||
public static final String CAPTCHA_KEY_PARAM_NAME = "captchaKey";
|
||||
|
||||
@Override
|
||||
public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
|
||||
// 检验登录接口的验证码
|
||||
if (LOGIN_PATH_REQUEST_MATCHER.matches(request)) {
|
||||
// 请求中的验证码
|
||||
String verifyCode = request.getParameter(VERIFY_CODE_PARAM_KEY);
|
||||
String verifyCode = request.getParameter(CAPTCHA_CODE_PARAM_NAME);
|
||||
// TODO 兼容没有验证码的版本(线上请移除这个判断)
|
||||
if (StrUtil.isBlank(verifyCode)) {
|
||||
chain.doFilter(request, response);
|
||||
@@ -43,8 +43,8 @@ public class VerifyCodeFilter extends OncePerRequestFilter {
|
||||
}
|
||||
// 缓存中的验证码
|
||||
StringRedisTemplate redisTemplate = SpringUtil.getBean("stringRedisTemplate", StringRedisTemplate.class);
|
||||
String verifyCodeKey = request.getParameter(VERIFY_CODE_KEY_PARAM_KEY);
|
||||
String cacheVerifyCode = redisTemplate.opsForValue().get(SecurityConstants.VERIFY_CODE_CACHE_PREFIX + verifyCodeKey);
|
||||
String verifyCodeKey = request.getParameter(CAPTCHA_KEY_PARAM_NAME);
|
||||
String cacheVerifyCode = redisTemplate.opsForValue().get(SecurityConstants.CAPTCHA_CODE_CACHE_PREFIX + verifyCodeKey);
|
||||
if (cacheVerifyCode == null) {
|
||||
ResponseUtils.writeErrMsg(response, ResultCode.VERIFY_CODE_TIMEOUT);
|
||||
} else {
|
||||
|
||||
@@ -16,9 +16,9 @@ import lombok.Data;
|
||||
public class CaptchaResult {
|
||||
|
||||
@Schema(description = "验证码缓存key")
|
||||
private String verifyCodeKey;
|
||||
private String captchaKey;
|
||||
|
||||
@Schema(description = "验证码图片Base64字符串")
|
||||
private String captchaImgBase64;
|
||||
private String captchaBase64;
|
||||
|
||||
}
|
||||
|
||||
@@ -5,9 +5,9 @@ import cn.hutool.captcha.generator.MathGenerator;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.youlai.system.common.constant.SecurityConstants;
|
||||
import com.youlai.system.core.security.jwt.JwtTokenProvider;
|
||||
import com.youlai.system.model.dto.CaptchaResult;
|
||||
import com.youlai.system.model.dto.LoginResult;
|
||||
import com.youlai.system.core.security.jwt.JwtTokenProvider;
|
||||
import com.youlai.system.service.AuthService;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
@@ -21,9 +21,12 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static java.awt.Font.SANS_SERIF;
|
||||
|
||||
/**
|
||||
* 认证服务实现类
|
||||
*
|
||||
@@ -85,21 +88,22 @@ public class AuthServiceImpl implements AuthService {
|
||||
*/
|
||||
@Override
|
||||
public CaptchaResult getCaptcha() {
|
||||
|
||||
|
||||
MathGenerator mathGenerator=new MathGenerator(1);
|
||||
CircleCaptcha circleCaptcha =new CircleCaptcha(120,25,4,3);
|
||||
circleCaptcha.setGenerator(mathGenerator);
|
||||
circleCaptcha.setFont(new Font(SANS_SERIF, Font.BOLD, 18));
|
||||
String captchaCode = circleCaptcha.getCode(); // 验证码
|
||||
String captchaBase64 = circleCaptcha.getImageBase64Data(); // 验证码图片Base64
|
||||
|
||||
// 验证码文本缓存至Redis,用于登录校验
|
||||
String verifyCodeKey = IdUtil.fastSimpleUUID();
|
||||
redisTemplate.opsForValue().set(SecurityConstants.VERIFY_CODE_CACHE_PREFIX + verifyCodeKey, captchaCode,
|
||||
String captchaKey = IdUtil.fastSimpleUUID();
|
||||
redisTemplate.opsForValue().set(SecurityConstants.CAPTCHA_CODE_CACHE_PREFIX + captchaKey, captchaCode,
|
||||
120, TimeUnit.SECONDS);
|
||||
|
||||
return CaptchaResult.builder()
|
||||
.verifyCodeKey(verifyCodeKey)
|
||||
.captchaImgBase64(captchaBase64)
|
||||
.captchaKey(captchaKey)
|
||||
.captchaBase64(captchaBase64)
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user