增加中文验证码功能
This commit is contained in:
110
README.md
110
README.md
@@ -28,6 +28,21 @@
|
||||

|
||||
|
||||
|
||||
**中文验证码:**
|
||||
|
||||

|
||||
  
|
||||

|
||||
  
|
||||

|
||||
|
||||

|
||||
  
|
||||

|
||||
  
|
||||

|
||||
|
||||
|
||||
|
||||
## 3.导入项目
|
||||
|
||||
@@ -126,6 +141,7 @@ public class LoginController {
|
||||
public JsonResult login(String username,String password,String code){
|
||||
|
||||
if (CaptchaUtil.ver(code, request)) {
|
||||
CaptchaUtil.clear(request);
|
||||
return JsonResult.error("验证码不正确");
|
||||
}
|
||||
}
|
||||
@@ -148,6 +164,48 @@ public class MainController {
|
||||
}
|
||||
```
|
||||
|
||||
### 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("验证码不正确");
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 4.更多设置
|
||||
|
||||
### 4.1.使用Gif验证码
|
||||
@@ -211,3 +269,55 @@ public class Test {
|
||||
TYPE_ONLY_CHAR | 纯字母
|
||||
|
||||
|
||||
### 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中。
|
||||
|
||||
|
Reference in New Issue
Block a user