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.

58 lines
1.4 KiB

6 years ago
/**
* 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;
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) {
//生成验证码
String captcha = producer.createText();
//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);
//验证码是否正确
if(code.equalsIgnoreCase(captcha)){
return true;
}
return false;
}
}