Kaptcha是一个比较好用的验证码组件,也是比较常用的,比其他验证码组件生成简便多了。
Kaptcha是谷歌放在github上的一个验证码jar包,我们可以简单配置属性实现验证码的验证功能
作用
- 验证码取值范围:数字,字符,汉字
- 自动生成随机验证码数字,数字个数
- 验证码图片背景,大小,边框粗细,边框颜色
- 验证码干扰线
- 验证码样式
pom.xml
<!-- 验证码 -->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
配置
配置kaptcha有两种方式,一个是基于xml形式,一种是java代码配置。
我使用的是java代码形式,本文也是介绍的这种(xml太麻烦了,看着就不舒服)。
package com.zjg.smsrt.util;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.util.Properties;
/**
* @author zjg
* @date 2018/7/24 21:27
* @Description 验证码工具类
*/
@Slf4j
@Component
public class KaptchaConfig {
@Bean
public DefaultKaptcha getDefaultKaptche () {
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
Properties properties = new Properties();
properties.setProperty("kaptcha.border","yes");
properties.setProperty("kaptcha.border.color","105,179,90");
properties.setProperty("kaptcha.textproducer.font.color","127,96,0");
properties.setProperty("kaptcha.textproducer.font.size","30");
properties.setProperty("kaptcha.image.width","110");
properties.setProperty("kaptcha.image.height","40");
properties.setProperty("kaptcha.session.key","code");
properties.setProperty("kaptcha.textproducer.char.length","4");
properties.setProperty("kaptcha.textproducer.font.names","宋体,楷体,微软雅黑");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
然后就可以调用它了
package com.zjg.smsrt.controller;
import lombok.extern.slf4j.Slf4j;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
* @author zjg
* @date 2018/7/8 20:14
* @Description
*/
@Slf4j
@RestController
public class LoginController {
@Autowired
private final DefaultKaptcha kaptcha;
@GetMapping("/kaptcha")
public void kaptcha ( HttpServletResponse servletResponse) {
log.info("tologin");
byte [] kaptchaBytes = null;
ByteArrayOutputStream byteArrOUT = new ByteArrayOutputStream();
String kaptchaText = kaptcha.createText();
BufferedImage bufferedImage = kaptcha.createImage(kaptchaText);
try {
ImageIO.write(bufferedImage,"jpg",byteArrOUT);
} catch (IOException e) {
log.info("输出验证码图片发生异常");
return;
}
//定义输出类型为image/jpeg类型,使用response输出流输出图片的byte数组
kaptchaBytes = byteArrOUT.toByteArray();
servletResponse.setHeader("Cache-Control","no-store");
servletResponse.setHeader("Pragma","no-cache");
servletResponse.setDateHeader("Expires",0);
servletResponse.setContentType("image/jpeg");
try {
ServletOutputStream servletOutputStream = servletResponse.getOutputStream();
servletOutputStream.write(kaptchaBytes);
servletOutputStream.flush();
servletOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
然后访问http://localhost:8080/kaptcha
看下运行效果截图
看具体的样式参数
Constant | 描述 | 默认值 |
---|---|---|
kaptcha.border | 图片边框,合法值:yes , no | yes |
kaptcha.border.color 边框颜色,合法值: r,g,b (and optional alpha) 或者 | white,black,blue. | black |
kaptcha.border.thickness | 边框厚度,合法值:>0 | 1 |
kaptcha.image.width | 图片宽 | 200 |
kaptcha.image.height | 图片高 | 50 |
kaptcha.producer.impl | 图片实现类 | com.google.code.kaptcha.impl.DefaultKaptcha |
kaptcha.textproducer.impl | 文本实现类 | com.google.code.kaptcha.text.impl.DefaultTextCreator |
kaptcha.textproducer.char.string | 文本集合,验证码值从此集合中获取 | abcde2345678gfynmnpwx |
kaptcha.textproducer.char.length | 验证码长度 | 4 |
kaptcha.textproducer.font.names | 字体 | Arial, Courier |
kaptcha.textproducer.font.size | 字体大小 | 40px. |
kaptcha.textproducer.font.color | 字体颜色,合法值: r,g,b 或者 white,black,blue. | black |
kaptcha.textproducer.char.space | 文字间隔 | 2 |
kaptcha.noise.impl | 干扰实现类 | com.google.code.kaptcha.impl.DefaultNoise |
kaptcha.noise.color | 干扰 颜色,合法值: r,g,b 或者 white,black,blue. black | |
kaptcha.obscurificator.impl | 图片样式: 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy阴影com.google.code.kaptcha.impl.ShadowGimpy | com.google.code.kaptcha.impl.WaterRipple |
kaptcha.background.impl | 背景实现类 | com.google.code.kaptcha.impl.DefaultBackground |
kaptcha.background.clear.from | 背景颜色渐变,开始颜色 | light grey |
kaptcha.background.clear.to | 背景颜色渐变, 结束颜色 | white |
kaptcha.word.impl | 文字渲染器 | com.google.code.kaptcha.text.impl.DefaultWordRenderer |
kaptcha.session.key | session key | KAPTCHA_SESSION_KEY |
kaptcha.session.date | session date | KAPTCHA_SESSION_DATE |