From 2b6fc497a84cfb51a1a30282c839c9466922d21e Mon Sep 17 00:00:00 2001 From: haoxr <1490493387@qq.com> Date: Mon, 5 Feb 2024 10:16:11 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=AA=8C=E8=AF=81=E7=A0=81?= =?UTF-8?q?=E6=8A=BD=E5=8F=96=E6=88=90=E5=85=AC=E5=85=B1=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=EF=BC=8C=E7=BD=91=E5=85=B3=EF=BC=88=E9=AA=8C=E8=AF=81=E7=A0=81?= =?UTF-8?q?=E7=94=9F=E6=88=90=EF=BC=89=E5=92=8C=E6=8E=88=E6=9D=83=E4=B8=AD?= =?UTF-8?q?=E5=BF=83=EF=BC=88=E8=AE=A4=E8=AF=81=E9=89=B4=E6=9D=83=EF=BC=89?= =?UTF-8?q?=E9=83=BD=E7=94=A8=EF=BC=8C=E5=90=8E=E7=BB=AD=E8=80=83=E8=99=91?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E7=A0=81=E7=94=9F=E6=88=90=E7=A7=BB=E8=87=B3?= =?UTF-8?q?=E8=AE=A4=E8=AF=81=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- youlai-common/common-captcha/pom.xml | 43 ++++++++++++++ .../captcha/component/CaptchaGenerator.java | 57 +++++++++++++++++++ .../common/captcha}/config/CaptchaConfig.java | 11 ++-- .../captcha}/config/CaptchaProperties.java | 7 ++- .../captcha}/enums/CaptchaTypeEnum.java | 2 +- ...ot.autoconfigure.AutoConfiguration.imports | 4 ++ 6 files changed, 116 insertions(+), 8 deletions(-) create mode 100644 youlai-common/common-captcha/pom.xml create mode 100644 youlai-common/common-captcha/src/main/java/com/youlai/common/captcha/component/CaptchaGenerator.java rename {youlai-gateway/src/main/java/com/youlai/gateway => youlai-common/common-captcha/src/main/java/com/youlai/common/captcha}/config/CaptchaConfig.java (79%) rename {youlai-gateway/src/main/java/com/youlai/gateway => youlai-common/common-captcha/src/main/java/com/youlai/common/captcha}/config/CaptchaProperties.java (92%) rename {youlai-gateway/src/main/java/com/youlai/gateway => youlai-common/common-captcha/src/main/java/com/youlai/common/captcha}/enums/CaptchaTypeEnum.java (88%) create mode 100644 youlai-common/common-captcha/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports diff --git a/youlai-common/common-captcha/pom.xml b/youlai-common/common-captcha/pom.xml new file mode 100644 index 000000000..d37be8549 --- /dev/null +++ b/youlai-common/common-captcha/pom.xml @@ -0,0 +1,43 @@ + + + 4.0.0 + + com.youlai + youlai-common + 3.0.1 + + + common-captcha + + + 17 + 17 + UTF-8 + + + + + org.projectlombok + lombok + + + + cn.hutool + hutool-all + + + + org.springframework.boot + spring-boot-starter + compile + + + + com.youlai + common-core + + + + \ No newline at end of file diff --git a/youlai-common/common-captcha/src/main/java/com/youlai/common/captcha/component/CaptchaGenerator.java b/youlai-common/common-captcha/src/main/java/com/youlai/common/captcha/component/CaptchaGenerator.java new file mode 100644 index 000000000..4d5efd9a5 --- /dev/null +++ b/youlai-common/common-captcha/src/main/java/com/youlai/common/captcha/component/CaptchaGenerator.java @@ -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; + } + +} diff --git a/youlai-gateway/src/main/java/com/youlai/gateway/config/CaptchaConfig.java b/youlai-common/common-captcha/src/main/java/com/youlai/common/captcha/config/CaptchaConfig.java similarity index 79% rename from youlai-gateway/src/main/java/com/youlai/gateway/config/CaptchaConfig.java rename to youlai-common/common-captcha/src/main/java/com/youlai/common/captcha/config/CaptchaConfig.java index c08b9085b..7281e9e20 100644 --- a/youlai-gateway/src/main/java/com/youlai/gateway/config/CaptchaConfig.java +++ b/youlai-common/common-captcha/src/main/java/com/youlai/common/captcha/config/CaptchaConfig.java @@ -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.MathGenerator; import cn.hutool.captcha.generator.RandomGenerator; +import com.youlai.common.enums.CaptchaCodeTypeEnum; import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -13,7 +14,7 @@ import java.awt.*; * 验证码自动装配配置 * * @author haoxr - * @since 2023/11/24 + * @since 3.1.0 */ @Configuration @RequiredArgsConstructor @@ -30,9 +31,11 @@ public class CaptchaConfig { public CodeGenerator codeGenerator() { String codeType = captchaProperties.getCode().getType(); int codeLength = captchaProperties.getCode().getLength(); - if ("math".equalsIgnoreCase(codeType)) { + if (CaptchaCodeTypeEnum.MATH.name().equalsIgnoreCase(codeType)) { + // 数学公式验证码 return new MathGenerator(codeLength); - } else if ("random".equalsIgnoreCase(codeType)) { + } else if (CaptchaCodeTypeEnum.RANDOM.name().equalsIgnoreCase(codeType)) { + // 随机字符验证码 return new RandomGenerator(codeLength); } else { throw new IllegalArgumentException("Invalid captcha generator type: " + codeType); diff --git a/youlai-gateway/src/main/java/com/youlai/gateway/config/CaptchaProperties.java b/youlai-common/common-captcha/src/main/java/com/youlai/common/captcha/config/CaptchaProperties.java similarity index 92% rename from youlai-gateway/src/main/java/com/youlai/gateway/config/CaptchaProperties.java rename to youlai-common/common-captcha/src/main/java/com/youlai/common/captcha/config/CaptchaProperties.java index 9968ceae4..9a91daeef 100644 --- a/youlai-gateway/src/main/java/com/youlai/gateway/config/CaptchaProperties.java +++ b/youlai-common/common-captcha/src/main/java/com/youlai/common/captcha/config/CaptchaProperties.java @@ -1,16 +1,17 @@ -package com.youlai.gateway.config; +package com.youlai.common.captcha.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; /** * 验证码配置 * * @author haoxr - * @since 2023/11/24 + * @since 3.1.0 */ -@Component +@Configuration @ConfigurationProperties(prefix = "captcha") @Data public class CaptchaProperties { diff --git a/youlai-gateway/src/main/java/com/youlai/gateway/enums/CaptchaTypeEnum.java b/youlai-common/common-captcha/src/main/java/com/youlai/common/captcha/enums/CaptchaTypeEnum.java similarity index 88% rename from youlai-gateway/src/main/java/com/youlai/gateway/enums/CaptchaTypeEnum.java rename to youlai-common/common-captcha/src/main/java/com/youlai/common/captcha/enums/CaptchaTypeEnum.java index 84a0c9800..d0124d9d4 100644 --- a/youlai-gateway/src/main/java/com/youlai/gateway/enums/CaptchaTypeEnum.java +++ b/youlai-common/common-captcha/src/main/java/com/youlai/common/captcha/enums/CaptchaTypeEnum.java @@ -1,4 +1,4 @@ -package com.youlai.gateway.enums; +package com.youlai.common.captcha.enums; /** * EasyCaptcha 验证码类型枚举 diff --git a/youlai-common/common-captcha/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/youlai-common/common-captcha/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 000000000..ba97b4d7f --- /dev/null +++ b/youlai-common/common-captcha/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1,4 @@ +com.youlai.common.captcha.config.CaptchaConfig +com.youlai.common.captcha.config.CaptchaProperties +com.youlai.common.captcha.component.CaptchaGenerator +