common-verification-code-spring-boot-starter
导入依赖
dependencies {
implementation "com.github.fangzhengjin:common-verification-code-spring-boot-starter:version"
}
开启需要的组件
#以下选项均为默认值
customize:
common:
verification:
recaptcha:
enable: true
redis:
enable: false
session:
enable: true
使用VerificationHelper
- 如需实现自定义验证码生成器,请参考下方样例(提供默认实现,不可更改默认实现配置)
/**
* 实现VerificationGeneratorProvider接口下的render方法和isSupports并将当前实现类注册到Spring容器中
*/
@Component
class DefaultVerificationGeneratorProvider : VerificationGeneratorProvider {
/**
* 验证码生成
*/
override fun render(): VerificationCode {
return VerificationCode(
randomCode, //验证码文本
buffImg //验证码图片流
)
}
/**
* 是否支持该类型的验证码生成
*/
override fun isSupports(verificationType: VerificationType): Boolean {
return verificationType == VerificationType.IMAGE
}
}
-
如需实现自定义验证码验证器,请实现VerificationValidatorProvider接口下的render方法和isSupports并注入到Spring容器中(参考自定义验证码生成)
-
使用VerificationHelper对象
@RestController
class DemoController {
@GetMapping("/verify/image")
fun image() {
val render = VerificationHelper.render()
println("验证码:$render")
}
@GetMapping("/verify/check")
fun verifyCheck(@RequestParam code: String) {
/**
* 验证码校验
* @param code 用户输入的验证码
* @param expireInSeconds 验证码有效期(秒),默认60
* @param cleanupVerificationInfoWhenWrong 验证码输入错误时,是否作废之前的验证码信息,默认false
* @param throwException 验证不通过时,是否抛出异常,默认false
* @return 如果选择验证不通过不抛出异常,则返回VerificationStatus验证状态枚举
*/
val status = VerificationHelper.validate(
code, //用户输入的验证码
60L //验证码有效期(秒),默认60秒
)
val msg = when (status) {
VerificationStatus.SUCCESS -> VerificationStatus.SUCCESS.message
VerificationStatus.WRONG -> "验证码错误"
VerificationStatus.NOT_FOUNT -> "认证信息未找到"
VerificationStatus.EXPIRED -> "验证码已过期"
}
}
}