[feat] 根据验证码类型生成验证码(新增数字)

pull/445/head
hsdllcw 2024-08-06 16:19:32 +08:00
parent 3c804db4f0
commit e700a7dca0
1 changed files with 128 additions and 118 deletions

View File

@ -1,118 +1,128 @@
package com.ruoyi.gateway.service.impl; package com.ruoyi.gateway.service.impl;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.FastByteArrayOutputStream; import org.springframework.util.FastByteArrayOutputStream;
import com.google.code.kaptcha.Producer; import com.google.code.kaptcha.Producer;
import com.ruoyi.common.core.constant.CacheConstants; import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.constant.Constants; import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.exception.CaptchaException; import com.ruoyi.common.core.exception.CaptchaException;
import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.sign.Base64; import com.ruoyi.common.core.utils.sign.Base64;
import com.ruoyi.common.core.utils.uuid.IdUtils; import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.core.web.domain.AjaxResult; import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.redis.service.RedisService; import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.gateway.config.properties.CaptchaProperties; import com.ruoyi.gateway.config.properties.CaptchaProperties;
import com.ruoyi.gateway.service.ValidateCodeService; import com.ruoyi.gateway.service.ValidateCodeService;
/** /**
* *
* *
* @author ruoyi * @author ruoyi
*/ */
@Service @Service
public class ValidateCodeServiceImpl implements ValidateCodeService public class ValidateCodeServiceImpl implements ValidateCodeService
{ {
@Resource(name = "captchaProducer") @Resource(name = "captchaProducer")
private Producer captchaProducer; private Producer captchaProducer;
@Resource(name = "captchaProducerMath") @Resource(name = "captchaProducerMath")
private Producer captchaProducerMath; private Producer captchaProducerMath;
@Autowired @Resource(name = "captchaProducerNumber")
private RedisService redisService; private Producer captchaProducerNumber;
@Autowired @Autowired
private CaptchaProperties captchaProperties; private RedisService redisService;
/** @Autowired
* private CaptchaProperties captchaProperties;
*/
@Override /**
public AjaxResult createCaptcha() throws IOException, CaptchaException *
{ */
AjaxResult ajax = AjaxResult.success(); @Override
boolean captchaEnabled = captchaProperties.getEnabled(); public AjaxResult createCaptcha() throws IOException, CaptchaException
ajax.put("captchaEnabled", captchaEnabled); {
if (!captchaEnabled) AjaxResult ajax = AjaxResult.success();
{ boolean captchaEnabled = captchaProperties.getEnabled();
return ajax; ajax.put("captchaEnabled", captchaEnabled);
} if (!captchaEnabled)
{
// 保存验证码信息 return ajax;
String uuid = IdUtils.simpleUUID(); }
String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
// 保存验证码信息
String capStr = null, code = null; String uuid = IdUtils.simpleUUID();
BufferedImage image = null; String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
String captchaType = captchaProperties.getType(); String capStr = null, code = null;
// 生成验证码 BufferedImage image = null;
if ("math".equals(captchaType))
{ String captchaType = captchaProperties.getType();
String capText = captchaProducerMath.createText(); // 生成验证码
capStr = capText.substring(0, capText.lastIndexOf("@")); switch (captchaType) {
code = capText.substring(capText.lastIndexOf("@") + 1); case "number": {
image = captchaProducerMath.createImage(capStr); capStr = code = captchaProducerNumber.createText();
} image = captchaProducerNumber.createImage(capStr);
else if ("char".equals(captchaType)) }
{ break;
capStr = code = captchaProducer.createText(); case "math": {
image = captchaProducer.createImage(capStr); String capText = captchaProducerMath.createText();
} capStr = capText.substring(0, capText.lastIndexOf("@"));
code = capText.substring(capText.lastIndexOf("@") + 1);
redisService.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES); image = captchaProducerMath.createImage(capStr);
// 转换流信息写出 }
FastByteArrayOutputStream os = new FastByteArrayOutputStream(); break;
try default: {
{ capStr = code = captchaProducer.createText();
ImageIO.write(image, "jpg", os); image = captchaProducer.createImage(capStr);
} }
catch (IOException e) break;
{ }
return AjaxResult.error(e.getMessage());
} redisService.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
// 转换流信息写出
ajax.put("uuid", uuid); FastByteArrayOutputStream os = new FastByteArrayOutputStream();
ajax.put("img", Base64.encode(os.toByteArray())); try
return ajax; {
} ImageIO.write(image, "jpg", os);
}
/** catch (IOException e)
* {
*/ return AjaxResult.error(e.getMessage());
@Override }
public void checkCaptcha(String code, String uuid) throws CaptchaException
{ ajax.put("uuid", uuid);
if (StringUtils.isEmpty(code)) ajax.put("img", Base64.encode(os.toByteArray()));
{ return ajax;
throw new CaptchaException("验证码不能为空"); }
}
String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, ""); /**
String captcha = redisService.getCacheObject(verifyKey); *
if (captcha == null) */
{ @Override
throw new CaptchaException("验证码已失效"); public void checkCaptcha(String code, String uuid) throws CaptchaException
} {
redisService.deleteObject(verifyKey); if (StringUtils.isEmpty(code))
if (!code.equalsIgnoreCase(captcha)) {
{ throw new CaptchaException("验证码不能为空");
throw new CaptchaException("验证码错误"); }
} String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, "");
} String captcha = redisService.getCacheObject(verifyKey);
} if (captcha == null)
{
throw new CaptchaException("验证码已失效");
}
redisService.deleteObject(verifyKey);
if (!code.equalsIgnoreCase(captcha))
{
throw new CaptchaException("验证码错误");
}
}
}