refactor(captcha/graphic): 新增图形验证码服务接口,并调整验证码默认启用

This commit is contained in:
2024-02-04 23:12:25 +08:00
parent 2b0fb9aff9
commit 3184faaa27
3 changed files with 60 additions and 16 deletions

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 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

View File

@@ -32,7 +32,7 @@ public class GraphicCaptchaProperties {
/**
* 是否启用图形验证码
*/
private boolean enabled = false;
private boolean enabled = true;
/**
* 类型

View File

@@ -0,0 +1,54 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* 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
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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;
}
}