目录
一、依赖
二、生成验证码服务
一、依赖
com.github.whvcse
easy-captcha
${captcha.version}
二、生成验证码服务
import java.io.IOException;
import java.util.Map;
public interface EasyCaptchaService {
Integer CAPTCHA_WIDTH = 130;
Integer CAPTCHA_HEIGHT = 48;
Integer CAPTCHA_LENGTH = 4;
String CAPTCHA_KEY_PRE = "CAPTCHA:LIST:";
Integer CAPTCHA_CACHE_TIME = 5;
int CAPTCHA_TYPE_DEFAULT_PNG = 10;
final int CAPTCHA_TYPE_GIF = 20;
final int CAPTCHA_TYPE_CHINESE = 30;
final int CAPTCHA_TYPE_GIF_CHINESE = 40;
final int CAPTCHA_TYPE_ARITHMETIC = 50;
Map outputCaptchaImg(Integer type) throws IOException;
boolean verifyCaptchaCode(String uuid,String captchaCode);
}
import java.io.IOException;
import java.util.Map;
public interface EasyCaptchaService {
Integer CAPTCHA_WIDTH = 130;
Integer CAPTCHA_HEIGHT = 48;
Integer CAPTCHA_LENGTH = 4;
String CAPTCHA_KEY_PRE = "CAPTCHA:LIST:";
Integer CAPTCHA_CACHE_TIME = 5;
int CAPTCHA_TYPE_DEFAULT_PNG = 10;
final int CAPTCHA_TYPE_GIF = 20;
final int CAPTCHA_TYPE_CHINESE = 30;
final int CAPTCHA_TYPE_GIF_CHINESE = 40;
final int CAPTCHA_TYPE_ARITHMETIC = 50;
Map outputCaptchaImg(Integer type) throws IOException;
boolean verifyCaptchaCode(String uuid,String captchaCode);
}
实现类:
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.exceptions.ApiException;
import com.party.constant.Constants;
import com.party.service.EasyCaptchaService;
import com.wf.captcha.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@Service
public class EasyCaptchaServiceImpl implements EasyCaptchaService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
private void saveCaptcha2Cache(String uuid, String codeValue) {
stringRedisTemplate.opsForValue().set(CAPTCHA_KEY_PRE + uuid, codeValue, CAPTCHA_CACHE_TIME, TimeUnit.MINUTES);
}
@Override
public Map outputCaptchaImg(Integer type) throws IOException {
String codeValue="";
Map map=new HashMap<>();
switch (type) {
case CAPTCHA_TYPE_DEFAULT_PNG:
SpecCaptcha specCaptcha = new SpecCaptcha(CAPTCHA_WIDTH,CAPTCHA_HEIGHT,CAPTCHA_LENGTH);
codeValue = specCaptcha.text().toLowerCase();
map.put(Constants.IMAGE,specCaptcha.toBase64());
break;
case CAPTCHA_TYPE_GIF:
GifCaptcha gifCaptcha = new GifCaptcha(CAPTCHA_WIDTH, CAPTCHA_HEIGHT, CAPTCHA_LENGTH);
codeValue = gifCaptcha.text().toLowerCase();
map.put(Constants.IMAGE,gifCaptcha.toBase64());
break;
case CAPTCHA_TYPE_CHINESE:
ChineseCaptcha chineseCaptcha = new ChineseCaptcha(CAPTCHA_WIDTH,CAPTCHA_HEIGHT,CAPTCHA_LENGTH);
codeValue = chineseCaptcha.text();
map.put(Constants.IMAGE,chineseCaptcha.toBase64());
break;
case CAPTCHA_TYPE_GIF_CHINESE:
ChineseGifCaptcha chineseGifCaptcha = new ChineseGifCaptcha(CAPTCHA_WIDTH, CAPTCHA_HEIGHT, CAPTCHA_LENGTH);
codeValue = chineseGifCaptcha.text();
map.put(Constants.IMAGE,chineseGifCaptcha.toBase64());
break;
case CAPTCHA_TYPE_ARITHMETIC:
ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha(CAPTCHA_WIDTH,CAPTCHA_HEIGHT);
codeValue = arithmeticCaptcha.text();
map.put(Constants.IMAGE,arithmeticCaptcha.toBase64());
break;
default:
throw new IOException("类型不存在");
}
//存入redis
String uuid = UUID.randomUUID().toString();
saveCaptcha2Cache(uuid,codeValue);
map.put(Constants.UUID,uuid);
return map;
}
@Override
public boolean verifyCaptchaCode(String uuid, String captchaCode) throws ApiException{
String cacheCaptchaCode = stringRedisTemplate.opsForValue().get(CAPTCHA_KEY_PRE + uuid);
if (StringUtils.isBlank(cacheCaptchaCode)) {
throw new ApiException("验证码已过期,请重新获取");
}
if (!cacheCaptchaCode.equals(captchaCode.toLowerCase())){
throw new ApiException("验证码错误");
}
return true;
}
}
调用如下:
easyCaptchaService.outputCaptchaImg(EasyCaptchaService.CAPTCHA_TYPE_GIF);
最后返回的结果为Base64转换后验证码图片,通过base64反解可以拿到图片,uuid为存入redis后对应的key名,验证码数据被存入了redis里。



