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

This commit is contained in:
zlt
2019-08-09 16:01:27 +08:00
parent d0d7deaf7a
commit 02c56a120d
5 changed files with 28 additions and 29 deletions
+9 -5
View File
@@ -26,7 +26,7 @@
<security-oauth2.version>2.3.5.RELEASE</security-oauth2.version> <security-oauth2.version>2.3.5.RELEASE</security-oauth2.version>
<security-jwt.version>1.0.9.RELEASE</security-jwt.version> <security-jwt.version>1.0.9.RELEASE</security-jwt.version>
<redisson.version>3.9.1</redisson.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> <hutool.version>4.6.1</hutool.version>
<mybatis-plus-boot-starter.version>3.1.2</mybatis-plus-boot-starter.version> <mybatis-plus-boot-starter.version>3.1.2</mybatis-plus-boot-starter.version>
<aliyun-sdk-oss>3.4.2</aliyun-sdk-oss> <aliyun-sdk-oss>3.4.2</aliyun-sdk-oss>
@@ -202,11 +202,11 @@
<artifactId>swagger-butler-core</artifactId> <artifactId>swagger-butler-core</artifactId>
<version>${swagger.butler.version}</version> <version>${swagger.butler.version}</version>
</dependency> </dependency>
<!-- kaptcha 验证码生成工具 --> <!-- 验证码生成工具 -->
<dependency> <dependency>
<groupId>com.github.axet</groupId> <groupId>com.github.whvcse</groupId>
<artifactId>kaptcha</artifactId> <artifactId>EasyCaptcha</artifactId>
<version>${kaptcha.version}</version> <version>${easyCaptcha.version}</version>
</dependency> </dependency>
<!-- hutool java工具类库 --> <!-- hutool java工具类库 -->
<dependency> <dependency>
@@ -359,6 +359,10 @@
<enabled>false</enabled> <enabled>false</enabled>
</snapshots> </snapshots>
</repository> </repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories> </repositories>
<distributionManagement> <distributionManagement>
<repository> <repository>
+4 -3
View File
@@ -50,10 +50,11 @@
<groupId>commons-io</groupId> <groupId>commons-io</groupId>
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>
</dependency> </dependency>
<!-- kaptcha 验证码生成工具 --> <!-- 验证码生成工具 -->
<dependency> <dependency>
<groupId>com.github.axet</groupId> <groupId>com.github.whvcse</groupId>
<artifactId>kaptcha</artifactId> <artifactId>EasyCaptcha</artifactId>
<version>1.5.0</version>
</dependency> </dependency>
<dependency> <dependency>
@@ -3,7 +3,9 @@ package com.central.oauth.controller;
import com.central.common.constant.SecurityConstants; import com.central.common.constant.SecurityConstants;
import com.central.common.model.Result; import com.central.common.model.Result;
import com.central.oauth.service.IValidateCodeService; 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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.util.Assert; 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.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
/** /**
* 验证码提供 * 验证码提供
@@ -23,9 +22,6 @@ import java.awt.image.BufferedImage;
*/ */
@Controller @Controller
public class ValidateCodeController { public class ValidateCodeController {
@Autowired
private Producer producer;
@Autowired @Autowired
private IValidateCodeService validateCodeService; private IValidateCodeService validateCodeService;
@@ -37,18 +33,16 @@ public class ValidateCodeController {
@GetMapping(SecurityConstants.DEFAULT_VALIDATE_CODE_URL_PREFIX + "/{deviceId}") @GetMapping(SecurityConstants.DEFAULT_VALIDATE_CODE_URL_PREFIX + "/{deviceId}")
public void createCode(@PathVariable String deviceId, HttpServletResponse response) throws Exception { public void createCode(@PathVariable String deviceId, HttpServletResponse response) throws Exception {
Assert.notNull(deviceId, "机器码不能为空"); Assert.notNull(deviceId, "机器码不能为空");
response.setHeader("Cache-Control", "no-store, no-cache"); // 设置请求头为输出图片类型
response.setContentType("image/jpeg"); CaptchaUtil.setHeader(response);
//生成文字验证码 // 三个参数分别为宽、高、位数
String text = producer.createText(); GifCaptcha gifCaptcha = new GifCaptcha(100, 35, 4);
//生成图片验证码 // 设置类型:字母数字混合
BufferedImage image = producer.createImage(text); gifCaptcha.setCharType(Captcha.TYPE_DEFAULT);
validateCodeService.saveImageCode(deviceId, text); // 保存验证码
try ( validateCodeService.saveImageCode(deviceId, gifCaptcha.text().toLowerCase());
ServletOutputStream out = response.getOutputStream() // 输出图片流
) { gifCaptcha.out(response.getOutputStream());
ImageIO.write(image, "JPEG", out);
}
} }
/** /**
@@ -115,7 +115,7 @@ public class ValidateCodeServiceImpl implements IValidateCodeService {
throw new ValidateCodeException("验证码不存在或已过期"); throw new ValidateCodeException("验证码不存在或已过期");
} }
if (!StringUtils.equals(code, codeInRequest)) { if (!StringUtils.equals(code, codeInRequest.toLowerCase())) {
throw new ValidateCodeException("验证码不正确"); throw new ValidateCodeException("验证码不正确");
} }
@@ -98,7 +98,7 @@
}); });
} else { } else {
layer.closeAll('loading'); layer.closeAll('loading');
layer.msg('登录失败,请重试', {icon: 5, time: 500}); layer.msg(data.resp_msg, {icon: 5, time: 500});
} }
}, },
error: function (xhr) { error: function (xhr) {