kapcha-spring-boot-autoconfigure

Kaptcha Spring Boot AutoConfigurer

License

License

Categories

Categories

Spring Boot Container Microservices Auto Application Layer Libs Code Generators config Configuration
GroupId

GroupId

com.oopsguy.kaptcha
ArtifactId

ArtifactId

kapcha-spring-boot-autoconfigure
Last Version

Last Version

1.0.0-beta
Release Date

Release Date

Type

Type

jar
Description

Description

kapcha-spring-boot-autoconfigure
Kaptcha Spring Boot AutoConfigurer
Project URL

Project URL

https://github.com/12software/kaptcha-spring-boot

Download kapcha-spring-boot-autoconfigure

How to add to project

<!-- https://jarcasting.com/artifacts/com.oopsguy.kaptcha/kapcha-spring-boot-autoconfigure/ -->
<dependency>
    <groupId>com.oopsguy.kaptcha</groupId>
    <artifactId>kapcha-spring-boot-autoconfigure</artifactId>
    <version>1.0.0-beta</version>
</dependency>
// https://jarcasting.com/artifacts/com.oopsguy.kaptcha/kapcha-spring-boot-autoconfigure/
implementation 'com.oopsguy.kaptcha:kapcha-spring-boot-autoconfigure:1.0.0-beta'
// https://jarcasting.com/artifacts/com.oopsguy.kaptcha/kapcha-spring-boot-autoconfigure/
implementation ("com.oopsguy.kaptcha:kapcha-spring-boot-autoconfigure:1.0.0-beta")
'com.oopsguy.kaptcha:kapcha-spring-boot-autoconfigure:jar:1.0.0-beta'
<dependency org="com.oopsguy.kaptcha" name="kapcha-spring-boot-autoconfigure" rev="1.0.0-beta">
  <artifact name="kapcha-spring-boot-autoconfigure" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.oopsguy.kaptcha', module='kapcha-spring-boot-autoconfigure', version='1.0.0-beta')
)
libraryDependencies += "com.oopsguy.kaptcha" % "kapcha-spring-boot-autoconfigure" % "1.0.0-beta"
[com.oopsguy.kaptcha/kapcha-spring-boot-autoconfigure "1.0.0-beta"]

Dependencies

compile (4)

Group / Artifact Type Version
org.springframework.boot : spring-boot-autoconfigure jar 2.1.1.RELEASE
org.springframework.boot : spring-boot-configuration-processor Optional jar 2.1.1.RELEASE
com.oopsguy.kaptcha : kaptcha jar 1.0.0-beta
javax.annotation : javax.annotation-api jar 1.3.2

provided (1)

Group / Artifact Type Version
javax.servlet : javax.servlet-api jar 3.1.0

Project Modules

There are no modules declared in this project.

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: true
    color: '200,200,200'
    thickness: 1
  noise:
    color: '239,166,131'
    # you can specify your own implementation
    impl:
  obscurificator:
    impl:
  producer:
    impl:
  background:
    impl:
    color-from: '255,0,0'
    color-to: '255,0,0'
  text-producer:
    impl:
    character:
      string: '01234567890ABCDEF'
      length: 4
      space: 10
    font:
      names:
      color: '255,255,255'
      size: 46
  word:
    impl:
  image:
    width: 200
    height: 60
  # You can configure multiple captcha
  # and configure properties for them individually
  items:
    home:
      path: /home/captcha
      session:
        key: homeCaptcha
      background:
        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: 8080

kaptcha:
  border:
    enbaled: true
    image:
      height: 60
      width: 160
  items:
    # home captcha
    home:
      path: /home/capthca
      text-producer:
        font:
          size: 16
      image:
        height: 40
    # admin captcha
    admin:
      path: /admin/capthca
      text-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")
class SystemController {
    
    @Resource
    private Producer captchaProducer;

    @GetMapping("/captcha")
    public void getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        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");
        String capText = captchaProducer.createText();
        // Save the captcha code to the session
        request.getSession().setAttribute("captchaCode", capText);
        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = 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

com.oopsguy.kaptcha

12 Software Engineer

Versions

Version
1.0.0-beta