谷歌验证码工具 Kaptcha 使用
Kaptcha 简介
Kaptcha 是一个可高度配置的实用验证码生成工具,可自由配置的选项如:
- 验证码的字体
- 验证码字体的大小
- 验证码字体的字体颜色
- 验证码内容的范围(数字,字母,中文汉字!)
- 验证码图片的大小,边框,边框粗细,边框颜色
- 验证码的干扰线
- 验证码的样式(鱼眼样式、3D、普通模糊、...)
Kaptcha 详细配置表
kaptcha.border | 图片边框,合法值:yes , no | yes |
kaptcha.border.color | 边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue. | black |
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 | 验证码长度 | 5 |
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 |
图片样式:<br />水纹 com.google.code.kaptcha.impl.WaterRipple <br /> 鱼眼 com.google.code.kaptcha.impl.FishEyeGimpy <br /> 阴影 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 |
用法
在pom.xml中导入
<dependency> <groupId>com.google.code.kaptcha</groupId> <artifactId>kaptcha</artifactId> <version>2.3</version> </dependency>
或者
<dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
代码实现:
1 | KaptchaConfig.java |
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 | @Component public class KaptchaConfig { @Bean public DefaultKaptcha getDDefaultKaptcha() { DefaultKaptcha dk = 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" , "red" ); // 图片宽 properties.setProperty( "kaptcha.image.width" , "110" ); // 图片高 properties.setProperty( "kaptcha.image.height" , "40" ); // 字体大小 properties.setProperty( "kaptcha.textproducer.font.size" , "30" ); // session key properties.setProperty( "kaptcha.session.key" , "code" ); // 验证码长度 properties.setProperty( "kaptcha.textproducer.char.length" , "4" ); // 字体 properties.setProperty( "kaptcha.textproducer.font.names" , "宋体,楷体,微软雅黑" ); Config config = new Config(properties); dk.setConfig(config); return dk; } } |
1 | ValidateCodeController.java |
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 | @Controller public class ValidateCodeController { @Autowired private KaptchaConfig kaptchaConfig; /** * 创建验证码 * * @throws Exception */ @GetMapping ( "/validata/code/{client_id}" ) public void createCode( @PathVariable String client_id, HttpServletResponse response) throws Exception { DefaultKaptcha kaptcha = kaptchaConfig.getDDefaultKaptcha(); Assert.notNull(client_id, "机器码不能为空" ); response.setHeader( "Cache-Control" , "no-store, no-cache" ); response.setContentType( "image/jpeg" ); //生成文字验证码 String text = kaptcha.createText(); //生成图片验证码 BufferedImage image = kaptcha.createImage(text); //validateCodeService.saveImageCode(client_id, text);//验证码保存方式根据个人想法实现 try ( ServletOutputStream out = response.getOutputStream() ) { ImageIO.write(image, "JPEG" , out); } } } |
MC❤涛
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)