springboot中使用kaptcha验证码
maven依赖
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
kaptcha配置
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 KaptchaConfig {
@Bean
public DefaultKaptcha getDefaultKaptcha() {
DefaultKaptcha kaptcha = new DefaultKaptcha();
Properties properties = new Properties();
// 图片边框
properties.setProperty("kaptcha.border", "no");
// 字体颜色
properties.setProperty("kaptcha.textproducer.font.color", "black");
// 字体间距
properties.setProperty("kaptcha.textproducer.char.space", "5");
// 图片宽
properties.setProperty("kaptcha.image.width", "120");
// 图片高
properties.setProperty("kaptcha.image.height", "50");
// 验证码长度
properties.setProperty("kaptcha.textproducer.char.length", "4");
// 噪点生成对象
properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");
// 边框颜色
properties.setProperty("kaptcha.border.color", "105,179,55");
// 字体大小
// properties.setProperty("kaptcha.textproducer.font.size", "30");
// session key
// properties.setProperty("kaptcha.session.key", "code");
// 字体
// properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
Config config = new Config(properties);
kaptcha.setConfig(config);
return kaptcha;
}
}
生成验证码
在这里我们把正确的验证码放到redis中,便于后续的验证
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.me.flashsale.common.Constants;
import com.me.flashsale.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.concurrent.TimeUnit;
/**
* 验证码的生成
*/
@Controller
@Slf4j
@RequestMapping("/kaptcha")
public class KaptchaController {
/**
* 1、验证码工具
*/
@Autowired
private DefaultKaptcha kaptcha;
@Autowired
private StringRedisTemplate redisTemplate;
/**
* 生成验证码
* @param httpServletResponse
* @param user
* @param goodsId
* @throws Exception
*/
@RequestMapping("/jpg")
public void getKaptcha(HttpServletResponse httpServletResponse, User user, long goodsId)
throws Exception {
byte[] captchaChallengeAsJpeg;
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
try {
// 生产验证码字符串并保存到redis中
String rightCode = kaptcha.createText();
log.info("rightCode:{}", rightCode);
redisTemplate.opsForValue().set(Constants.Cache.KAPTCHA_REDIS_PREFIX+user.getId()+"_goods"+goodsId, rightCode,
Constants.Cache.KAPTCHA_EXPIRE_TIME, TimeUnit.SECONDS);
// 使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
BufferedImage challenge = kaptcha.createImage(rightCode);
ImageIO.write(challenge, "jpg", jpegOutputStream);
} catch (IllegalArgumentException e) {
httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
// 定义response输出类型为image/jpeg类型,使用response输出流输出图片的byte数组
captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
httpServletResponse.setHeader("Cache-Control", "no-store");
httpServletResponse.setHeader("Pragma", "no-cache");
httpServletResponse.setDateHeader("Expires", 0);
httpServletResponse.setContentType("image/jpeg");
ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
responseOutputStream.write(captchaChallengeAsJpeg);
responseOutputStream.flush();
responseOutputStream.close();
}
}
前端使用
使用的方式很简单,直接img标签的src属性调用controller的接口即可
验证逻辑
@Autowired
private StringRedisTemplate redisTemplate;
/**
* 校对验证码
* @param user
* @param goodsId
* @param tryCode
* @return
*/
public Boolean imgVerifyCode(User user, long goodsId, String tryCode) {
String rightCode = redisTemplate.opsForValue().get(Constants.Cache.KAPTCHA_REDIS_PREFIX + user.getId()
+"_goods"+ goodsId);
log.info("rightCode={}, tryCode={}", rightCode, tryCode);
return tryCode.equals(rightCode);
}
springboot中使用kaptcha验证码的更多相关文章
- Spring mvc 中使用 kaptcha 验证码
生成验证码的方式有很多,个人认为较为灵活方便的是Kaptcha ,他是基于SimpleCaptcha的开源项目.使用Kaptcha 生成验证码十分简单并且参数可以进行自定义.只需添加jar包配置下就可 ...
- springboot 集成kaptcha验证码Demo
验证码(CAPTCHA)是“Completely Automated Public Turing test to tell Computers and Humans Apart”(全自动区分计算机和人 ...
- Springboot整合kaptcha验证码
01.通过配置类来配置kaptcha 01-01.添加kaptcha的依赖: <!-- kaptcha验证码 --> <dependency> <groupId>c ...
- SpringBoot开发九-生成验证码
需求介绍-生成验证码 先生成随机字符串然后利用Kaptcha API生成验证图片 代码实现 先在pom.xml引入 <dependency> <groupId>com.gith ...
- Java实现验证码制作之一Kaptcha验证码
Kaptcha验证码 是google提供的验证码插件,使用起来相对简单,设置的干扰线以及字体扭曲不易让其他人读取破解. 这里我们需要 导入一个 kaptcha-2.3.jar 下载地址:http:/ ...
- kaptcha验证码插件的使用
kaptcha 是一个非常实用的验证码生成工具.有了它,你可以生成各种样式的验证码,因为它是可配置的.kaptcha工作的原理是调用 com.google.code.kaptcha.servlet.K ...
- 如何使用kaptcha验证码组件
kaptcha是基于SimpleCaptcha的验证码开源项目. kaptcha是纯配置的,使用起来比较友好.如使用了Servlet,所有配置都在web.xml中.如果你在项目中使用了开源框架(比如S ...
- kaptcha 验证码组件使用
kaptcha 验证码组件使用简介 kaptcha 是一个非常实用的验证码生成工具.有了它,你可以生成各种样式的验证码,因为它是可配置的.kaptcha工作的原理是调用 com.google.co ...
- KAPTCHA验证码使用步骤
使用kaptcha可以方便的配置: · 验证码的字体 · 验证码字体的大小 · 验证码字体的字体颜色 · 验证码内容的范围(数字,字母,中文汉字!) · 验证码图片的大小,边框,边框粗细,边框颜色 · ...
随机推荐
- linux 基础入门(9) 系统服务 systemctl 与 xinted的运用
9.系统服务 9.1系统服务 可以把计算机理解为一个地点比如中关村大街系统服务理解为中关村大街的理发店.饭店.商场等等,每一个都是一个系统服务,为客户提供不同内容的服务 服务:常驻在内存中的程序,且可 ...
- 机器学习算法——kNN
顶级数据挖掘会议ICDM于2006年12月评选出了数据挖掘领域的十大经典算法,kNN便是其中一个. kNN算法的思想是:在训练集中选取与输入数据最近的k个邻居,统计k个邻居中出现次数最多的类别,以此作 ...
- 插入数据失败提示: Setting autocommit to false on JDBC Connection 自动提交失败
来源:https://blog.csdn.net/qq_42799475/article/details/102742109 今天在执行mybstis的测试时,明明已经写好了插入语句但是数据库没有插入 ...
- Mac下搭建selenium环境
1,安装selenium 打开terminal,使用以下命令安装selenium: pip install -U selenium 2,下载chromedriver,并放在python的安装根目录 ...
- 二、Nginx配置实例
Nginx配置实例 一.反向代理 实例一 1.实现效果 打开浏览器,在浏览器地址栏输入地址 www.123.com ,跳转到linux系统tomcat主页面中. 2.准备工作 在linux系统中安装t ...
- Windows10设置系统参数
屏幕分辨率设置 电源屏幕显示时间 投影可以进行手机投影到电脑进行操作,远程桌面可以进行远程访问,如云服务器 设置桌面图标和背景 设置默认应用 安装软件,必备的几项软件 --其中个人认为(1)(2)是 ...
- ArcGIS Runtime SDK for Android中SimpleFillSymbol.Style样式
SimpleFillSymbol.Style样式枚举共8种: 1.BACKWARD_DIAGONAL 反对角线填充 2.CROSS 交叉线填充 3.DIAGONAL_CROSS 前后对角线填充 4.F ...
- 快速读写模板(int)
一.快速读入模板(int) inline int read(int x){ char ch=getchar(); int x=0,f=1; while(ch>='9'||ch<='0'){ ...
- Java多线程之互斥锁Syncharnized
public class Bank { private int money; private String name; public Bank(String name, int money) { th ...
- 【笔记】机器学习 - 李宏毅 - 11 - Keras Demo2 & Fizz Buzz
1. Keras Demo2 前节的Keras Demo代码: import numpy as np from keras.models import Sequential from keras.la ...