Skip to content

Repository files navigation

Kaptcha Spring Boot Starter

Kaptcha Spring Boot Starter can help you use Google Kaptcha with Spring Boot easier.

Usage

Add kaptcha-spring-boot-starter dependency to your pom.xml file.

<dependency>
<groupId>com.oopsguy.kaptcha</groupId>
<artifactId>kaptcha-spring-boot-starter</artifactId>
<version>1.0.0-beta-2</version>
</dependency>

Properties

You can use properties to custom captcha without Java code:

kaptcha:
border:
enabled: truecolor: '200,200,200'thickness: 1noise:
color: '239,166,131'# you can specify your own implementationimpl:
obscurificator:
impl:
producer:
impl:
background:
impl:
color-from: '255,0,0'color-to: '255,0,0'text-producer:
impl:
character:
string: '01234567890ABCDEF'length: 4space: 10font:
names:
color: '255,255,255'size: 46word:
impl:
image:
width: 200height: 60# You can configure multiple captcha# and configure properties for them individuallyitems:
home:
path: /home/captchasession:
key: homeCaptchabackground:
color-from: '255,255,255'color-to: '255,255,255'text-producer:
font:
color: '68,155,44'# more items ...

Examples

YAML

The following use yaml to configure common kaptcha properties and define two kaptcha servlets(home and admin), you can configure them in your application.yml:

server:
port: 8080kaptcha:
border:
enbaled: trueimage:
height: 60width: 160items:
# home captchahome:
path: /home/capthcatext-producer:
font:
size: 16image:
height: 40# admin captchaadmin:
path: /admin/capthcatext-producer:
character:
length: 4

Then browse http://localhost:8080/home/capthca and http://localhost:8080/admin/capthca

Java Code

You can also inject a Producer bean directly by annotation to generate captcha.

@Controller@RequestMapping("/sys")
classSystemController {
@ResourceprivateProducercaptchaProducer;
@GetMapping("/captcha")
publicvoidgetKaptchaImage(HttpServletRequestrequest, HttpServletResponseresponse) throwsException {
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
StringcapText = captchaProducer.createText();
// Save the captcha code to the sessionrequest.getSession().setAttribute("captchaCode", capText);
BufferedImagebi = captchaProducer.createImage(capText);
ServletOutputStreamout = response.getOutputStream();
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
}
}

For more examples, please see kaptcha-spring-boot-starter-example

Notice

If you custom your own Producer bean, it will replace the default.

License

MIT License

About

Kaptcha Spring Boot Starter help you use Google Kaptcha with Spring Boot easier. 一个简单封装了 Kaptcha 验证码库的 Spring Boot Starter

Topics

Resources

Stars

25 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages