2018-02-11 14:46:59 +08:00
|
|
|
|
# EasyCaptcha
|
2018-07-27 15:52:40 +08:00
|
|
|
|
|
2018-07-27 16:18:18 +08:00
|
|
|
|
![JitPack](https://img.shields.io/jitpack/v/whvcse/EasyCaptcha.svg?style=flat-square)
|
|
|
|
|
![Hex.pm](https://img.shields.io/hexpm/l/plug.svg?style=flat-square)
|
|
|
|
|
|
|
|
|
|
|
2018-07-27 15:52:40 +08:00
|
|
|
|
## 1.简介
|
|
|
|
|
|
2018-07-28 00:23:15 +08:00
|
|
|
|
  Java图形验证码,支持gif验证码、中文验证码,可用于Java Web、JavaSE项目。
|
2018-07-27 15:52:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 2.效果展示
|
|
|
|
|
|
|
|
|
|
**gif效果:**
|
|
|
|
|
|
2018-07-28 10:26:10 +08:00
|
|
|
|
![验证码](https://ws1.sinaimg.cn/large/006a7GCKly1ftpd30vgc2g303m01c0st.jpg)
|
2018-07-27 15:52:40 +08:00
|
|
|
|
  
|
2018-07-28 10:26:10 +08:00
|
|
|
|
![验证码](https://ws1.sinaimg.cn/large/006a7GCKly1ftpd22ixh9g303m01c3yj.jpg)
|
2018-07-27 15:52:40 +08:00
|
|
|
|
  
|
2018-07-28 10:26:10 +08:00
|
|
|
|
![验证码](https://ws1.sinaimg.cn/large/006a7GCKly1ftpd2k0gjug303m01ct8t.jpg)
|
2018-07-27 15:52:40 +08:00
|
|
|
|
|
|
|
|
|
**png效果:**
|
|
|
|
|
|
2018-07-28 10:26:10 +08:00
|
|
|
|
![验证码](https://ws1.sinaimg.cn/large/006a7GCKly1ftpd09lqpaj303m01cwee.jpg)
|
2018-07-27 15:52:40 +08:00
|
|
|
|
  
|
2018-07-28 10:26:10 +08:00
|
|
|
|
![验证码](https://ws1.sinaimg.cn/large/006a7GCKly1ftpd0tj6nhj303m01ct8m.jpg)
|
2018-07-27 15:52:40 +08:00
|
|
|
|
  
|
2018-07-28 10:26:10 +08:00
|
|
|
|
![验证码](https://ws1.sinaimg.cn/large/006a7GCKly1ftpd1cw3pcj303m01c0sn.jpg)
|
2018-07-27 15:52:40 +08:00
|
|
|
|
|
|
|
|
|
|
2018-07-28 00:22:49 +08:00
|
|
|
|
**中文验证码:**
|
|
|
|
|
|
|
|
|
|
![验证码](https://ws1.sinaimg.cn/large/006a7GCKly1ftovnvbgx6g303m01cjrc.jpg)
|
|
|
|
|
  
|
|
|
|
|
![验证码](https://ws1.sinaimg.cn/large/006a7GCKly1ftovojp2gng303m01cq2w.jpg)
|
|
|
|
|
  
|
|
|
|
|
![验证码](https://ws1.sinaimg.cn/large/006a7GCKly1ftovp5u4c4g303m01cdft.jpg)
|
|
|
|
|
|
|
|
|
|
![验证码](https://ws1.sinaimg.cn/large/006a7GCKly1ftovmfxg71j303m01cq2s.jpg)
|
|
|
|
|
  
|
|
|
|
|
![验证码](https://ws1.sinaimg.cn/large/006a7GCKly1ftovmy720dj303m01cq2s.jpg)
|
|
|
|
|
  
|
|
|
|
|
![验证码](https://ws1.sinaimg.cn/large/006a7GCKly1ftovn68um6j303m01cglf.jpg)
|
|
|
|
|
|
|
|
|
|
|
2018-07-27 15:52:40 +08:00
|
|
|
|
|
|
|
|
|
## 3.导入项目
|
|
|
|
|
|
|
|
|
|
### 2.1.gradle方式的引入
|
2018-05-14 11:39:09 +08:00
|
|
|
|
需要先在project的build.gradle中添加:
|
2018-07-27 15:52:40 +08:00
|
|
|
|
```text
|
2018-05-14 11:39:09 +08:00
|
|
|
|
allprojects {
|
|
|
|
|
repositories {
|
|
|
|
|
maven { url 'https://jitpack.io' }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
在项目的build.gradle中添加
|
2018-07-27 15:52:40 +08:00
|
|
|
|
```text
|
2018-05-14 11:39:09 +08:00
|
|
|
|
dependencies {
|
2018-07-28 10:26:10 +08:00
|
|
|
|
compile 'com.github.whvcse:EasyCaptcha:1.3.0'
|
2018-05-14 11:39:09 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-07-27 15:52:40 +08:00
|
|
|
|
### 2.2.maven方式引入
|
|
|
|
|
在你的pom.xml中添加如下代码:
|
2018-05-14 11:39:09 +08:00
|
|
|
|
```xml
|
2018-07-27 15:52:40 +08:00
|
|
|
|
<project>
|
|
|
|
|
<repositories>
|
|
|
|
|
<repository>
|
|
|
|
|
<id>jitpack.io</id>
|
|
|
|
|
<url>https://jitpack.io</url>
|
|
|
|
|
</repository>
|
|
|
|
|
</repositories>
|
|
|
|
|
|
|
|
|
|
<dependencies>
|
|
|
|
|
<dependency>
|
|
|
|
|
<groupId>com.github.whvcse</groupId>
|
|
|
|
|
<artifactId>EasyCaptcha</artifactId>
|
2018-07-28 10:26:10 +08:00
|
|
|
|
<version>1.3.0</version>
|
2018-07-27 15:52:40 +08:00
|
|
|
|
</dependency>
|
|
|
|
|
</dependencies>
|
|
|
|
|
</project>
|
|
|
|
|
|
2018-05-14 11:39:09 +08:00
|
|
|
|
```
|
|
|
|
|
|
2018-07-27 15:52:40 +08:00
|
|
|
|
### 2.3.jar包下载
|
2018-07-28 10:39:53 +08:00
|
|
|
|
[EasyCaptcha-1.3.0.jar](https://gitee.com/whvse/EasyCaptcha/releases)
|
2018-07-27 15:52:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
---
|
2018-05-14 11:39:09 +08:00
|
|
|
|
|
2018-07-27 15:52:40 +08:00
|
|
|
|
|
|
|
|
|
## 3.使用方法
|
|
|
|
|
|
|
|
|
|
### 3.1.快速使用
|
2018-05-14 11:39:09 +08:00
|
|
|
|
1.在web.xml里面加入如下配置:
|
|
|
|
|
```xml
|
2018-07-27 15:52:40 +08:00
|
|
|
|
<web-app>
|
|
|
|
|
<!-- 图形验证码servlet -->
|
|
|
|
|
<servlet>
|
|
|
|
|
<servlet-name>CaptchaServlet</servlet-name>
|
|
|
|
|
<servlet-class>com.wf.captcha.servlet.CaptchaServlet</servlet-class>
|
|
|
|
|
</servlet>
|
|
|
|
|
<servlet-mapping>
|
|
|
|
|
<servlet-name>CaptchaServlet</servlet-name>
|
|
|
|
|
<url-pattern>/images/captcha</url-pattern>
|
|
|
|
|
</servlet-mapping>
|
|
|
|
|
</web-app>
|
|
|
|
|
|
2018-05-14 11:39:09 +08:00
|
|
|
|
```
|
|
|
|
|
2.前端代码
|
|
|
|
|
```html
|
2018-05-15 08:32:50 +08:00
|
|
|
|
<img src="/images/captcha" />
|
2018-05-14 11:39:09 +08:00
|
|
|
|
```
|
|
|
|
|
|
2018-07-27 15:52:40 +08:00
|
|
|
|
### 3.2.在SpringMVC中使用
|
|
|
|
|
也可以使用controller的形式输出验证码,方法如下:
|
2018-05-14 11:39:09 +08:00
|
|
|
|
```java
|
2018-07-27 15:52:40 +08:00
|
|
|
|
@Controller
|
|
|
|
|
public class MainController {
|
|
|
|
|
|
|
|
|
|
@RequestMapping("/images/captcha")
|
|
|
|
|
public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
|
|
|
|
CaptchaUtil.out(request, response);
|
2018-05-14 11:39:09 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
前端代码:
|
|
|
|
|
```html
|
2018-05-15 08:32:50 +08:00
|
|
|
|
<img src="/images/captcha" />
|
2018-05-14 11:39:09 +08:00
|
|
|
|
```
|
|
|
|
|
|
2018-07-27 15:52:40 +08:00
|
|
|
|
### 3.3.判断验证码是否正确
|
2018-05-14 11:39:09 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2018-07-27 15:52:40 +08:00
|
|
|
|
@Controller
|
|
|
|
|
public class LoginController {
|
|
|
|
|
|
|
|
|
|
@PostMapping("/login")
|
|
|
|
|
public JsonResult login(String username,String password,String code){
|
|
|
|
|
|
2018-07-30 13:10:51 +08:00
|
|
|
|
if (!CaptchaUtil.ver(code, request)) {
|
2018-07-28 00:22:49 +08:00
|
|
|
|
CaptchaUtil.clear(request);
|
2018-07-27 15:52:40 +08:00
|
|
|
|
return JsonResult.error("验证码不正确");
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-14 11:39:09 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
2018-07-27 15:52:40 +08:00
|
|
|
|
|
|
|
|
|
### 3.4.设置宽高和位数
|
2018-05-14 11:39:09 +08:00
|
|
|
|
```java
|
2018-07-27 15:52:40 +08:00
|
|
|
|
@Controller
|
|
|
|
|
public class MainController {
|
|
|
|
|
|
|
|
|
|
@RequestMapping("/images/captcha")
|
|
|
|
|
public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
|
|
|
|
// 设置位数
|
|
|
|
|
CaptchaUtil.out(5, request, response);
|
|
|
|
|
|
|
|
|
|
// 设置宽、高、位数
|
|
|
|
|
CaptchaUtil.out(130, 48, 5, request, response);
|
|
|
|
|
}
|
2018-05-14 11:39:09 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-07-28 00:22:49 +08:00
|
|
|
|
### 3.5.不使用工具类
|
|
|
|
|
|
|
|
|
|
  CaptchaUtil是为了简化操作,封装了生成验证码、存session、判断验证码等功能。CaptchaUtil使用的GifCaptcha
|
|
|
|
|
生成的字母数字混合的gif验证码,如果需要设置更多的参数,请参照如下操作使用:
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
@Controller
|
|
|
|
|
public class MainController {
|
|
|
|
|
|
|
|
|
|
@RequestMapping("/images/captcha")
|
|
|
|
|
public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
|
|
|
|
// 设置请求头为输出图片类型
|
|
|
|
|
CaptchaUtil.setHeader(response);
|
|
|
|
|
|
|
|
|
|
// 三个参数分别为宽、高、位数
|
|
|
|
|
GifCaptcha gifCaptcha = new GifCaptcha(130, 48, 5);
|
|
|
|
|
|
|
|
|
|
// 设置字体
|
|
|
|
|
gifCaptcha.setFont(new Font("Verdana", Font.PLAIN, 32)); // 有默认字体,可以不用设置
|
|
|
|
|
|
|
|
|
|
// 设置类型,纯数字、纯字母、字母数字混合
|
|
|
|
|
gifCaptcha.setCharType(Captcha.TYPE_ONLY_NUMBER);
|
|
|
|
|
|
|
|
|
|
// 验证码存入session
|
|
|
|
|
request.getSession().setAttribute("captcha", gifCaptcha.text().toLowerCase());
|
|
|
|
|
|
|
|
|
|
// 输出图片流
|
|
|
|
|
gifCaptcha.out(response.getOutputStream());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("/login")
|
|
|
|
|
public JsonResult login(String username,String password,String code){
|
|
|
|
|
// 获取session中的验证码
|
|
|
|
|
String sessionCode = request.getSession().getAttribute("captcha");
|
|
|
|
|
// 判断验证码
|
|
|
|
|
if (code==null || !sessionCode.equals(code.trim().toLowerCase())) {
|
|
|
|
|
return JsonResult.error("验证码不正确");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-07-27 15:52:40 +08:00
|
|
|
|
## 4.更多设置
|
2018-05-14 11:39:09 +08:00
|
|
|
|
|
2018-07-27 16:18:18 +08:00
|
|
|
|
### 4.1.使用Gif验证码
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public class Test {
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
OutputStream outputStream = new FileOutputStream(new File("D:/a/aa.gif"));
|
|
|
|
|
|
|
|
|
|
// 三个参数分别为宽、高、位数
|
|
|
|
|
GifCaptcha gifCaptcha = new GifCaptcha(130, 48, 5);
|
|
|
|
|
|
|
|
|
|
// 设置字体
|
|
|
|
|
gifCaptcha.setFont(new Font("Verdana", Font.PLAIN, 32)); // 有默认字体,可以不用设置
|
|
|
|
|
|
2018-07-27 17:08:16 +08:00
|
|
|
|
// 设置类型,纯数字、纯字母、字母数字混合
|
|
|
|
|
gifCaptcha.setCharType(Captcha.TYPE_ONLY_NUMBER);
|
|
|
|
|
|
2018-07-27 16:18:18 +08:00
|
|
|
|
// 生成的验证码
|
|
|
|
|
String code = gifCaptcha.text();
|
|
|
|
|
|
|
|
|
|
// 输出图片流
|
|
|
|
|
gifCaptcha.out(outputStream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 4.2.使用png验证码
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public class Test {
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
2018-07-27 17:20:37 +08:00
|
|
|
|
OutputStream outputStream = new FileOutputStream(new File("D:/a/aa.png"));
|
2018-07-27 16:18:18 +08:00
|
|
|
|
|
|
|
|
|
// 三个参数分别为宽、高、位数
|
|
|
|
|
SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
|
|
|
|
|
|
|
|
|
|
// 设置字体
|
|
|
|
|
specCaptcha.setFont(new Font("Verdana", Font.PLAIN, 32)); // 有默认字体,可以不用设置
|
|
|
|
|
|
2018-07-27 17:08:16 +08:00
|
|
|
|
// 设置类型,纯数字、纯字母、字母数字混合
|
|
|
|
|
specCaptcha.setCharType(Captcha.TYPE_ONLY_NUMBER);
|
|
|
|
|
|
2018-07-27 16:18:18 +08:00
|
|
|
|
// 生成的验证码
|
|
|
|
|
String code = specCaptcha.text();
|
|
|
|
|
|
|
|
|
|
// 输出图片流
|
|
|
|
|
specCaptcha.out(outputStream);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-27 17:08:16 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 4.3.验证码类型
|
|
|
|
|
|
|
|
|
|
类型 | 描述
|
|
|
|
|
:--- | :---
|
|
|
|
|
TYPE_DEFAULT | 数字和字母混合
|
|
|
|
|
TYPE_ONLY_NUMBER | 纯数字
|
|
|
|
|
TYPE_ONLY_CHAR | 纯字母
|
|
|
|
|
|
|
|
|
|
|
2018-07-28 00:22:49 +08:00
|
|
|
|
### 4.4.中文验证码
|
|
|
|
|
|
|
|
|
|
中文png验证码:
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public class Test {
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
OutputStream outputStream = new FileOutputStream(new File("D:/a/aa.png"));
|
|
|
|
|
|
|
|
|
|
// 三个参数分别为宽、高、位数
|
|
|
|
|
ChineseCaptcha chineseCaptcha = new ChineseCaptcha(130, 48, 4);
|
|
|
|
|
|
|
|
|
|
// 设置字体
|
|
|
|
|
chineseCaptcha.setFont(new Font("楷体", Font.PLAIN, 28)); // 有默认字体,可以不用设置
|
|
|
|
|
|
|
|
|
|
// 生成的验证码
|
|
|
|
|
String code = chineseCaptcha.text();
|
|
|
|
|
|
|
|
|
|
// 输出图片流
|
|
|
|
|
chineseCaptcha.out(outputStream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
中文gif验证码:
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public class Test {
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
OutputStream outputStream = new FileOutputStream(new File("D:/a/aa.png"));
|
|
|
|
|
|
|
|
|
|
// 三个参数分别为宽、高、位数
|
|
|
|
|
ChineseGifCaptcha chineseGifCaptcha = new ChineseGifCaptcha(130, 48, 4);
|
|
|
|
|
|
|
|
|
|
// 设置字体
|
|
|
|
|
chineseGifCaptcha.setFont(new Font("楷体", Font.PLAIN, 28)); // 有默认字体,可以不用设置
|
|
|
|
|
|
|
|
|
|
// 生成的验证码
|
|
|
|
|
String code = chineseGifCaptcha.text();
|
|
|
|
|
|
|
|
|
|
// 输出图片流
|
|
|
|
|
chineseGifCaptcha.out(outputStream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 4.5.前后端分离项目的使用
|
|
|
|
|
|
|
|
|
|
  分离项目建议不要存储在session中,存储在redis中。
|
|
|
|
|
|
2018-07-28 10:26:10 +08:00
|
|
|
|
|
|
|
|
|
## 5.自定义效果
|
|
|
|
|
|
|
|
|
|
参考源代码中的SpecCaptcha和GifCaptcha继承Captcha即可。
|