验证码类型支持(数组计算、字符验证)
parent
4126f634ba
commit
41cf67da6d
|
@ -5,6 +5,7 @@ import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import com.google.code.kaptcha.impl.DefaultKaptcha;
|
import com.google.code.kaptcha.impl.DefaultKaptcha;
|
||||||
import com.google.code.kaptcha.util.Config;
|
import com.google.code.kaptcha.util.Config;
|
||||||
|
import static com.google.code.kaptcha.Constants.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 验证码配置
|
* 验证码配置
|
||||||
|
@ -14,42 +15,67 @@ import com.google.code.kaptcha.util.Config;
|
||||||
@Configuration
|
@Configuration
|
||||||
public class CaptchaConfig
|
public class CaptchaConfig
|
||||||
{
|
{
|
||||||
|
@Bean(name = "captchaProducer")
|
||||||
|
public DefaultKaptcha getKaptchaBean()
|
||||||
|
{
|
||||||
|
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
|
||||||
|
Properties properties = new Properties();
|
||||||
|
// 是否有边框 默认为true 我们可以自己设置yes,no
|
||||||
|
properties.setProperty(KAPTCHA_BORDER, "yes");
|
||||||
|
// 验证码文本字符颜色 默认为Color.BLACK
|
||||||
|
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "black");
|
||||||
|
// 验证码图片宽度 默认为200
|
||||||
|
properties.setProperty(KAPTCHA_IMAGE_WIDTH, "160");
|
||||||
|
// 验证码图片高度 默认为50
|
||||||
|
properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "60");
|
||||||
|
// 验证码文本字符大小 默认为40
|
||||||
|
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "38");
|
||||||
|
// KAPTCHA_SESSION_KEY
|
||||||
|
properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCode");
|
||||||
|
// 验证码文本字符长度 默认为5
|
||||||
|
properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
|
||||||
|
// 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
|
||||||
|
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");
|
||||||
|
// 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy
|
||||||
|
properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy");
|
||||||
|
Config config = new Config(properties);
|
||||||
|
defaultKaptcha.setConfig(config);
|
||||||
|
return defaultKaptcha;
|
||||||
|
}
|
||||||
|
|
||||||
@Bean(name = "captchaProducerMath")
|
@Bean(name = "captchaProducerMath")
|
||||||
public DefaultKaptcha getKaptchaBeanMath()
|
public DefaultKaptcha getKaptchaBeanMath()
|
||||||
{
|
{
|
||||||
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
|
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
// 是否有边框 默认为true 我们可以自己设置yes,no
|
// 是否有边框 默认为true 我们可以自己设置yes,no
|
||||||
properties.setProperty("kaptcha.border", "yes");
|
properties.setProperty(KAPTCHA_BORDER, "yes");
|
||||||
// 边框颜色 默认为Color.BLACK
|
// 边框颜色 默认为Color.BLACK
|
||||||
properties.setProperty("kaptcha.border.color", "105,179,90");
|
properties.setProperty(KAPTCHA_BORDER_COLOR, "105,179,90");
|
||||||
// 验证码文本字符颜色 默认为Color.BLACK
|
// 验证码文本字符颜色 默认为Color.BLACK
|
||||||
properties.setProperty("kaptcha.textproducer.font.color", "blue");
|
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "blue");
|
||||||
// 验证码图片宽度 默认为200
|
// 验证码图片宽度 默认为200
|
||||||
properties.setProperty("kaptcha.image.width", "160");
|
properties.setProperty(KAPTCHA_IMAGE_WIDTH, "160");
|
||||||
// 验证码图片高度 默认为50
|
// 验证码图片高度 默认为50
|
||||||
properties.setProperty("kaptcha.image.height", "60");
|
properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "60");
|
||||||
// 验证码文本字符大小 默认为40
|
// 验证码文本字符大小 默认为40
|
||||||
properties.setProperty("kaptcha.textproducer.font.size", "35");
|
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "35");
|
||||||
// KAPTCHA_SESSION_KEY
|
// KAPTCHA_SESSION_KEY
|
||||||
properties.setProperty("kaptcha.session.key", "kaptchaCodeMath");
|
properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCodeMath");
|
||||||
// 验证码文本生成器
|
// 验证码文本生成器
|
||||||
properties.setProperty("kaptcha.textproducer.impl", "com.ruoyi.gateway.config.KaptchaTextCreator");
|
properties.setProperty(KAPTCHA_TEXTPRODUCER_IMPL, "com.ruoyi.gateway.config.KaptchaTextCreator");
|
||||||
// 验证码文本字符间距 默认为2
|
// 验证码文本字符间距 默认为2
|
||||||
properties.setProperty("kaptcha.textproducer.char.space", "3");
|
properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "3");
|
||||||
// 验证码文本字符长度 默认为5
|
// 验证码文本字符长度 默认为5
|
||||||
properties.setProperty("kaptcha.textproducer.char.length", "6");
|
properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "6");
|
||||||
// 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1,
|
// 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
|
||||||
// fontSize)
|
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");
|
||||||
properties.setProperty("kaptcha.textproducer.font.names", "Arial,Courier");
|
|
||||||
// 验证码噪点颜色 默认为Color.BLACK
|
// 验证码噪点颜色 默认为Color.BLACK
|
||||||
properties.setProperty("kaptcha.noise.color", "white");
|
properties.setProperty(KAPTCHA_NOISE_COLOR, "white");
|
||||||
// 干扰实现类
|
// 干扰实现类
|
||||||
properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");
|
properties.setProperty(KAPTCHA_NOISE_IMPL, "com.google.code.kaptcha.impl.NoNoise");
|
||||||
// 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple
|
// 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy
|
||||||
// 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy
|
properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy");
|
||||||
// 阴影com.google.code.kaptcha.impl.ShadowGimpy
|
|
||||||
properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.ShadowGimpy");
|
|
||||||
Config config = new Config(properties);
|
Config config = new Config(properties);
|
||||||
defaultKaptcha.setConfig(config);
|
defaultKaptcha.setConfig(config);
|
||||||
return defaultKaptcha;
|
return defaultKaptcha;
|
||||||
|
|
|
@ -3,6 +3,7 @@ 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.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;
|
||||||
|
@ -25,28 +26,46 @@ import com.ruoyi.gateway.service.ValidateCodeService;
|
||||||
@Service
|
@Service
|
||||||
public class ValidateCodeServiceImpl implements ValidateCodeService
|
public class ValidateCodeServiceImpl implements ValidateCodeService
|
||||||
{
|
{
|
||||||
@Autowired
|
@Resource(name = "captchaProducer")
|
||||||
private Producer producer;
|
private Producer captchaProducer;
|
||||||
|
|
||||||
|
@Resource(name = "captchaProducerMath")
|
||||||
|
private Producer captchaProducerMath;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private RedisService redisService;
|
private RedisService redisService;
|
||||||
|
|
||||||
|
// 验证码类型
|
||||||
|
private String captchaType = "math";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成验证码
|
* 生成验证码
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public AjaxResult createCapcha() throws IOException, CaptchaException
|
public AjaxResult createCapcha() throws IOException, CaptchaException
|
||||||
{
|
{
|
||||||
// 生成验证码
|
|
||||||
String capText = producer.createText();
|
|
||||||
String capStr = capText.substring(0, capText.lastIndexOf("@"));
|
|
||||||
String verifyCode = capText.substring(capText.lastIndexOf("@") + 1);
|
|
||||||
BufferedImage image = producer.createImage(capStr);
|
|
||||||
// 保存验证码信息
|
// 保存验证码信息
|
||||||
String uuid = IdUtils.simpleUUID();
|
String uuid = IdUtils.simpleUUID();
|
||||||
String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
|
String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
|
||||||
|
|
||||||
redisService.setCacheObject(verifyKey, verifyCode, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
|
String capStr = null, code = null;
|
||||||
|
BufferedImage image = null;
|
||||||
|
|
||||||
|
// 生成验证码
|
||||||
|
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))
|
||||||
|
{
|
||||||
|
capStr = code = captchaProducer.createText();
|
||||||
|
image = captchaProducer.createImage(capStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
redisService.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
|
||||||
// 转换流信息写出
|
// 转换流信息写出
|
||||||
FastByteArrayOutputStream os = new FastByteArrayOutputStream();
|
FastByteArrayOutputStream os = new FastByteArrayOutputStream();
|
||||||
try
|
try
|
||||||
|
|
Loading…
Reference in New Issue