Skip to content

介绍

对外提供图片,邮箱,短信,三种方式的验证,简化基于各种类型验证码的开发,其图片验证码代码来源于 RuoYi-Vue-Plus 代码,在此基础上进行微调和二次封装。

依赖库

名称描述
zebra-spring-boot-starter-cache
commons-lang3
zebra-spring-boot-starter-message

快速开始

引入

xml
<dependency>
		<groupId>io.github.zhanghongbin</groupId>
		<artifactId>zebra-spring-boot-starter-captcha</artifactId>
</dependency>

验证码发送服务

1. 短信验证码

获取短信验证码(1分钟内,只能发送一次,暂时没有放开此配置)
如果引入的工程不存在 context-path 配置,如果有则需要在服务地址前加上context-path

服务地址方法描述
/captcha/sms/code?phoneNumber=xxxGET 请求方法phoneNumber 为手机号

2. 邮箱验证码

获取邮箱验证码(1分钟内,只能发送一次,暂时没有放开此配置)
如果引入的工程不存在 context-path 配置,如果有则需要在服务地址前加上context-path

服务地址方法描述
/captcha/email/code?email=xxxGET 请求方法email 邮箱

3. 图片验证码

获取图片验证码(1分钟内,同一ip地址只能发送10次以下,并且验证码2分钟内有效,暂时没有放开此配置)
如果引入的工程不存在 context-path 配置,如果有则需要在服务地址前加上context-path

服务地址方法描述
/captcha/picture/codeGET 请求方法无参数,返回值为以下格式
json
{
	"code":0,
	"msg":"",
	"data":{
		"enabled":true,
		"uuid":"",
		"img":""
	}
}

enabled 是否开启验证码
uuid 唯一标识
img base64加密图片

验证码验证

短信验证码验证

获取 SmsCaptchaHandler 对象,然后调用check接口

java
@Autowired
private SmsCaptchaHandler smsCaptchaHandler;

public void test() {
		//第一个参数为手机号,第二个参数为验证码
		boolean flag = smsCaptchaHandler.check(phonenumber, smsCode);
}

邮箱验证码验证

获取 MailCaptchaHandler 对象,然后调用check接口

java
@Autowired
private MailCaptchaHandler mailCaptchaHandler;

public void test() {
		//第一个参数为邮箱号,第二个参数为验证码
		boolean flag = mailCaptchaHandler.check(mail, smsCode);
}

图片验证码验证

获取 PictureCaptchaHandler 对象,然后调用check接口

java
@Autowired
private PictureCaptchaHandler pictureCaptchaHandler;
@Autowired
private PictureCaptchaProperties pictureCaptchaProperties

public void test() {
		//第一个参数为返回给前端的唯一标识,第二个参数为验证码
		boolean flag = pictureCaptchaHandler.check(uuid, code);
		//pictureCaptchaProperties 可以获取是否启用验证码
		boolean enabled = captchaProperties.isEnabled();
}