ReCAPTCHA Parent

The parent module of the reCAPTCHA integration project for Spring Boot.

License

License

GroupId

GroupId

com.szityu.oss.spring.recaptcha
ArtifactId

ArtifactId

recaptcha-parent
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

pom
Description

Description

ReCAPTCHA Parent
The parent module of the reCAPTCHA integration project for Spring Boot.
Project URL

Project URL

https://github.com/Mr-DeWitt/spring-boot-recaptcha
Source Code Management

Source Code Management

https://github.com/Mr-DeWitt/spring-boot-recaptcha

Download recaptcha-parent

How to add to project

<!-- https://jarcasting.com/artifacts/com.szityu.oss.spring.recaptcha/recaptcha-parent/ -->
<dependency>
    <groupId>com.szityu.oss.spring.recaptcha</groupId>
    <artifactId>recaptcha-parent</artifactId>
    <version>1.1.0</version>
    <type>pom</type>
</dependency>
// https://jarcasting.com/artifacts/com.szityu.oss.spring.recaptcha/recaptcha-parent/
implementation 'com.szityu.oss.spring.recaptcha:recaptcha-parent:1.1.0'
// https://jarcasting.com/artifacts/com.szityu.oss.spring.recaptcha/recaptcha-parent/
implementation ("com.szityu.oss.spring.recaptcha:recaptcha-parent:1.1.0")
'com.szityu.oss.spring.recaptcha:recaptcha-parent:pom:1.1.0'
<dependency org="com.szityu.oss.spring.recaptcha" name="recaptcha-parent" rev="1.1.0">
  <artifact name="recaptcha-parent" type="pom" />
</dependency>
@Grapes(
@Grab(group='com.szityu.oss.spring.recaptcha', module='recaptcha-parent', version='1.1.0')
)
libraryDependencies += "com.szityu.oss.spring.recaptcha" % "recaptcha-parent" % "1.1.0"
[com.szityu.oss.spring.recaptcha/recaptcha-parent "1.1.0"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

  • lib
  • autoconfigure
  • starter
  • sample-app

Spring Boot reCAPTCHA starter

This project intended to make reCAPTCHA integration easier for Spring Boot projects through a custom starter and autoconfiguration.

Prerequisites

The starter's autoconfiguration will be triggered if the following requirements are met:

  • The project runs in a web environment e.g.: via a simple spring web starter.
  • The recaptcha.protected-urls property has been set.
  • The reCAPTCHA maven starter project included into dependencies:
<dependencies>
    ...
    <dependency>
        <groupId>com.szityu.oss.spring.recaptcha</groupId>
        <artifactId>recaptcha-spring-boot-starter</artifactId>
        <packaging>jar</packaging>
        <version>1.1.0</version>
    </dependency>
    ...
</dependencies>

Usage scenarios

Simple usage

If you do not want to customize the reCAPTCHA validation behaviour just configure it from application properties. At the moment the following properties can be configured:

  • recaptcha.secret The secret key obtained from Google's reCAPTCHA service used for validating user sent captcha values.
  • recaptcha.protectedUrls An Ant styled URL list of captcha protected resources separated by commas.
  • recaptcha.headerName The header name in which the client needs to send the captcha check value. The default value is g-recaptcha-response.
  • recaptcha.excludedProfiles A profile name list. If one of the profiles is active the reCAPTCHA validator filter won't be registered. This is useful if you don't need reCAPTCHA validation during dev stage or integration testing.

Example

Place the following lines into your application.yaml

recaptcha:
    secret: your-very-secret-code-from-google
    protectedUrls: /protected/urls/**,/other/protected-url

That's all, you're good to go now, try to reach your protected URL-s. Without a valid reCAPTCHA response HTTP 400 error will be thrown by the server.

Customize server responses

If you want custom server responses for different scenarios, just define your own callback handler beans. At the moment the following callback handlers are supported:

  • ValidCaptchaCallbackHandler: this handler will be called when a client sends a valid reCAPTCHA value.
  • InvalidCaptchaCallbackHandler: this handler will be called when a client sends an invalid reCAPTCHA value.
  • MissingCaptchaCallbackHandler: this handler will be called when a client does not send any reCAPTCHA value.

Example

If we want custom error handling.

@Configuration
public class MyOwnCustomCallbackHandlers {
    
    @Bean
    public InvalidCaptchaCallbackHandler invalidCaptchaCallbackHandler () {
        return new InvalidCaptchaCallbackHandler() {
            @Override
            public void handle(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
                response.setStatus(418);
                response.getWriter().append("My own custom invalid captcha error response body.");
            }
        };
    }
    
    @Bean
    public MissingCaptchaCallbackHandler missingCaptchaCallbackHandler () {
        return new MissingCaptchaCallbackHandler() {
            @Override
            public void handle(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
                response.setStatus(418);
                response.getWriter().append("O!M!G! You didn't send captcha!");
            }
        };
    }
}

Fully customized behaviour

If you have totally different opinion about how reCAPTCHA validation should be done (or just find a bug you are eager to solve), then define a bean with type RecaptchaValidatorFilter. You can override a bunch of it's methods and customize it's basic behaviour.

Example

Note that this filter requires RecaptchaConfigProperties which can be simple autowired.

@Configuration
public class IKnowBetterConfig {
    
    @Bean
    public RecaptchaValidatorFilter recaptchaValidatorFilter (RecaptchaConfigProperties reCaptchaConfigProperties) {
        return new RecaptchaValidatorFilter(reCaptchaConfigProperties) {
            @Override
            protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
                // My own implementation.
            }
        };
    }
}

See RecaptchaValidatorFilter's & RecaptchaValidatorCallbackFilter's Java doc for further information.

Author

License

This project is licensed under the MIT License - see the LICENSE file for details

Versions

Version
1.1.0
1.0.1
1.0.0