tonglin0325的个人主页

SpringBoot学习笔记——kaptcha

kaptcha是一个java验证码生成框架,可以和spring集成用于验证码服务

和spring集成的官方文档

1
2
https://code.google.com/archive/p/kaptcha/wikis/SpringUsage.wiki

1.依赖

1
2
3
4
5
6
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>

2.kaptcha生成验证码的配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
public class KaptchaConfig {

/**
* 验证码生成器参数
*/
@Bean
public DefaultKaptcha captchaProducer() {
DefaultKaptcha captchaProducer = new DefaultKaptcha();
Properties properties = new Properties();
properties.setProperty(Constants.KAPTCHA_IMAGE_WIDTH, "100");
properties.setProperty(Constants.KAPTCHA_IMAGE_HEIGHT, "30");
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE, "22");
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "6");
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR, "black");
properties.setProperty(Constants.KAPTCHA_BORDER_COLOR, "LIGHT_GRAY");
properties.setProperty(Constants.KAPTCHA_BACKGROUND_CLR_FROM, "WHITE");
properties.setProperty(Constants.KAPTCHA_NOISE_IMPL, "com.google.code.kaptcha.impl.NoNoise");
properties.setProperty(Constants.KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy");
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_STRING, "0123456789");
properties.setProperty(Constants.KAPTCHA_SESSION_CONFIG_KEY, "checkCode");
Config config = new Config(properties);
captchaProducer.setConfig(config);
return captchaProducer;
}

}

配置的含义参考

1
https://code.google.com/archive/p/kaptcha/wikis/ConfigParameters.wiki

验证码图片生成接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;

@Slf4j
@Controller
@RequestMapping("/kaptcha")
public class KaptchaController {

@Autowired
private Producer captchaProducer;

@RequestMapping("/getCode")
public ModelAndView getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setDateHeader("Expires", 0);
// Set standard HTTP/1.1 no-cache headers.
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
// Set standard HTTP/1.0 no-cache header.
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
// create the text for the image
String capText = captchaProducer.createText();
log.info("******************验证码是: " + capText + "******************");
// store the text in the session
request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
// create the image with the text
BufferedImage bi = captchaProducer.createImage(capText);
ServletOutputStream out = response.getOutputStream();
// write the data out
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
return null;
}
}

验证码校验和参数含义参考:Google-kaptcha验证码使用步骤(基于springboot/使用redis存储)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
kaptcha.border                     	| Border around kaptcha. Legal values are yes or no.                     			| yes 
kaptcha.border.color | Color of the border. Legal values are r,g,b (and optional alpha) or white,black,blue. | black
kaptcha.border.thickness | Thickness of the border around kaptcha. Legal values are > 0. | 1
kaptcha.image.width | Width in pixels of the kaptcha image. | 200
kaptcha.image.height | Height in pixels of the kaptcha image. | 50
kaptcha.producer.impl | The image producer. | com.google.code.kaptcha.impl.DefaultKaptcha
kaptcha.textproducer.impl | The text producer. | com.google.code.kaptcha.text.impl.DefaultTextCreator
kaptcha.textproducer.char.string | The characters that will create the kaptcha. | abcde2345678gfynmnpwx
kaptcha.textproducer.char.length | The number of characters to display. | 5
kaptcha.textproducer.font.names | A list of comma separated font names. | Arial, Courier
kaptcha.textproducer.font.size | The size of the font to use. | 40px.
kaptcha.textproducer.font.color | The color to use for the font. Legal values are r,g,b. | black
kaptcha.textproducer.char.space | The space between the characters | 2
kaptcha.noise.impl | The noise producer. | com.google.code.kaptcha.impl.DefaultNoise
kaptcha.noise.color | The noise color. Legal values are r,g,b. | black
kaptcha.obscurificator.impl | The obscurificator implementation. | com.google.code.kaptcha.impl.WaterRipple
kaptcha.background.impl | The background implementation. | com.google.code.kaptcha.impl.DefaultBackground
kaptcha.background.clear.from | Starting background color. Legal values are r,g,b. | light grey
kaptcha.background.clear.to | Ending background color. Legal values are r,g,b. | white
kaptcha.word.impl | The word renderer implementation. | com.google.code.kaptcha.text.impl.DefaultWordRenderer
kaptcha.session.key | The value for the kaptcha is generated and is put into the HttpSession. | KAPTCHA_SESSION_KEY
This is the key value for that item in the session.
kaptcha.session.date | The date the kaptcha is generated is put into the HttpSession. | KAPTCHA_SESSION_DATE
This is the key value for that item in the session.

效果

校验的原理:

生成验证码的时候返回一个uuid给浏览器,同时将uuid作为key,验证码作为value存在服务器session当中,比如redis,前端提交验证码的时候将uuid带上,查询redis进行验证