• Introduce kaptCHA dependencies
  • Adding a Configuration Class
  • Interface implementation
  • test

Verification code

Login assistance authentication is a common but necessary feature used by most systems. There are a variety of verification methods, such as graphic verification, verification bar, dragging puzzle pieces, etc. Here is a graphic verification code for the way to achieve. The open source implementation of KapCHA is adopted.

Introduction of kaptcha

Kaptcha is a highly configurable utility captcha generator with optional options such as:

  • The font of the verification code
  • The size of the captcha font
  • The font color of the verification code font
  • Verification code content range (numbers, letters, Chinese characters!)
  • Captcha image size, border, border thickness, border color
  • Verification code interference line
  • Captcha styles (Fisheye style, 3D, plain blur,…)

Kaptcha configuration

Constant describe The default value
kaptcha.border Image border, valid value: yes, no yes
kaptcha.border.color Border color, legal values: R, G, B (and optional alpha) or White,black,blue black
kaptcha.image.width Image width 200
kaptcha.image.height Pictures of high 50
kaptcha.producer.impl Picture implementation class com.google.code.kaptcha.impl.DefaultKaptcha
kaptcha.textproducer.impl Text implementation class com.google.code.kaptcha.text.impl.DefaultTextCreator
kaptcha.textproducer.char.string Text collection from which the captcha value is obtained abcde2345678gfynmnpwx
kaptcha.textproducer.char.length Verification code length 5
kaptcha.textproducer.font.names The font Arial, Courier
kaptcha.textproducer.font.size The font size 40px.
kaptcha.textproducer.font.color Font color, legal values: R, G, B or white,black,blue. black
kaptcha.textproducer.char.space Text interval 2
kaptcha.noise.impl Interference implementation class com.google.code.kaptcha.impl.DefaultNoise
kaptcha.noise.colorv Interference colors, legal values: R, G, B or white,black,blue. black
kaptcha.obscurificator.impl Image style:

Water lines com. Google. Code. Kaptcha. Impl. WaterRipple

Fisheye com. Google. Code. Kaptcha. Impl. FishEyeGimpy

Shadow com. Google. Code. Kaptcha. Impl. ShadowGimpy
com.google.code.kaptcha.impl.WaterRipple
kaptcha.background.impl Background implementation class com.google.code.kaptcha.impl.DefaultBackground
kaptcha.background.clear.from Background color gradient, start color light grey
kaptcha.background.clear.to Background color gradient, end color white
kaptcha.word.impl Text renderer com.google.code.kaptcha.text.impl.DefaultWordRenderer
kaptcha.session.key session key KAPTCHA_SESSION_KEY
kaptcha.session.date session date KAPTCHA_SESSION_DATE

Implementation captcha

Introduce kaptCHA dependencies

        <! Add captcha implementation library dependency kaptcha-->
        <dependency>
            <groupId>com.github.axet</groupId>
            <artifactId>kaptcha</artifactId>
            <version>0.0.9</version>
 </dependency> Copy the code

Adding a Configuration Class

/ * ** Captcha configuration class* /
@Configuration
public class KaptchaConfig {
  @Bean  public DefaultKaptcha producer(a) throws KrbException {  Properties properties = new Properties();  properties.put("kaptcha.border"."no");  properties.put("kaptcha.textproducer.font,color"."black");  properties.put("kaptcha.textproducer.char.space"."5");  Config config = new Config(properties);  DefaultKaptcha defaultKaptcha = new DefaultKaptcha();  defaultKaptcha.setConfig(config);  return defaultKaptcha;  } } Copy the code

Interface implementation

@Api(tags = "Login Verification")
@RequestMapping(value = "login")
@RestController
public class SysLoginController {

 @Autowired  private Producer producer;   @ApiOperation(value = "Generate captcha image")  @GetMapping(value = "captchat.jpg")  public void createKacptcha(HttpServletResponse response, HttpServletRequest request) throws IOException {  response.setHeader("Cache-Control"."no-store");  response.setContentType("image/jpeg");  // Text verification code  String text = producer.createText();  // Image verification code  BufferedImage image = producer.createImage(text);  // Save the verification code to session  request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY,text);  ServletOutputStream outputStream = response.getOutputStream();  ImageIO.write(image,"jpg",outputStream);  IOUtils.closeQuietly(outputStream);  } } Copy the code

test

Open the Swagger interface test page and execute the interface. The result is as follows. Each time it is executed, the verification code is refreshed


This article is formatted using MDNICE