Skip to content

Demo app of how to protect an endpoint with captcha using @akiraly/cage.

Notifications You must be signed in to change notification settings

andre-bedregal/java-captcha-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Logo Logo

Java Captcha demo app

Purpose

Demo API which generates a totally customizable captcha using @akiraly/cage.

Technologies Used

Requirements

  • JDK (version 11+) installed.

Architecture and directories

Project
├── src
|   ├── main
|   |   ├── java
|   |   |   ├── com.github.javacaptchademo
|   |   |   |   ├── captcha: Customized captcha generator.
|   |   |   |   ├── controller: Endpoints of the application.
|   |   |   |   ├── dto: Data transfer objects.
|   |   |   |   ├── service: Business rules to save the generated captcha in cache.
|   |   |   |   ├── JavaCaptchaDemoApplication.java: Entry point of the Spring Boot Application.
|   |   ├── resources
|   |   |   ├── fonts: Fonts to be used by the captcha generator.
|   |   |   ├── samples: Sample captcha images generated.
|   |   |   ├── application.properties: Properties of the application.
│   |── tests: Unit tests to validate business rules
├── package.json: File that manages all the dependecies and contains script definitions.

Local Configuration

  • After cloning the project and installing JDK 11+, you can simply run ./mvnw spring-boot:run.

Documentation

GET: Request a new captcha

http://localhost:8080/captcha

Obtains a new captcha (Base64 encoded image) along with the captcha id.

{
  "captchaId": "5ff6735a-ba78-4c97-8c8d-76b44ff06f3e",
  "captchaEncodedImage": "/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wAARCABGAMgDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII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yhn7KKpabql1fXBAiAjzyfSszTtLn1CTzZywjzyT1NdRbwR28YjiUKopVI0qUeXeQ6bqVXzbIlooorjOoKKKKACiiigAooooAKKKKACiiigAqOZDJC6A4LDGakooTsG5zA0GZL1Od0eclq6GG2igJMaAFupqaitqledTcyp0Y09gprKGUqehGKdRWJqcbfWklhe79vybsg11NndxXUKtGwJxyKkngjuIykigg1ivoc8Em60mIHpXY6ka8UpuzRyKEqMm4q6Zv0h461hm+1OLKmEHb/FWXc6xeSFlMmAeCBUwwk5PRoqWJjHoyx4ivknmWGPkJ1PvTNG0k3biaUERKenrTNJ0t72XfKCIh39a6yONYowiDCgYAroq1VRh7KG5hSpOrL2k9hVUIoVRgDtTqKK809AKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKAI54VniaNs4b0rJi8PQJMWdiy9loorWFScE1Fmc6cZNNo2ERY0CoAFHQCnUUVkaBRRRQAUUUUAf/Z"
}

HTTP Status: 200 OK.

You can render the image in an HTML <img> tag like this:

<img alt="Captcha" id="5ff6735a-ba78-4c97-8c8d-76b44ff06f3e" src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wAARCABGAMgDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i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q6GG2igJMaAFupqaitqledTcyp0Y09gprKGUqehGKdRWJqcbfWklhe79vybsg11NndxXUKtGwJxyKkngjuIykigg1ivoc8Em60mIHpXY6ka8UpuzRyKEqMm4q6Zv0h461hm+1OLKmEHb/FWXc6xeSFlMmAeCBUwwk5PRoqWJjHoyx4ivknmWGPkJ1PvTNG0k3biaUERKenrTNJ0t72XfKCIh39a6yONYowiDCgYAroq1VRh7KG5hSpOrL2k9hVUIoVRgDtTqKK809AKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKAI54VniaNs4b0rJi8PQJMWdiy9loorWFScE1Fmc6cZNNo2ERY0CoAFHQCnUUVkaBRRRQAUUUUAf/Z"

Captcha

POST: Access to a captcha protected endpoint

localhost:8080/protected?captchaId=5ff6735a-ba78-4c97-8c8d-76b44ff06f3e&captcha=ditem&username=andre

Parameters

Name Type Description
captchaId string (Required) The id of the captcha obtained.
captcha string (Required) The solved captcha.
username string (Required) A username to be displayed in the response.

If the solved captcha matches the value in the server, it will return a greeting for the provided username:

Hello andre

HTTP Status: 200 OK.

Otherwise, the server will return an error response of:

Wrong captcha

HTTP Status: 400 Bad Request.

Possible Improvements

A system is never perfect and there is always room for improvement. Here are some points that I would like to enhance if I had more time:

  • Add Redis as cache;
  • Add more customization to the generated captcha;

Support

  • If you have any query or doubt, please, feel free to contact me by e-mail.

About

Demo app of how to protect an endpoint with captcha using @akiraly/cage.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages