refactor: 验证码抽取成公共模块,网关(验证码生成)和授权中心(认证鉴权)都用,后续考虑验证码生成移至认证模块

This commit is contained in:
haoxr 2024-02-05 10:16:11 +08:00
parent 8aa45c1d8a
commit 2b6fc497a8
6 changed files with 116 additions and 8 deletions

View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.youlai</groupId>
<artifactId>youlai-common</artifactId>
<version>3.0.1</version>
</parent>
<artifactId>common-captcha</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.youlai</groupId>
<artifactId>common-core</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,57 @@
package com.youlai.common.captcha.component;
import cn.hutool.captcha.AbstractCaptcha;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.generator.CodeGenerator;
import com.youlai.common.captcha.config.CaptchaProperties;
import com.youlai.common.captcha.enums.CaptchaTypeEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import java.awt.*;
/**
* 图形验证码生成器
*
* @author haoxr
* @since 3.1.0
*/
@Component
@RequiredArgsConstructor
public class CaptchaGenerator {
private final CodeGenerator codeGenerator;
private final Font captchaFont;
private final CaptchaProperties captchaProperties;
/**
* 生成图形验证码
*/
public AbstractCaptcha generate() {
String captchaType = captchaProperties.getType();
int width = captchaProperties.getWidth();
int height = captchaProperties.getHeight();
int interfereCount = captchaProperties.getInterfereCount();
int codeLength = captchaProperties.getCode().getLength();
AbstractCaptcha captcha;
if (CaptchaTypeEnum.CIRCLE.name().equalsIgnoreCase(captchaType)) {
captcha = CaptchaUtil.createCircleCaptcha(width, height, codeLength, interfereCount);
} else if (CaptchaTypeEnum.GIF.name().equalsIgnoreCase(captchaType)) {
captcha = CaptchaUtil.createGifCaptcha(width, height, codeLength);
} else if (CaptchaTypeEnum.LINE.name().equalsIgnoreCase(captchaType)) {
captcha = CaptchaUtil.createLineCaptcha(width, height, codeLength, interfereCount);
} else if (CaptchaTypeEnum.SHEAR.name().equalsIgnoreCase(captchaType)) {
captcha = CaptchaUtil.createShearCaptcha(width, height, codeLength, interfereCount);
} else {
throw new IllegalArgumentException("Invalid captcha type: " + captchaType);
}
captcha.setGenerator(codeGenerator);
captcha.setTextAlpha(captchaProperties.getTextAlpha());
captcha.setFont(captchaFont);
return captcha;
}
}

View File

@ -1,8 +1,9 @@
package com.youlai.gateway.config; package com.youlai.common.captcha.config;
import cn.hutool.captcha.generator.CodeGenerator; import cn.hutool.captcha.generator.CodeGenerator;
import cn.hutool.captcha.generator.MathGenerator; import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.captcha.generator.RandomGenerator; import cn.hutool.captcha.generator.RandomGenerator;
import com.youlai.common.enums.CaptchaCodeTypeEnum;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -13,7 +14,7 @@ import java.awt.*;
* 验证码自动装配配置 * 验证码自动装配配置
* *
* @author haoxr * @author haoxr
* @since 2023/11/24 * @since 3.1.0
*/ */
@Configuration @Configuration
@RequiredArgsConstructor @RequiredArgsConstructor
@ -30,9 +31,11 @@ public class CaptchaConfig {
public CodeGenerator codeGenerator() { public CodeGenerator codeGenerator() {
String codeType = captchaProperties.getCode().getType(); String codeType = captchaProperties.getCode().getType();
int codeLength = captchaProperties.getCode().getLength(); int codeLength = captchaProperties.getCode().getLength();
if ("math".equalsIgnoreCase(codeType)) { if (CaptchaCodeTypeEnum.MATH.name().equalsIgnoreCase(codeType)) {
// 数学公式验证码
return new MathGenerator(codeLength); return new MathGenerator(codeLength);
} else if ("random".equalsIgnoreCase(codeType)) { } else if (CaptchaCodeTypeEnum.RANDOM.name().equalsIgnoreCase(codeType)) {
// 随机字符验证码
return new RandomGenerator(codeLength); return new RandomGenerator(codeLength);
} else { } else {
throw new IllegalArgumentException("Invalid captcha generator type: " + codeType); throw new IllegalArgumentException("Invalid captcha generator type: " + codeType);

View File

@ -1,16 +1,17 @@
package com.youlai.gateway.config; package com.youlai.common.captcha.config;
import lombok.Data; import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
* 验证码配置 * 验证码配置
* *
* @author haoxr * @author haoxr
* @since 2023/11/24 * @since 3.1.0
*/ */
@Component @Configuration
@ConfigurationProperties(prefix = "captcha") @ConfigurationProperties(prefix = "captcha")
@Data @Data
public class CaptchaProperties { public class CaptchaProperties {

View File

@ -1,4 +1,4 @@
package com.youlai.gateway.enums; package com.youlai.common.captcha.enums;
/** /**
* EasyCaptcha 验证码类型枚举 * EasyCaptcha 验证码类型枚举

View File

@ -0,0 +1,4 @@
com.youlai.common.captcha.config.CaptchaConfig
com.youlai.common.captcha.config.CaptchaProperties
com.youlai.common.captcha.component.CaptchaGenerator