|
|
|
@ -2,10 +2,13 @@ package com.ruoyi.gateway.service.impl;
|
|
|
|
|
|
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.util.FastByteArrayOutputStream;
|
|
|
|
|
import com.google.code.kaptcha.Producer;
|
|
|
|
@ -24,8 +27,7 @@ import com.ruoyi.gateway.service.ValidateCodeService;
|
|
|
|
|
* @author ruoyi
|
|
|
|
|
*/
|
|
|
|
|
@Service
|
|
|
|
|
public class ValidateCodeServiceImpl implements ValidateCodeService
|
|
|
|
|
{
|
|
|
|
|
public class ValidateCodeServiceImpl implements ValidateCodeService {
|
|
|
|
|
@Resource(name = "captchaProducer")
|
|
|
|
|
private Producer captchaProducer;
|
|
|
|
|
|
|
|
|
@ -36,14 +38,14 @@ public class ValidateCodeServiceImpl implements ValidateCodeService
|
|
|
|
|
private RedisService redisService;
|
|
|
|
|
|
|
|
|
|
// 验证码类型
|
|
|
|
|
private String captchaType = "math";
|
|
|
|
|
private String captchaType = "char";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成验证码
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public AjaxResult createCapcha() throws IOException, CaptchaException
|
|
|
|
|
{
|
|
|
|
|
public AjaxResult createCapcha() throws IOException, CaptchaException {
|
|
|
|
|
|
|
|
|
|
// 保存验证码信息
|
|
|
|
|
String uuid = IdUtils.simpleUUID();
|
|
|
|
|
String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
|
|
|
|
@ -52,15 +54,12 @@ public class ValidateCodeServiceImpl implements ValidateCodeService
|
|
|
|
|
BufferedImage image = null;
|
|
|
|
|
|
|
|
|
|
// 生成验证码
|
|
|
|
|
if ("math".equals(captchaType))
|
|
|
|
|
{
|
|
|
|
|
if ("math".equals(captchaType)) {
|
|
|
|
|
String capText = captchaProducerMath.createText();
|
|
|
|
|
capStr = capText.substring(0, capText.lastIndexOf("@"));
|
|
|
|
|
code = capText.substring(capText.lastIndexOf("@") + 1);
|
|
|
|
|
image = captchaProducerMath.createImage(capStr);
|
|
|
|
|
}
|
|
|
|
|
else if ("char".equals(captchaType))
|
|
|
|
|
{
|
|
|
|
|
} else if ("char".equals(captchaType)) {
|
|
|
|
|
capStr = code = captchaProducer.createText();
|
|
|
|
|
image = captchaProducer.createImage(capStr);
|
|
|
|
|
}
|
|
|
|
@ -68,12 +67,9 @@ public class ValidateCodeServiceImpl implements ValidateCodeService
|
|
|
|
|
redisService.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
|
|
|
|
|
// 转换流信息写出
|
|
|
|
|
FastByteArrayOutputStream os = new FastByteArrayOutputStream();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
ImageIO.write(image, "jpg", os);
|
|
|
|
|
}
|
|
|
|
|
catch (IOException e)
|
|
|
|
|
{
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
return AjaxResult.error(e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -87,22 +83,18 @@ public class ValidateCodeServiceImpl implements ValidateCodeService
|
|
|
|
|
* 校验验证码
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void checkCapcha(String code, String uuid) throws CaptchaException
|
|
|
|
|
{
|
|
|
|
|
if (StringUtils.isEmpty(code))
|
|
|
|
|
{
|
|
|
|
|
public void checkCapcha(String code, String uuid) throws CaptchaException {
|
|
|
|
|
if (StringUtils.isEmpty(code)) {
|
|
|
|
|
throw new CaptchaException("验证码不能为空");
|
|
|
|
|
}
|
|
|
|
|
if (StringUtils.isEmpty(uuid))
|
|
|
|
|
{
|
|
|
|
|
if (StringUtils.isEmpty(uuid)) {
|
|
|
|
|
throw new CaptchaException("验证码已失效");
|
|
|
|
|
}
|
|
|
|
|
String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
|
|
|
|
|
String captcha = redisService.getCacheObject(verifyKey);
|
|
|
|
|
redisService.deleteObject(verifyKey);
|
|
|
|
|
|
|
|
|
|
if (!code.equalsIgnoreCase(captcha))
|
|
|
|
|
{
|
|
|
|
|
if (!code.equalsIgnoreCase(captcha)) {
|
|
|
|
|
throw new CaptchaException("验证码错误");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|