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可以方便的配置: · 验证码的字体 · 验证码字体的大小 · 验证码字体的字体颜色 · 验证码内容的范围(数字,字母,中文汉字!) · 验证码图片的大小,边框,边框粗细,边框颜色 · ...
随机推荐
- Codeforces Round 450 D 隔板法+容斥
题意: Count the number of distinct sequences a1, a2, ..., an (1 ≤ ai) consisting of positive integers ...
- Kong 系列【六】添加插件---ip-restriction之黑白名单
写在前边 本地postMan请求http://192.168.130.131:8000/test-route,可以正常访问,本地IP:192.168.130.1同样在虚拟机环境192.168.130. ...
- [转]Android Adapter以及getView()方法的理解
Android Adapter基本理解: 我的理解是: 1.一个有许多getter的类(就是getView(),getCount()....这些方法) 2.有多少个get方法?都是什么? 这些gett ...
- 剑指offer-面试题59_2-队列的最大值-队列
/* 题目: 定义一个含max函数的队列类,并实现pop_front().push_back().max()函数. */ #include<iostream> #include<cs ...
- Couchdb垂直权限绕过到命令执行
0x00couchdb简介 Apache CouchDB是一个开源数据库,专注于易用性和成为"完全拥抱web的数据库".它是一个使用JSON作为存储格式,JavaScript作为查 ...
- Chapter3数学与简单DP
Chapter 3 数学与简单DP 上取整: a / b //下取整 (a + b - 1) / b //上取整 +++ 数学 1.买不到的数目 1205 //如果不知道公式,可以暴搜打表找规律(★) ...
- 转载 CXF动态调用webservice
/** * * @param wsdlUrl wsdl的地址:http://localhost:8001/demo/HelloServiceDemoUrl?wsdl * @param methodNa ...
- 关于Synchronized研伸扩展
代码1 synchronized方法 synchronized void method(){ .......... } 代码2 synchronized代码块 synchronized (obj){ ...
- css3基础-文本与字体+转换+过渡+动画+案例
Css3文本与字体 文本阴影 h1 { text-shadow: 5px 5px 5px red; } word-break换行: h1:nth-child(1) { word-break: no ...
- Neo4j入门-开始使用
前言 关系,指事物之间相互作用.相互影响的状态. 数据之间的关系也是如此,数据之间关系的存储在RDS就已经开始.从数据库支持的外键,到手动建立的关系表,人们采取了许多方法,只为了解决查询复杂.缓慢等问 ...