You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.0 KiB

6 years ago
/**
* Copyright (c) 2018 人人开源 All rights reserved.
*
* https://www.renren.io
*
* 版权所有侵权必究
*/
package com.epmet.service.impl;
import com.epmet.StringRandomUtils;
import com.epmet.commons.tools.constant.NumConstant;
6 years ago
import com.epmet.redis.CaptchaRedis;
import com.epmet.service.CaptchaService;
import com.google.code.kaptcha.Producer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
6 years ago
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.awt.image.BufferedImage;
/**
* 验证码
*
* @author Mark sunlightcs@gmail.com
* @since 1.0.0
*/
@Service
public class CaptchaServiceImpl implements CaptchaService {
private Logger logger = LogManager.getLogger(CaptchaServiceImpl.class);
6 years ago
@Autowired
private Producer producer;
@Autowired
private CaptchaRedis captchaRedis;
@Override
public BufferedImage create(String uuid) {
//生成验证码 //producer.createText();
String captcha = StringRandomUtils.getRandomStr(NumConstant.FIVE);
//logger.info("uuid:"+uuid+",生成的验证码:"+captcha);
6 years ago
//保存验证码
captchaRedis.set(uuid, captcha);
return producer.createImage(captcha);
}
@Override
public boolean validate(String uuid, String code) {
String captcha = captchaRedis.get(uuid);
//验证码是否正确
4 years ago
return code.equalsIgnoreCase(captcha);
6 years ago
}
/**
* 返回文字版的验证码
* 磐石大屏3.10号提出的需求
*
* @param uuid
* @return
*/
@Override
public String getTextCaptcha(String uuid) {
// 生成验证码 //producer.createText();
String captcha = StringRandomUtils.getRandomStr(NumConstant.FIVE);
// logger.info("uuid:"+uuid+",生成的验证码:"+captcha);
// 保存验证码
captchaRedis.set(uuid, captcha);
return captcha;
}
6 years ago
}