refactor: 代码优化

This commit is contained in:
haoxr
2024-01-17 22:13:15 +08:00
parent db6253ca88
commit ce291f1a93
3 changed files with 23 additions and 20 deletions

View File

@@ -21,7 +21,7 @@ public interface CacheConstants {
/** /**
* 黑名单Token缓存前缀 * 黑名单Token缓存前缀
*/ */
String BLACKLIST_TOKEN_PREFIX = "blacklist_token:"; String BLACKLIST_TOKEN_PREFIX = "token:blacklist:";
} }

View File

@@ -4,25 +4,24 @@ package com.youlai.system.common.enums;
* EasyCaptcha 验证码类型枚举 * EasyCaptcha 验证码类型枚举
* *
* @author haoxr * @author haoxr
* @since 2023/03/24 * @since 2.5.1
*/ */
public enum CaptchaTypeEnum { public enum CaptchaTypeEnum {
/** /**
* 算数 * 圆圈干扰验证码
*/ */
ARITHMETIC, CIRCLE,
/** /**
* 中文 * GIF验证码
*/
CHINESE,
/**
* 中文闪图
*/
CHINESE_GIF,
/**
* 闪图
*/ */
GIF, GIF,
SPEC /**
* 干扰线验证码
*/
LINE,
/**
* 扭曲干扰验证码
*/
SHEAR
} }

View File

@@ -6,6 +6,7 @@ import cn.hutool.captcha.generator.CodeGenerator;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.youlai.system.common.constant.CacheConstants; import com.youlai.system.common.constant.CacheConstants;
import com.youlai.system.common.enums.CaptchaTypeEnum;
import com.youlai.system.core.security.jwt.JwtTokenProvider; import com.youlai.system.core.security.jwt.JwtTokenProvider;
import com.youlai.system.model.dto.CaptchaResult; import com.youlai.system.model.dto.CaptchaResult;
import com.youlai.system.model.dto.LoginResult; import com.youlai.system.model.dto.LoginResult;
@@ -14,6 +15,7 @@ import com.youlai.system.service.AuthService;
import io.jsonwebtoken.Claims; import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@@ -35,6 +37,7 @@ import java.util.concurrent.TimeUnit;
*/ */
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
@Slf4j
public class AuthServiceImpl implements AuthService { public class AuthServiceImpl implements AuthService {
private final AuthenticationManager authenticationManager; private final AuthenticationManager authenticationManager;
@@ -73,6 +76,7 @@ public class AuthServiceImpl implements AuthService {
if (StrUtil.isNotBlank(token)) { if (StrUtil.isNotBlank(token)) {
Claims claims = jwtTokenProvider.getTokenClaims(token); Claims claims = jwtTokenProvider.getTokenClaims(token);
String jti = claims.get("jti", String.class); String jti = claims.get("jti", String.class);
Date expiration = claims.getExpiration(); Date expiration = claims.getExpiration();
if (expiration != null) { if (expiration != null) {
long ttl = expiration.getTime() - System.currentTimeMillis(); long ttl = expiration.getTime() - System.currentTimeMillis();
@@ -92,23 +96,23 @@ public class AuthServiceImpl implements AuthService {
@Override @Override
public CaptchaResult getCaptcha() { public CaptchaResult getCaptcha() {
String type = captchaProperties.getType(); String captchaType = captchaProperties.getType();
int width = captchaProperties.getWidth(); int width = captchaProperties.getWidth();
int height = captchaProperties.getHeight(); int height = captchaProperties.getHeight();
int interfereCount = captchaProperties.getInterfereCount(); int interfereCount = captchaProperties.getInterfereCount();
int codeLength = captchaProperties.getCode().getLength(); int codeLength = captchaProperties.getCode().getLength();
AbstractCaptcha captcha; AbstractCaptcha captcha;
if ("circle".equalsIgnoreCase(type)) { if (CaptchaTypeEnum.CIRCLE.name().equalsIgnoreCase(captchaType)) {
captcha = CaptchaUtil.createCircleCaptcha(width, height, codeLength, interfereCount); captcha = CaptchaUtil.createCircleCaptcha(width, height, codeLength, interfereCount);
} else if ("gif".equalsIgnoreCase(type)) { } else if (CaptchaTypeEnum.GIF.name().equalsIgnoreCase(captchaType)) {
captcha = CaptchaUtil.createGifCaptcha(width, height, codeLength); captcha = CaptchaUtil.createGifCaptcha(width, height, codeLength);
} else if ("line".equalsIgnoreCase(type)) { } else if (CaptchaTypeEnum.LINE.name().equalsIgnoreCase(captchaType)) {
captcha = CaptchaUtil.createLineCaptcha(width, height, codeLength, interfereCount); captcha = CaptchaUtil.createLineCaptcha(width, height, codeLength, interfereCount);
} else if ("shear".equalsIgnoreCase(type)) { } else if (CaptchaTypeEnum.SHEAR.name().equalsIgnoreCase(captchaType)) {
captcha = CaptchaUtil.createShearCaptcha(width, height, codeLength, interfereCount); captcha = CaptchaUtil.createShearCaptcha(width, height, codeLength, interfereCount);
} else { } else {
throw new IllegalArgumentException("Invalid captcha type: " + type); throw new IllegalArgumentException("Invalid captcha type: " + captchaType);
} }
captcha.setGenerator(codeGenerator); captcha.setGenerator(codeGenerator);
captcha.setTextAlpha(captchaProperties.getTextAlpha()); captcha.setTextAlpha(captchaProperties.getTextAlpha());