refactor(captcha/graphic): 优化图形验证码配置

This commit is contained in:
2024-02-02 20:50:40 +08:00
parent 8598e6d109
commit 30d76314d6
2 changed files with 24 additions and 22 deletions

View File

@@ -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.");
}
}

View File

@@ -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;
}