Kaptcha图片验证码工具
阅读文本大概需要3分钟。
验证码的作用
图片验证码自从诞生以来从未被抛弃,依然发出属于它所应有的光。验证码经常验证如下一些场景。
1、用户登录,防止机器人登录
2、论坛留言,防止恶意灌水
3、短信验证码发送,防止盗刷短信
Kaptcha 简介
Kaptcha 是一个可高度配置的实用验证码生成工具,可自由配置的选项如:
验证码的字体
验证码字体的大小
验证码字体的字体颜色
验证码内容的范围(数字,字母,中文汉字!)
验证码图片的大小,边框,边框粗细,边框颜色
验证码的干扰线
验证码的样式(鱼眼样式、3D、普通模糊)
Kaptcha详细配置表
配置项:kaptcha.border
描述:图片边框,合法值:yes , no
默认值:yes配置项:kaptcha.border.color
描述:边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue.
默认值:black配置项:kaptcha.image.width
描述:图片宽
默认值:200配置项:kaptcha.image.height
描述:图片高
默认值:50配置项:kaptcha.producer.impl
描述:图片实现类
默认值:com.google.code.kaptcha.impl.DefaultKaptcha配置项:kaptcha.textproducer.impl
描述:文本实现类
默认值:com.google.code.kaptcha.text.impl.DefaultTextCreator配置项:kaptcha.textproducer.char.string
描述:文本集合,验证码值从此集合中获取
默认值:abcde2345678gfynmnpwx配置项:kaptcha.textproducer.char.length
描述:验证码长度
默认值:5配置项:kaptcha.textproducer.font.names
描述:字体
默认值:Arial, Courier配置项:kaptcha.textproducer.font.size
描述:字体大小
默认值:40px.配置项:kaptcha.textproducer.font.color
描述:字体颜色,合法值: r,g,b 或者 white,black,blue.
默认值:black配置项:kaptcha.textproducer.char.space
描述:文字间隔
默认值:2配置项:kaptcha.noise.impl
描述:干扰实现类
默认值:com.google.code.kaptcha.impl.DefaultNoise配置项:kaptcha.noise.color
描述:干扰 颜色,合法值: r,g,b 或者 white,black,blue.
默认值:black配置项:kaptcha.obscurificator.impl
描述:图片样式,水纹 com.google.code.kaptcha.impl.WaterRipple 鱼眼 com.google.code.kaptcha.impl.FishEyeGimpy 阴影 com.google.code.kaptcha.impl.ShadowGimpy
默认值:com.google.code.kaptcha.impl.WaterRipple配置项:kaptcha.background.impl
描述:背景实现类
默认值:com.google.code.kaptcha.impl.DefaultBackground配置项:kaptcha.background.clear.from
描述:背景颜色渐变,开始颜色
默认值:light grey配置项:kaptcha.background.clear.to
描述:背景颜色渐变, 结束颜色
默认值:white配置项:kaptcha.word.impl
描述:文字渲染器
默认值:com.google.code.kaptcha.text.impl.DefaultWordRenderer配置项:kaptcha.session.key
描述:session key
默认值:KAPTCHA_SESSION_KEY配置项:kaptcha.session.date
描述:session date
默认值:KAPTCHA_SESSION_DATE
SpringBoot整合 Kaptcha
1、pom.xml文件中引入
<!-- https://mvnrepository.com/artifact/com.oopsguy.kaptcha/kaptcha-spring-boot-autoconfigure -->
<dependency><groupId>com.oopsguy.kaptcha</groupId><artifactId>kaptcha-spring-boot-autoconfigure</artifactId><version>1.0.0-beta-2</version>
</dependency>
2、配置DefaultKaptcha
package com.piano;import java.util.Properties;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;@Configuration
public class ConfigBean {@Beanpublic DefaultKaptcha getDefaultKaptcha(){DefaultKaptcha dk = new DefaultKaptcha();Properties properties = new Properties();properties.put("kaptcha.border", "yes");properties.put("kaptcha.border.color","105,179,90");properties.put("kaptcha.textproducer.font.color","blue");properties.put("kaptcha.image.width","125");properties.put("kaptcha.image.height","45");properties.put("kaptcha.textproducer.font.size","45");properties.put("kaptcha.session.key","code");properties.put("kaptcha.textproducer.char.length","4");properties.put("kaptcha.textproducer.font.names","宋体,楷体,微软雅黑");Config config = new Config(properties );dk.setConfig(config);return dk;}}
3、编写controller
package com.piano.student.controller;import java.awt.image.BufferedImage;
import java.io.IOException;import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;@Controller
public class KaptchaController {@Autowiredprivate DefaultKaptcha captchaProducer;@RequestMapping(value = "verification", method = RequestMethod.GET)public ModelAndView verification(HttpServletRequest request, HttpServletResponse response) throws IOException {response.setDateHeader("Expires", 0);// Set standard HTTP/1.1 no-cache headers.response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");// Set IE extended HTTP/1.1 no-cache headers (use addHeader).response.addHeader("Cache-Control", "post-check=0, pre-check=0");// Set standard HTTP/1.0 no-cache header.response.setHeader("Pragma", "no-cache");// return a jpegresponse.setContentType("image/jpeg");// create the text for the imageString capText = captchaProducer.createText();// store the text in the sessionrequest.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);// create the image with the textBufferedImage bi = captchaProducer.createImage(capText);ServletOutputStream out = response.getOutputStream();// write the data outImageIO.write(bi, "jpg", out);try {out.flush();} finally {out.close();}return null;}
}
4、访问http://127.0.0.1:8083/verification
☆
往期精彩
☆
01 漫谈发版哪些事,好课程推荐
02 Linux的常用最危险的命令
03 精讲Spring Boot—入门+进阶+实例
04 优秀的Java程序员必须了解的GC哪些
05 互联网支付系统整体架构详解
关注我
每天进步一点点
很干!在看吗?☟
Kaptcha图片验证码工具相关推荐
- 谷歌了java集成开发_Spring整合Kaptcha谷歌验证码工具的开发步骤
开发步骤: 1.加入依赖 com.google.code.kaptcha kaptcha 2.3 国内镜像无法下载该依赖,需要手动通过jar包在本地仓库安装一个依赖. 安装命令: mvn instal ...
- Kaptcha 图片验证码使用
前言 图验设计初衷就是为了提高系统的安全性,防止有些人利用自动化工具恶意攻击网站,比如批量注册.撞库等,Kaptcha 框架就是现场一个图片验证码工具,简单好上手,我们大可直接使用 使用 1. 添加依 ...
- kaptcha图片验证码
谷歌kaptcha图片验证码的使用 文章目录 前言 一.引入依赖 二.kaptcha配置 1.配置类 2.接口信息 二.在Swagger中进行测试 前言 验证码可以有效的防止重复提交表单数据,防止来自 ...
- bufferedimage生成的图片模糊_Kaptcha图片验证码工具
阅读文本大概需要3分钟. 验证码的作用 图片验证码自从诞生以来从未被抛弃,依然发出属于它所应有的光.验证码经常验证如下一些场景. 1.用户登录,防止机器人登录 2.论坛留言,防止恶意灌水 3.短信验证 ...
- Java生成随机图片验证码工具类
一.第一种生成随机图片验证码 public class VerifyCodeUtil {//生成随机数据的数据源 public static final String VERIFY_CODES = & ...
- 谷歌kaptcha 图片验证码的使用
谷歌验证码kaptcha 使用步骤如下: 1.导入谷歌验证码的jar 包 kaptcha-2.3.2.jar 2.在web.xml 中去配置用于生成验证码的Servlet 程序 <servlet ...
- SpringBoot整合kaptcha(谷歌验证码工具)实现验证码功能
介绍: kaptcha是Google提供的一个图形验证码插件,有了它,你可以通过简单的配置生成各种样式的验证码. 1:SpringBoot引入kaptcha的依赖 <dependency> ...
- 使用Kaptcha生成图片验证码
使用Kaptcha生成图片验证码 kaptcha是什么? kaptcha使用步骤 kaptcha代码实例 引入依赖 图片验证码配置类 自定义结果返回类 生成验证码 返回结果枚举 kaptcha是什么? ...
- Spring Boot整合Shiro + JSP教程(用户认证,权限管理,图片验证码)
在此首先感谢**编程不良人**up主提供的视频教程 代码都是跟着up的视频敲的,遇到的一些问题也是通过CSDN博主提供的教程解决的,在此也感谢那些提供bug解决方案的前辈们~ 项目完整代码已经发布到g ...
最新文章
- 截断 四舍五入 进位php,PHP取整,四舍五入取整、向上取整、向下取整、小数截取 四舍五入是什么意思 四舍五入就是一个亿 excel数值四舍五...
- 汇编 begin_【精品】小学作文500字汇编九篇
- 从概念到案例:初学者须知的十大机器学习算法
- 2 引入失败_苦等2年!总投资2800万!镇江首个民国风商场,来了!
- 栈在表达式计算过程中的应用
- weiphp 简介--笔记
- Java基础(三)--final关键字
- cc2530设计性实验代码四
- CSDN免费快速获得积分和直接获取下载码的几个办法
- 湖北省小学生计算机大赛,湖北省中小学电脑制作活动机器人竞赛类暨创客竞赛类项目选拔赛在我校举行...
- 手握IP却不知如何讲好城市故事?“宝藏天津”慢直播支招城市营销
- Android AES 文件加密解密
- 高端加密IC开发常见问题
- php音频对比技术,HIFIDIY论坛-三种音频功放的音质特点比较 供参考 - Powered by Discuz!...
- 用自己训练的AI玩王者荣耀是什么体验?
- File xxx specified for property 'resourceFile' does not exist.解决方法
- 金融计算机次方的计算,金融理财计算器使用方法CASIOFC100,200.pdf
- android 破解软件
- 如何创作一款商业级的安卓独立应用
- 串口硬盘如何使用ghost