mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-25 06:57:13 +08:00
refactor(captcha/graphic): 优化图形验证码配置
This commit is contained in:
@@ -16,14 +16,21 @@
|
|||||||
|
|
||||||
package top.charles7c.continew.starter.captcha.graphic.autoconfigure;
|
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 jakarta.annotation.PostConstruct;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
import top.charles7c.continew.starter.core.constant.PropertiesConstants;
|
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);
|
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
|
@PostConstruct
|
||||||
public void postConstruct() {
|
public void postConstruct() {
|
||||||
log.debug("[ContiNew Starter] - Auto Configuration 'Graphic Captcha' completed initialization.");
|
log.debug("[ContiNew Starter] - Auto Configuration 'Captcha-Graphic' completed initialization.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -16,9 +16,6 @@
|
|||||||
|
|
||||||
package top.charles7c.continew.starter.captcha.graphic.autoconfigure;
|
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 org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import top.charles7c.continew.starter.captcha.graphic.enums.GraphicCaptchaType;
|
import top.charles7c.continew.starter.captcha.graphic.enums.GraphicCaptchaType;
|
||||||
import top.charles7c.continew.starter.core.constant.PropertiesConstants;
|
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;
|
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() {
|
public boolean isEnabled() {
|
||||||
return enabled;
|
return enabled;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user