提交 02c56a12 编写于 作者: zlt2000's avatar zlt2000

优化图像验证码,改为gif动图

上级 d0d7deaf
......@@ -26,7 +26,7 @@
<security-oauth2.version>2.3.5.RELEASE</security-oauth2.version>
<security-jwt.version>1.0.9.RELEASE</security-jwt.version>
<redisson.version>3.9.1</redisson.version>
<kaptcha.version>0.0.9</kaptcha.version>
<easyCaptcha.version>1.5.0</easyCaptcha.version>
<hutool.version>4.6.1</hutool.version>
<mybatis-plus-boot-starter.version>3.1.2</mybatis-plus-boot-starter.version>
<aliyun-sdk-oss>3.4.2</aliyun-sdk-oss>
......@@ -202,11 +202,11 @@
<artifactId>swagger-butler-core</artifactId>
<version>${swagger.butler.version}</version>
</dependency>
<!-- kaptcha 验证码生成工具 -->
<!-- 验证码生成工具 -->
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>kaptcha</artifactId>
<version>${kaptcha.version}</version>
<groupId>com.github.whvcse</groupId>
<artifactId>EasyCaptcha</artifactId>
<version>${easyCaptcha.version}</version>
</dependency>
<!-- hutool java工具类库 -->
<dependency>
......@@ -359,6 +359,10 @@
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<distributionManagement>
<repository>
......
......@@ -50,10 +50,11 @@
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<!-- kaptcha 验证码生成工具 -->
<!-- 验证码生成工具 -->
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>kaptcha</artifactId>
<groupId>com.github.whvcse</groupId>
<artifactId>EasyCaptcha</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
......
......@@ -3,7 +3,9 @@ package com.central.oauth.controller;
import com.central.common.constant.SecurityConstants;
import com.central.common.model.Result;
import com.central.oauth.service.IValidateCodeService;
import com.google.code.kaptcha.Producer;
import com.wf.captcha.Captcha;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
......@@ -11,10 +13,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
/**
* 验证码提供
......@@ -23,9 +22,6 @@ import java.awt.image.BufferedImage;
*/
@Controller
public class ValidateCodeController {
@Autowired
private Producer producer;
@Autowired
private IValidateCodeService validateCodeService;
......@@ -37,18 +33,16 @@ public class ValidateCodeController {
@GetMapping(SecurityConstants.DEFAULT_VALIDATE_CODE_URL_PREFIX + "/{deviceId}")
public void createCode(@PathVariable String deviceId, HttpServletResponse response) throws Exception {
Assert.notNull(deviceId, "机器码不能为空");
response.setHeader("Cache-Control", "no-store, no-cache");
response.setContentType("image/jpeg");
//生成文字验证码
String text = producer.createText();
//生成图片验证码
BufferedImage image = producer.createImage(text);
validateCodeService.saveImageCode(deviceId, text);
try (
ServletOutputStream out = response.getOutputStream()
) {
ImageIO.write(image, "JPEG", out);
}
// 设置请求头为输出图片类型
CaptchaUtil.setHeader(response);
// 三个参数分别为宽、高、位数
GifCaptcha gifCaptcha = new GifCaptcha(100, 35, 4);
// 设置类型:字母数字混合
gifCaptcha.setCharType(Captcha.TYPE_DEFAULT);
// 保存验证码
validateCodeService.saveImageCode(deviceId, gifCaptcha.text().toLowerCase());
// 输出图片流
gifCaptcha.out(response.getOutputStream());
}
/**
......
......@@ -115,7 +115,7 @@ public class ValidateCodeServiceImpl implements IValidateCodeService {
throw new ValidateCodeException("验证码不存在或已过期");
}
if (!StringUtils.equals(code, codeInRequest)) {
if (!StringUtils.equals(code, codeInRequest.toLowerCase())) {
throw new ValidateCodeException("验证码不正确");
}
......
......@@ -98,7 +98,7 @@
});
} else {
layer.closeAll('loading');
layer.msg('登录失败,请重试', {icon: 5, time: 500});
layer.msg(data.resp_msg, {icon: 5, time: 500});
}
},
error: function (xhr) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册