Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/michaelbull/spring-boot-starter-recaptcha
Spring Boot starter for Google's reCAPTCHA v3.
https://github.com/michaelbull/spring-boot-starter-recaptcha
boot captcha mvc recaptcha spring v3 validation validator verify
Last synced: about 1 month ago
JSON representation
Spring Boot starter for Google's reCAPTCHA v3.
- Host: GitHub
- URL: https://github.com/michaelbull/spring-boot-starter-recaptcha
- Owner: michaelbull
- License: isc
- Created: 2019-11-03T19:25:19.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-12-28T17:05:59.000Z (almost 3 years ago)
- Last Synced: 2023-09-13T12:38:27.807Z (over 1 year ago)
- Topics: boot, captcha, mvc, recaptcha, spring, v3, validation, validator, verify
- Language: Kotlin
- Homepage:
- Size: 134 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Spring Boot reCAPTCHA v3 Starter
[![Maven Central](https://img.shields.io/maven-central/v/com.michael-bull.spring-boot-starter-recaptcha/spring-boot-starter-recaptcha.svg)](https://search.maven.org/search?q=g:com.michael-bull.spring-boot-starter-recaptcha) [![CI Status](https://github.com/michaelbull/spring-boot-starter-recaptcha/workflows/ci/badge.svg)](https://github.com/michaelbull/spring-boot-starter-recaptcha/actions?query=workflow%3Aci) [![License](https://img.shields.io/github/license/michaelbull/spring-boot-starter-recaptcha.svg)](https://github.com/michaelbull/spring-boot-starter-recaptcha/blob/master/LICENSE)
Spring Boot starter for Google's [reCAPTCHA v3][recaptcha-v3].
## Installation
```groovy
repositories {
mavenCentral()
}dependencies {
implementation("com.michael-bull.spring-boot-starter-recaptcha:spring-boot-starter-recaptcha:1.0.4")
}
```## Getting Started
#### 1. Register reCAPTCHA v3 keys
Register your application on the [key registration page][recaptcha-v3-keys].
#### 2. Add the configuration properties to your `application.yaml`:
```yaml
recaptcha.keys:
site: ""
secret: ""
```#### 3. Model the form that recaptcha exists on:
```kotlin
class RegisterForm {var recaptchaAction: String? = "register"
var recaptchaResponseToken: String? = null
var email: String? = null
}
```#### 4. Add a validator for your form:
```kotlin
@Component
@RequestScope
class RegisterFormValidator @Inject constructor(
private val request: HttpServletRequest,
private val recaptchaValidator: RecaptchaValidator
) : Validator {override fun supports(clazz: Class<*>): Boolean {
return RegisterForm::class.java.isAssignableFrom(clazz)
}override fun validate(target: Any, errors: Errors) {
val form = target as RecoverAccountForm
val action = form.recaptchaAction
val responseToken = form.recaptchaResponseTokenrecaptchaValidator
.validate("recaptchaResponseToken", request, action, responseToken, errors)
.onSuccess { (_, response) -> checkResponse(response, errors) }
}private fun checkResponse(response: SiteVerifyResponse, errors: Errors) {
val score = response.scoreif (score != null && score < 0.2) {
errors.rejectValue("recaptchaResponseToken", "Score too low")
}
}
}
```#### 5. Bind the validator in your `Controller`:
```kotlin
@Controller
class RegisterController @Inject constructor(
private val formValidator: RegisterFormValidator
) {@InitBinder("form")
fun initFormBinder(binder: WebDataBinder) {
binder.addValidators(formValidator)
}/* get and post handlers... */
}
```## I18n
Error codes generated by the RecaptchaValidator can be internationalized by
adding the following entries to your `messages.properties`:```properties
captcha.error.actionMissing=Captcha action missing.
captcha.error.incomplete=Captcha incomplete.
captcha.error.request=Failed to submit captcha.
captcha.error.responseMissing=No response from captcha service.
captcha.error.response=Error response from captcha service.
captcha.error.failed=Captcha failed. Please try again.
captcha.error.actionMismatch=Captcha action mismatch.
```## Contributing
Bug reports and pull requests are welcome on [GitHub][github].
## License
This project is available under the terms of the ISC license. See the
[`LICENSE`](LICENSE) file for the copyright information and licensing terms.[recaptcha-v3]: https://developers.google.com/recaptcha/docs/v3
[recaptcha-v3-keys]: https://g.co/recaptcha/v3
[github]: https://github.com/michaelbull/spring-boot-starter-recaptcha