From 30d76314d66c392e36411229afeaed045f491d7a Mon Sep 17 00:00:00 2001 From: Charles7c Date: Fri, 2 Feb 2024 20:50:40 +0800 Subject: [PATCH] =?UTF-8?q?refactor(captcha/graphic):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=9B=BE=E5=BD=A2=E9=AA=8C=E8=AF=81=E7=A0=81=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GraphicCaptchaAutoConfiguration.java | 24 ++++++++++++++++++- .../GraphicCaptchaProperties.java | 22 +---------------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/continew-starter-captcha/continew-starter-captcha-graphic/src/main/java/top/charles7c/continew/starter/captcha/graphic/autoconfigure/GraphicCaptchaAutoConfiguration.java b/continew-starter-captcha/continew-starter-captcha-graphic/src/main/java/top/charles7c/continew/starter/captcha/graphic/autoconfigure/GraphicCaptchaAutoConfiguration.java index 794650c1..3eb2ea8a 100644 --- a/continew-starter-captcha/continew-starter-captcha-graphic/src/main/java/top/charles7c/continew/starter/captcha/graphic/autoconfigure/GraphicCaptchaAutoConfiguration.java +++ b/continew-starter-captcha/continew-starter-captcha-graphic/src/main/java/top/charles7c/continew/starter/captcha/graphic/autoconfigure/GraphicCaptchaAutoConfiguration.java @@ -16,14 +16,21 @@ package top.charles7c.continew.starter.captcha.graphic.autoconfigure; +import cn.hutool.core.util.ReflectUtil; +import cn.hutool.core.util.StrUtil; +import com.wf.captcha.base.Captcha; import jakarta.annotation.PostConstruct; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; import top.charles7c.continew.starter.core.constant.PropertiesConstants; +import java.awt.*; + /** * 图形验证码自动配置 * @@ -37,8 +44,23 @@ public class GraphicCaptchaAutoConfiguration { private static final Logger log = LoggerFactory.getLogger(GraphicCaptchaAutoConfiguration.class); + /** + * 验证码配置 + */ + @Bean + @ConditionalOnMissingBean + public Captcha captcha(GraphicCaptchaProperties properties) { + Captcha captcha = ReflectUtil.newInstance(properties.getType().getCaptchaImpl(), properties + .getWidth(), properties.getHeight()); + captcha.setLen(properties.getLength()); + if (StrUtil.isNotBlank(properties.getFontName())) { + captcha.setFont(new Font(properties.getFontName(), Font.PLAIN, properties.getFontSize())); + } + return captcha; + } + @PostConstruct public void postConstruct() { - log.debug("[ContiNew Starter] - Auto Configuration 'Graphic Captcha' completed initialization."); + log.debug("[ContiNew Starter] - Auto Configuration 'Captcha-Graphic' completed initialization."); } } diff --git a/continew-starter-captcha/continew-starter-captcha-graphic/src/main/java/top/charles7c/continew/starter/captcha/graphic/autoconfigure/GraphicCaptchaProperties.java b/continew-starter-captcha/continew-starter-captcha-graphic/src/main/java/top/charles7c/continew/starter/captcha/graphic/autoconfigure/GraphicCaptchaProperties.java index 65c452e6..cd8acd71 100644 --- a/continew-starter-captcha/continew-starter-captcha-graphic/src/main/java/top/charles7c/continew/starter/captcha/graphic/autoconfigure/GraphicCaptchaProperties.java +++ b/continew-starter-captcha/continew-starter-captcha-graphic/src/main/java/top/charles7c/continew/starter/captcha/graphic/autoconfigure/GraphicCaptchaProperties.java @@ -16,9 +16,6 @@ package top.charles7c.continew.starter.captcha.graphic.autoconfigure; -import cn.hutool.core.util.ReflectUtil; -import cn.hutool.core.util.StrUtil; -import com.wf.captcha.base.Captcha; import org.springframework.boot.context.properties.ConfigurationProperties; import top.charles7c.continew.starter.captcha.graphic.enums.GraphicCaptchaType; import top.charles7c.continew.starter.core.constant.PropertiesConstants; @@ -42,7 +39,7 @@ public class GraphicCaptchaProperties { /** * 类型 */ - private GraphicCaptchaType type; + private GraphicCaptchaType type = GraphicCaptchaType.SPEC; /** * 内容长度 @@ -69,23 +66,6 @@ public class GraphicCaptchaProperties { */ private int fontSize = 25; - /** - * 获取图形验证码 - * - * @return 图形验证码 - */ - public Captcha getCaptcha() { - if (this.enabled) { - Captcha captcha = ReflectUtil.newInstance(this.type.getCaptchaImpl(), this.width, this.height); - captcha.setLen(this.length); - if (StrUtil.isNotBlank(this.fontName)) { - captcha.setFont(new Font(fontName, Font.PLAIN, this.fontSize)); - } - return captcha; - } - return null; - } - public boolean isEnabled() { return enabled; }