SpringBoot 实现图形验证码

        <dependency>
            <groupId>com.github.penggle</groupId>
            <artifactId>kaptcha</artifactId>
            <version>2.3.2</version>
        </dependency>

  把kaptcha作为工程的一个类,加上@Configration注解在返回kaptcha的方法中加上@Bean注解

/**
 * Created by mhSui on 2019/12/05.
 *
 * @author mhSui
 */
@Configuration
public class KaptchaConfig {

	private final static String CODE_LENGTH = "4";

	private final static String SESSION_KEY = "handsome_yang";

	@Bean
	public DefaultKaptcha defaultKaptcha() {
		DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
		Properties properties = new Properties();
		// 设置边框,合法值:yes , no
		properties.setProperty("kaptcha.border", "yes");
		// 设置边框颜色,合法值: r,g,b (and optional alpha) 或者 white,
		properties.setProperty("kaptcha.border.color", "105,179,90");
		// 设置字体颜色, r,g,b 或者 white,black,blue.
		properties.setProperty("kaptcha.textproducer.font.color", "blue");
		// 设置图片宽度
		properties.setProperty("kaptcha.image.width", "118");
		// 设置图片高度
		properties.setProperty("kaptcha.image.height", "40");
		// 设置字体尺寸
		properties.setProperty("kaptcha.textproducer.font.size", "30");
		// 设置session key
		properties.setProperty("kaptcha.session.key", SESSION_KEY);
		// 设置验证码长度
		properties.setProperty("kaptcha.textproducer.char.length", CODE_LENGTH);
		// 设置字体
		properties.setProperty("kaptcha.textproducer.font.names", "楷体");
		Config config = new Config(properties);
		defaultKaptcha.setConfig(config);
		return defaultKaptcha;
	}

}

  下面是我调用封装到对象返回给前端,因为前端原因,这样我这里返回base64的方式

/**
 * PicKaptchaUtil.
 *
 * @author mhSui
 */
@Slf4j
@Component
public class PicKaptchaUtil {

	@Autowired
	private DefaultKaptcha defaultKaptcha;

	@Autowired
	private RedisClient redisTemplate;

	/**
	 * 生成验证码.
	 * @param request request
	 * @param response response
	 * @return CaptchaDTO
	 */
	public CaptchaDTO kaptcha(HttpServletRequest request, HttpServletResponse response) {
		// 获取sessionId
		CaptchaDTO captchaDTO = new CaptchaDTO();
		String key = "JQ" + request.getSession().getId();
		// 生产验证码字符串
		String createText = this.defaultKaptcha.createText();
		// 使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
		BufferedImage bufferedImage = this.defaultKaptcha.createImage(createText);
		ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
		try {
			// 使用生产的验证码字符串返回一个BufferedImage
			ImageIO.write(bufferedImage, "jpeg", jpegOutputStream);
			Base64.Encoder encoder = Base64.getEncoder();
			String base64 = encoder.encodeToString(jpegOutputStream.toByteArray());
			String captchaBase64 = "data:image/jpeg;base64,"
					+ base64.replaceAll("
", "");

			captchaDTO.setCaptchaBase64(captchaBase64);
			captchaDTO.setKey(key);

			// 存储到redis,过期时间30分钟
			this.redisTemplate.set(key, createText);
			this.redisTemplate.expire(key, 15 * 60);

			log.info("图形验证码" + createText);
		}
		catch (IOException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
		finally {
			try {
				if (jpegOutputStream != null) {
					jpegOutputStream.close();
				}
			}
			catch (IOException e) {
				e.printStackTrace();
			}
		}
		return captchaDTO;
	}

	/**
	 * 校验验证码.
	 * @param key kaptcha-key
	 * @param kaptcha kaptcha
	 * @return 校验结果
	 */
	public boolean check(String key, String kaptcha) {
		String redisKaptcha = this.redisTemplate.get(key);
		return (!StringUtils.isEmpty(kaptcha)) && kaptcha.equals(redisKaptcha);
	}

}

  这就是我项目中运用的kaptcha图形验证码

原文地址:https://www.cnblogs.com/mhSui/p/12187279.html