设为首页 加入收藏

TOP

Springboot通过谷歌Kaptcha 组件,生成图形验证码(一)
2023-07-25 21:25:12 】 浏览:69
Tags:Springboot 通过谷 Kaptcha 组件 成图形

图形验证码属于老生常谈了,具体细节这里就不说了。生成图形验证码的办法非常多,今天讲解一种通过Kaptcha组件快速生成图形验证码的方法。
Kaptcha是谷歌开源的一款简单实用的图形验证码组件。我个人推荐它的最大原因是容易上手,采用约定大于配置的方式,快速契合到项目中。
话不多说,我们看看该如何使用它:
一、首先我们在springboot中引入以下maven组件:

1 <dependency>
2     <groupId>com.google.code.kaptcha</groupId>
3     <artifactId>kaptcha</artifactId>
4     <version>2.3</version>
5 </dependency>

如果上述组件你一直无法拉取下来的话,也可以用如下配置:

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

二、接着我们在springboot项目中加入对应的config配置类,(防盗连接:本文首发自http://www.cnblogs.com/jilodream/ )这一步也可以配合配置中心来完成。它的作用是自动生成我们所需的config bean。
其中的配置项我们都可以选填,这里是只是一个参考,具体内容可见下文表

 1 @Component
 2 public class KaptchaConfig {
 3 
 4     @Bean
 5     public DefaultKaptcha getDefaultKaptcha(){
 6         com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();
 7         Properties properties = new Properties();
 8         properties.put("kaptcha.border", "no");
 9         properties.put("kaptcha.textproducer.font.color", "red");
10         properties.put("kaptcha.image.width", "213");
11         properties.put("kaptcha.image.height", "88");
12         properties.put("kaptcha.textproducer.font.size", "45");
13         properties.put("kaptcha.session.key", "verifyCode");
14         properties.put("kaptcha.textproducer.char.space", "6");
15         properties.put("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.WaterRipple");
16        // properties.put("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
17         properties.put("kaptcha.background.clear.from", "yellow");
18         properties.put("kaptcha.background.clear.to", "green");
19         Config config = new Config(properties);
20         defaultKaptcha.setConfig(config);
21 
22         return defaultKaptcha;
23     }
24 }

配置表

配置名 配置作用 默认值
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.backg
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Java 网络编程 —— 非阻塞式编程 下一篇不是单例的单例——巧用ClassLoad..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目