DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Compliance Automated Standard Solution (COMPASS), Part 10: How OSCAL Mapping Paves the Way for Continuous Compliance Scalability
  • GenAI Implementation Isn't Magic — It’s a Lifecycle
  • The Phantom Write Problem: Why Your Idempotency Implementation Is Silently Losing Data
  • Standards as the Invisible Infrastructure of Software

Trending

  • GCP Secret Manager, Live-Reloaded in Go With mamori
  • Stop Debugging Code That Works: Identifying False Failures in Kubernetes
  • Engineering Audit-Ready Data Pipelines for Regulatory Reporting
  • Building Enterprise File-Heavy AI Workflows: From Secure Uploads to Governed Document Intelligence

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

Captcha Implementation

Captcha Implementation in Spring boot.

By 
Vicky Singh user avatar
Vicky Singh
·
Jul. 10, 22 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
8.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

CAPTCHA helps protect you from spam and password decryption by asking you to complete a simple test that proves you are human and not a computer trying to break into a password-protected account.

We can implement a captcha in Spring boot where a user can get an encoded image, and the corresponding readable captcha can be stored in the Database.

We can add simplecaptcha in the pom file to include inside the dependencies.

XML
 
<dependency>
	<groupId>cn.apiclub.tool</groupId>
	<artifactId>simplecaptcha</artifactId>
	<!-- we have to manage version here -->
	<version>1.2.2</version> 
 </dependency>

We can create the captcha by providing the height and width for the same and inside the create captcha we can add the text, background-color, and gimp as well like FishEyeGimpyRenderer

Java
 
Captcha captcha = createCaptcha(240, 70);
String readableImage = encodeCaptcha(captcha);
Java
 
public static Captcha createCaptcha(Integer width, Integer height) {

		return new Captcha.Builder(width, height).addBackground(new GradiatedBackgroundProducer())
				.addText(new DefaultTextProducer(6, DEFAULT_CHARS), new DefaultWordRenderer()).gimp(new FishEyeGimpyRenderer()).addNoise()
				.build();
	}

We can also use encode Captcha method by passing the encoded captcha to Base64, getEncoder method

Java
 
public static String encodeCaptcha(Captcha captcha) {
		String image = null;
		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ImageIO.write(captcha.getImage(), "jpg", bos);
			byte[] byteArray = Base64.getEncoder().encode(bos.toByteArray());
			image = new String(byteArray);
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		return image;
}

 When we have the readable captcha, we can store it in DB or cache to validate whether the user has used the correct captcha or not.

Happy Coding!

Implementation

Opinions expressed by DZone contributors are their own.

Related

  • Compliance Automated Standard Solution (COMPASS), Part 10: How OSCAL Mapping Paves the Way for Continuous Compliance Scalability
  • GenAI Implementation Isn't Magic — It’s a Lifecycle
  • The Phantom Write Problem: Why Your Idempotency Implementation Is Silently Losing Data
  • Standards as the Invisible Infrastructure of Software

Partner Resources

×

Comments

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook