From 3184faaa27111845867d2210f0db16381d53d800 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Sun, 4 Feb 2024 23:12:25 +0800 Subject: [PATCH] =?UTF-8?q?refactor(captcha/graphic):=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E5=9B=BE=E5=BD=A2=E9=AA=8C=E8=AF=81=E7=A0=81=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=B9=B6=E8=B0=83=E6=95=B4=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E7=A0=81=E9=BB=98=E8=AE=A4=E5=90=AF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GraphicCaptchaAutoConfiguration.java | 20 ++----- .../GraphicCaptchaProperties.java | 2 +- .../graphic/core/GraphicCaptchaService.java | 54 +++++++++++++++++++ 3 files changed, 60 insertions(+), 16 deletions(-) create mode 100644 continew-starter-captcha/continew-starter-captcha-graphic/src/main/java/top/charles7c/continew/starter/captcha/graphic/core/GraphicCaptchaService.java 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 3eb2ea8a..f6af7615 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,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 jakarta.annotation.PostConstruct; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,10 +24,9 @@ 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.captcha.graphic.core.GraphicCaptchaService; import top.charles7c.continew.starter.core.constant.PropertiesConstants; -import java.awt.*; - /** * 图形验证码自动配置 * @@ -39,24 +35,18 @@ import java.awt.*; */ @AutoConfiguration @EnableConfigurationProperties(GraphicCaptchaProperties.class) -@ConditionalOnProperty(prefix = PropertiesConstants.CAPTCHA_GRAPHIC, name = PropertiesConstants.ENABLED, havingValue = "true") +@ConditionalOnProperty(prefix = PropertiesConstants.CAPTCHA_GRAPHIC, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true) 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; + public GraphicCaptchaService graphicCaptchaService(GraphicCaptchaProperties properties) { + return new GraphicCaptchaService(properties); } @PostConstruct 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 6b586fd9..812771cb 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 @@ -32,7 +32,7 @@ public class GraphicCaptchaProperties { /** * 是否启用图形验证码 */ - private boolean enabled = false; + private boolean enabled = true; /** * 类型 diff --git a/continew-starter-captcha/continew-starter-captcha-graphic/src/main/java/top/charles7c/continew/starter/captcha/graphic/core/GraphicCaptchaService.java b/continew-starter-captcha/continew-starter-captcha-graphic/src/main/java/top/charles7c/continew/starter/captcha/graphic/core/GraphicCaptchaService.java new file mode 100644 index 00000000..55a75703 --- /dev/null +++ b/continew-starter-captcha/continew-starter-captcha-graphic/src/main/java/top/charles7c/continew/starter/captcha/graphic/core/GraphicCaptchaService.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.charles7c.continew.starter.captcha.graphic.core; + +import cn.hutool.core.util.ReflectUtil; +import cn.hutool.core.util.StrUtil; +import com.wf.captcha.base.Captcha; +import top.charles7c.continew.starter.captcha.graphic.autoconfigure.GraphicCaptchaProperties; + +import java.awt.*; + +/** + * 图形验证码服务接口 + * + * @author Charles7c + * @since 1.4.0 + */ +public class GraphicCaptchaService { + + private final GraphicCaptchaProperties properties; + + public GraphicCaptchaService(GraphicCaptchaProperties properties) { + this.properties = properties; + } + + /** + * 获取验证码实例 + * + * @return 验证码实例 + */ + public Captcha getCaptcha() { + 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; + } +}