refactor: 简化 GIF 验证码为算术验证码(PS:太闪了)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
package com.youlai.system.filter;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.captcha.generator.MathGenerator;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import com.youlai.system.common.constant.SecurityConstants;
|
||||
@@ -10,7 +10,7 @@ import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
@@ -35,23 +35,24 @@ public class VerifyCodeFilter extends OncePerRequestFilter {
|
||||
// 检验登录接口的验证码
|
||||
if (LOGIN_PATH_REQUEST_MATCHER.matches(request)) {
|
||||
// 请求中的验证码
|
||||
String requestVerifyCode = request.getParameter(VERIFY_CODE_PARAM_KEY);
|
||||
String userInputCode = request.getParameter(VERIFY_CODE_PARAM_KEY);
|
||||
|
||||
// TODO 兼容 2.0.0 无验证码版本,后续移除
|
||||
if (StrUtil.isBlank(requestVerifyCode)) {
|
||||
if (StrUtil.isBlank(userInputCode)) {
|
||||
// 非登录接口放行
|
||||
chain.doFilter(request, response);
|
||||
return;
|
||||
}
|
||||
// 缓存中的验证码
|
||||
RedisTemplate redisTemplate = SpringUtil.getBean("redisTemplate", RedisTemplate.class);
|
||||
StringRedisTemplate redisTemplate = SpringUtil.getBean("stringRedisTemplate", StringRedisTemplate.class);
|
||||
String verifyCodeKey = request.getParameter(VERIFY_CODE_KEY_PARAM_KEY);
|
||||
Object cacheVerifyCode = redisTemplate.opsForValue().get(SecurityConstants.VERIFY_CODE_CACHE_PREFIX + verifyCodeKey);
|
||||
String cacheVerifyCode = redisTemplate.opsForValue().get(SecurityConstants.VERIFY_CODE_CACHE_PREFIX + verifyCodeKey);
|
||||
if (cacheVerifyCode == null) {
|
||||
ResponseUtils.writeErrMsg(response, ResultCode.VERIFY_CODE_TIMEOUT);
|
||||
} else {
|
||||
// 验证码比对
|
||||
if (StrUtil.equals(requestVerifyCode, Convert.toStr(cacheVerifyCode))) {
|
||||
MathGenerator mathGenerator = new MathGenerator();
|
||||
if (mathGenerator.verify(cacheVerifyCode, userInputCode)) {
|
||||
chain.doFilter(request, response);
|
||||
} else {
|
||||
ResponseUtils.writeErrMsg(response, ResultCode.VERIFY_CODE_ERROR);
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
package com.youlai.system.service.impl;
|
||||
|
||||
import cn.hutool.captcha.CaptchaUtil;
|
||||
import cn.hutool.captcha.GifCaptcha;
|
||||
import cn.hutool.captcha.LineCaptcha;
|
||||
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.security.jwt.JwtTokenProvider;
|
||||
import com.youlai.system.service.AuthService;
|
||||
import com.youlai.system.model.dto.CaptchaResult;
|
||||
import com.youlai.system.model.dto.LoginResult;
|
||||
import com.youlai.system.security.jwt.JwtTokenProvider;
|
||||
import com.youlai.system.service.AuthService;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
@@ -33,7 +33,7 @@ import java.util.concurrent.TimeUnit;
|
||||
public class AuthServiceImpl implements AuthService {
|
||||
|
||||
private final AuthenticationManager authenticationManager;
|
||||
private final RedisTemplate redisTemplate;
|
||||
private final StringRedisTemplate redisTemplate;
|
||||
private final JwtTokenProvider jwtTokenProvider;
|
||||
|
||||
/**
|
||||
@@ -83,10 +83,12 @@ public class AuthServiceImpl implements AuthService {
|
||||
*/
|
||||
@Override
|
||||
public CaptchaResult getCaptcha() {
|
||||
// 获取验证码
|
||||
GifCaptcha captcha = CaptchaUtil.createGifCaptcha(120, 40, 4); // 宽、高、位数
|
||||
String captchaCode = captcha.getCode(); // 验证码
|
||||
String captchaBase64 = captcha.getImageBase64Data(); // 验证码图片Base64
|
||||
|
||||
MathGenerator mathGenerator=new MathGenerator(1);
|
||||
LineCaptcha lineCaptcha =new LineCaptcha(480,120,4,20);
|
||||
lineCaptcha.setGenerator(mathGenerator);
|
||||
String captchaCode = lineCaptcha.getCode(); // 验证码
|
||||
String captchaBase64 = lineCaptcha.getImageBase64Data(); // 验证码图片Base64
|
||||
|
||||
// 验证码文本缓存至Redis,用于登录校验
|
||||
String verifyCodeKey = IdUtil.fastSimpleUUID();
|
||||
|
||||
Reference in New Issue
Block a user