|
|
|
/**
|
|
|
|
* Copyright (c) 2018 人人开源 All rights reserved.
|
|
|
|
*
|
|
|
|
* https://www.renren.io
|
|
|
|
*
|
|
|
|
* 版权所有,侵权必究!
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.epmet.service.impl;
|
|
|
|
|
|
|
|
import com.google.code.kaptcha.Producer;
|
|
|
|
import com.epmet.redis.CaptchaRedis;
|
|
|
|
import com.epmet.service.CaptchaService;
|
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 验证码
|
|
|
|
*
|
|
|
|
* @author Mark sunlightcs@gmail.com
|
|
|
|
* @since 1.0.0
|
|
|
|
*/
|
|
|
|
@Service
|
|
|
|
public class CaptchaServiceImpl implements CaptchaService {
|
|
|
|
private Logger logger = LogManager.getLogger(CaptchaServiceImpl.class);
|
|
|
|
@Autowired
|
|
|
|
private Producer producer;
|
|
|
|
@Autowired
|
|
|
|
private CaptchaRedis captchaRedis;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BufferedImage create(String uuid) {
|
|
|
|
//生成验证码
|
|
|
|
String captcha = producer.createText();
|
|
|
|
//logger.info("uuid:"+uuid+",生成的验证码:"+captcha);
|
|
|
|
//保存验证码
|
|
|
|
captchaRedis.set(uuid, captcha);
|
|
|
|
|
|
|
|
return producer.createImage(captcha);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BufferedImage createIcLoginCaptcha(String uuid) {
|
|
|
|
//生成验证码
|
|
|
|
String captchaText = producer.createText();
|
|
|
|
//logger.info("uuid:"+uuid+",生成的验证码:"+captcha);
|
|
|
|
//保存验证码
|
|
|
|
captchaRedis.setIcLoginCaptcha(uuid, captchaText);
|
|
|
|
|
|
|
|
return producer.createImage(captchaText);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean validate(String uuid, String code) {
|
|
|
|
String captcha = captchaRedis.get(uuid);
|
|
|
|
|
|
|
|
//验证码是否正确
|
|
|
|
if(code.equalsIgnoreCase(captcha)){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|