1.效果展示:
2.实现方式
1)引入依赖
<!--验证码-->
<dependency><groupId>com.github.axet</groupId><artifactId>kaptcha</artifactId><version>0.0.9<</version>
</dependency>
2)配置KaptchaConfiguration 类
package cn.ffcs.videocloud.admin.config;import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.Properties;@Configuration
public class KaptchaConfiguration {private static final String KAPTCHA_BORDER = "kaptcha.border";private static final String KAPTCHA_TEXTPRODUCER_FONT_COLOR = "kaptcha.textproducer.font.color";private static final String KAPTCHA_TEXTPRODUCER_CHAR_SPACE = "kaptcha.textproducer.char.space";private static final String KAPTCHA_IMAGE_WIDTH = "kaptcha.image.width";private static final String KAPTCHA_IMAGE_HEIGHT = "kaptcha.image.height";private static final String KAPTCHA_TEXTPRODUCER_CHAR_LENGTH = "kaptcha.textproducer.char.length";private static final Object KAPTCHA_IMAGE_FONT_SIZE = "kaptcha.textproducer.font.size";/*** 默认生成图形验证码宽度*/private static final String DEFAULT_IMAGE_WIDTH = "80";/*** 默认生成图像验证码高度*/private static final String DEFAULT_IMAGE_HEIGHT = "36";/*** 边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue.*/private static final String DEFAULT_COLOR_FONT = "black";/*** 图片边框*/private static final String DEFAULT_IMAGE_BORDER = "no";/*** 默认图片间隔*/private static final String DEFAULT_CHAR_SPACE = "5";/*** 验证码文字大小*/private static final String DEFAULT_IMAGE_FONT_SIZE = "30";private static final String CODE_SIZE = "4";@Beanpublic DefaultKaptcha producer() {Properties properties = new Properties();properties.put(KAPTCHA_BORDER, DEFAULT_IMAGE_BORDER);properties.put(KAPTCHA_TEXTPRODUCER_FONT_COLOR, DEFAULT_COLOR_FONT);properties.put(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, DEFAULT_CHAR_SPACE);properties.put(KAPTCHA_IMAGE_WIDTH, DEFAULT_IMAGE_WIDTH);properties.put(KAPTCHA_IMAGE_HEIGHT, DEFAULT_IMAGE_HEIGHT);properties.put(KAPTCHA_IMAGE_FONT_SIZE, DEFAULT_IMAGE_FONT_SIZE);properties.put(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, CODE_SIZE);Config config = new Config(properties);DefaultKaptcha defaultKaptcha = new DefaultKaptcha();defaultKaptcha.setConfig(config);return defaultKaptcha;}
}
3)业务实现类
package cn.ffcs.videocloud.admin.service.code;import cn.ffcs.videocloud.common.constant.AdminConstants;
import com.google.code.kaptcha.Producer;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.TimeUnit;@Slf4j
@Component
public class ImagesCaptcha {@Resourceprivate Producer producer;@Resourceprivate RedisTemplate redisTemplate;private static final String JPEG = "jpeg";@Value("${test.key:SHEN_YU}")private String REDIS_KEY;@Value("${test.code_time_out:300}")private Integer CODE_TIME_OUT ; // 超时时间public static final String IMAGES_CAPTCHA_KEY = "IMAGES_CAPTCHA_KEY_"; // 图片验证码属性名public String buildCaptcha(String uuid, HttpServletResponse response){response.addHeader("uuid", uuid);//生成验证码String text = producer.createText();redisTemplate.opsForValue().set(REDIS_KEY+IMAGES_CAPTCHA_KEY + uuid, text, CODE_TIME_OUT, TimeUnit.SECONDS);BufferedImage image = producer.createImage(text);try {ImageIO.write(image, JPEG, response.getOutputStream());} catch (IOException e) {e.printStackTrace();}return uuid;}/*** 判断验证码是否正确** @param uuid* @return*/public String checkImagesCaptcha(String uuid, String code) {if(StringUtils.isEmpty(uuid) || StringUtils.isEmpty(code)){return "error";}String key = REDIS_KEY+IMAGES_CAPTCHA_KEY + uuid;String codeRight = (String)redisTemplate.opsForValue().get(key);if (StringUtils.isEmpty(codeRight)){return AdminConstants.IMAGES_CAPTCHA_TIME_OUT;} else if (codeRight.equalsIgnoreCase(code)) {redisTemplate.delete(key);return "succes";} else {return "error";}}
}
以上仅是本人自己的学习分享,有什么不妥之处请告知。