阅读文本大概需要3分钟。
验证码的作用
图片验证码自从诞生以来从未被抛弃,依然发出属于它所应有的光。验证码经常验证如下一些场景。
1、用户登录,防止机器人登录
2、论坛留言,防止恶意灌水
3、短信验证码发送,防止盗刷短信
Kaptcha 简介
Kaptcha 是一个可高度配置的实用验证码生成工具,可自由配置的选项如:
验证码的字体
验证码字体的大小
验证码字体的字体颜色
验证码内容的范围(数字,字母,中文汉字!)
验证码图片的大小,边框,边框粗细,边框颜色
验证码的干扰线
验证码的样式(鱼眼样式、3D、普通模糊)
Kaptcha详细配置表
描述:图片边框,合法值:yes,no 默认值:yes 配置项:kaptcha.border.color 描述:边框颜色,合法值:r,g,b(andoptionalalpha)或者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 描述:背景颜色渐变,开始颜色 默认值:lightgrey 配置项:kaptcha.background.clear.to 描述:背景颜色渐变,结束颜色 默认值:white 配置项:kaptcha.word.impl 描述:文字渲染器 默认值:com.google.code.kaptcha.text.impl.DefaultWordRenderer 配置项:kaptcha.session.key 描述:sessionkey 默认值:KAPTCHA_SESSION_KEY 配置项:kaptcha.session.date 描述:sessiondate 默认值:KAPTCHA_SESSION_DATE配置项:kaptcha.border
SpringBoot整合 Kaptcha
1、pom.xml文件中引入
<dependency> <groupId>com.oopsguy.kaptchagroupId> <artifactId>kaptcha-spring-boot-autoconfigureartifactId> <version>1.0.0-beta-2version> dependency>
2、配置DefaultKaptcha
importjava.util.Properties; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; importcom.google.code.kaptcha.impl.DefaultKaptcha; importcom.google.code.kaptcha.util.Config; @Configuration publicclassConfigBean{ @Bean publicDefaultKaptchagetDefaultKaptcha(){ DefaultKaptchadk=newDefaultKaptcha(); Propertiesproperties=newProperties(); 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","宋体,楷体,微软雅黑"); Configconfig=newConfig(properties); dk.setConfig(config); returndk; } }packagecom.piano;
3、编写controller
importjava.awt.image.BufferedImage; importjava.io.IOException; importjavax.imageio.ImageIO; importjavax.servlet.ServletOutputStream; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.stereotype.Controller; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RequestMethod; importorg.springframework.web.servlet.ModelAndView; importcom.google.code.kaptcha.Constants; importcom.google.code.kaptcha.impl.DefaultKaptcha; @Controller publicclassKaptchaController{ @Autowired privateDefaultKaptchacaptchaProducer; @RequestMapping(value="verification",method=RequestMethod.GET) publicModelAndViewverification(HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException{ response.setDateHeader("Expires",0); //SetstandardHTTP/1.1no-cacheheaders. response.setHeader("Cache-Control","no-store,no-cache,must-revalidate"); //SetIEextendedHTTP/1.1no-cacheheaders(useaddHeader). response.addHeader("Cache-Control","post-check=0,pre-check=0"); //SetstandardHTTP/1.0no-cacheheader. response.setHeader("Pragma","no-cache"); //returnajpeg response.setContentType("image/jpeg"); //createthetextfortheimage StringcapText=captchaProducer.createText(); //storethetextinthesession request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY,capText); //createtheimagewiththetext BufferedImagebi=captchaProducer.createImage(capText); ServletOutputStreamout=response.getOutputStream(); //writethedataout ImageIO.write(bi,"jpg",out); try{ out.flush(); }finally{ out.close(); } returnnull; } }packagecom.piano.student.controller;
4、访问http://127.0.0.1:8083/verification
☆
往期精彩
☆
01漫谈发版哪些事,好课程推荐
02Linux的常用最危险的命令
03精讲Spring Boot—入门+进阶+实例
04优秀的Java程序员必须了解的GC哪些
05互联网支付系统整体架构详解
关注我
每天进步一点点
很干!在看吗?☟