目标:Spring Boot 3.x 项目中一行配置接入 suretool 的常用组件(验证码等)。
<dependency>
<groupId>io.github.tasure</groupId>
<artifactId>sure-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
引入依赖后,自动配置生效,可直接注入:
import com.sure.tool.boot.CaptchaGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CaptchaService {
@Autowired
private CaptchaGenerator captchaGenerator;
public String createCaptcha() {
return captchaGenerator.generate().getImageBase64(); // 生成 Base64 验证码图片
}
public boolean verifyCaptcha(String userInputCode) {
return captchaGenerator.generate().verify(userInputCode); // 校验(忽略大小写)
}
}
suretool:
captcha:
width: 320 # 默认 200
height: 120 # 默认 100
enabled: true # false 时关闭自动装配(Bean 不注册)
不需要 starter 组件时,关闭即可(Bean 不再注册):
suretool:
captcha:
enabled: false
完整可运行示例见 sure-examples:
# 克隆仓库后直接运行
cd sure-examples
mvn spring-boot:run
starter 有自动化冒烟测试(SureToolAutoConfigurationTest):
CaptchaGeneratorwidth=320/height=120 配置生效enabled=false 时 Bean 不存在接入即得质量保障,配置错误会在启动时暴露而非运行期。