/** * 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; 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; 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); @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); //保存验证码 captchaRedis.set(uuid, captcha); return producer.createImage(captcha); } @Override public boolean validate(String uuid, String code) { String captcha = captchaRedis.get(uuid); //验证码是否正确 return code.equalsIgnoreCase(captcha); } /** * 返回文字版的验证码 * 磐石大屏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; } }