common-verification-code-spring-boot-starter


License

License

Categories

Categories

Spring Boot Container Microservices
GroupId

GroupId

com.github.fangzhengjin
ArtifactId

ArtifactId

common-verification-code-spring-boot-starter
Last Version

Last Version

0.0.10
Release Date

Release Date

Type

Type

jar
Description

Description

common-verification-code-spring-boot-starter
common-verification-code-spring-boot-starter
Project URL

Project URL

https://github.com/fangzhengjin/common-verification-code-spring-boot-starter
Source Code Management

Source Code Management

https://github.com/fangzhengjin/common-verification-code-spring-boot-starter

Download common-verification-code-spring-boot-starter

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.fangzhengjin/common-verification-code-spring-boot-starter/ -->
<dependency>
    <groupId>com.github.fangzhengjin</groupId>
    <artifactId>common-verification-code-spring-boot-starter</artifactId>
    <version>0.0.10</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.fangzhengjin/common-verification-code-spring-boot-starter/
implementation 'com.github.fangzhengjin:common-verification-code-spring-boot-starter:0.0.10'
// https://jarcasting.com/artifacts/com.github.fangzhengjin/common-verification-code-spring-boot-starter/
implementation ("com.github.fangzhengjin:common-verification-code-spring-boot-starter:0.0.10")
'com.github.fangzhengjin:common-verification-code-spring-boot-starter:jar:0.0.10'
<dependency org="com.github.fangzhengjin" name="common-verification-code-spring-boot-starter" rev="0.0.10">
  <artifact name="common-verification-code-spring-boot-starter" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.fangzhengjin', module='common-verification-code-spring-boot-starter', version='0.0.10')
)
libraryDependencies += "com.github.fangzhengjin" % "common-verification-code-spring-boot-starter" % "0.0.10"
[com.github.fangzhengjin/common-verification-code-spring-boot-starter "0.0.10"]

Dependencies

compile (2)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-reflect jar 1.3.71
org.jetbrains.kotlin : kotlin-stdlib-jdk8 jar 1.3.71

runtime (1)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-web jar 2.2.6.RELEASE

Project Modules

There are no modules declared in this project.

common-verification-code-spring-boot-starter

Codecov branch Build Status Maven Central Bintray License SpringBootVersion

导入依赖

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

  1. 如需实现自定义验证码生成器,请参考下方样例(提供默认实现,不可更改默认实现配置)
/**
 * 实现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
    }
}
  1. 如需实现自定义验证码验证器,请实现VerificationValidatorProvider接口下的render方法和isSupports并注入到Spring容器中(参考自定义验证码生成)

  2. 使用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 -> "验证码已过期"
        }
    }
}

Versions

Version
0.0.10
0.0.9
0.0.7
0.0.5
0.0.3
0.0.2
0.0.1