
Kaptcha+SpringBoot实现验证码功能(单体/前后端分离)
发布日期:2021-05-08 01:12:19
浏览次数:26
分类:原创文章
本文共 5192 字,大约阅读时间需要 17 分钟。
1、导入Kaptcha依赖支持
<dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version></dependency>
2、编写实现验证码功能的实现类
//实现验证码功能配置类@Configurationpublic class KaptchaConfig { private final static String CODE_LENGTH = "4"; @Bean public Producer kaptchaProducer() { Properties properties = new Properties(); // 设置边框 properties.setProperty("kaptcha.border", "yes"); // 设置边框颜色 properties.setProperty("kaptcha.border.color", "105,179,90"); // 设置字体颜色 properties.setProperty("kaptcha.textproducer.font.color", "0,153,153"); // 设置图片宽度 properties.setProperty("kaptcha.image.width", "118"); // 设置图片高度 properties.setProperty("kaptcha.image.height", "36"); // 设置字体尺寸 properties.setProperty("kaptcha.textproducer.font.size", "30"); // 设置字体 properties.setProperty("kaptcha.textproducer.font.names", "Blackletter,Hakuu,Tahoma"); // properties.setProperty("kaptcha.background.clear.from","153,255,204"); // properties.setProperty("kaptcha.background.clear.to","153,204,153"); // 验证码字符串 properties.setProperty("kaptcha.textproducer.char.string", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYAZ"); // 设置验证码长度 properties.setProperty("kaptcha.textproducer.char.length", CODE_LENGTH); // 图片干扰线:无干扰线 properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise"); DefaultKaptcha kaptcha = new DefaultKaptcha(); Config config = new Config(properties); kaptcha.setConfig(config); return kaptcha; }}
3、编写Controller类调用验证码功能
/* 单体应用项目*/ @Autowired private Producer kaptchaProducer; @GetMapping("/kaptcha") public void getKaptcha(HttpServletRequest req, HttpServletResponse resp){ //生成验证码 String text = kaptchaProducer.createText(); BufferedImage image = kaptchaProducer.createImage(text); //将验证码存放在session中 req.getSession().setAttribute("kaptcha",text); //将图片输出给浏览器 resp.setContentType("image/png"); try { ServletOutputStream os = resp.getOutputStream(); ImageIO.write(image, "png", os); } catch (IOException e) { throw new RuntimeException("响应验证码失败:" + e.getMessage()); } }/* 前后端分离项目*/@RestController@CrossOriginpublic class KaptchaController { @Autowired private Producer defaultKaptcha; @GetMapping("/createImageCode") public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { byte[] captchaOutputStream = null; ByteArrayOutputStream imgOutputStream = new ByteArrayOutputStream(); try { //生产验证码字符串并保存到session中 String verifyCode = defaultKaptcha.createText(); httpServletRequest.getSession().setAttribute("verifyCode", verifyCode); BufferedImage challenge = defaultKaptcha.createImage(verifyCode); ImageIO.write(challenge, "jpg", imgOutputStream); } catch (IllegalArgumentException e) { httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND); return; } captchaOutputStream = imgOutputStream.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(captchaOutputStream); responseOutputStream.flush(); responseOutputStream.close(); }}
4、前台页面实现(单体)
5、实现点击验证码切换功能
前后端分离
6、Kaptcha配置表
Constant | 描述 | 默认值 |
kaptcha.border | 图片边框,合法值:yes , no | yes |
kaptcha.border.color | 边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue. | black |
kaptcha.border.thickness | 边框厚度,合法值:>0 | 1 |
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 | 背景颜色渐变,开始颜色 | blue |
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 |
发表评论
最新留言
感谢大佬
[***.8.128.20]2025年03月26日 10时21分00秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
技术写作技巧分享:我是如何从写作小白成长为多平台优秀作者的?
2019-03-06
Jupyter Notebook 暗色自定义主题
2019-03-06
[Python学习笔记]组织文件
2019-03-06
DCL之单例模式
2019-03-06
什么?你竟然还没有用这几个chrome插件?
2019-03-06
将你的前端应用打包成docker镜像并部署到服务器?仅需一个脚本搞定
2019-03-06
【俗话说】换个角度理解TCP的三次握手和四次挥手
2019-03-06
基于Redo Log和Undo Log的MySQL崩溃恢复流程
2019-03-06
消息队列杂谈
2019-03-06
从RocketMQ的Broker源码层面验证一下这两个点
2019-03-06
如何正确的在项目中接入微信JS-SDK
2019-03-06
初探WebAssembly
2019-03-06
关于Objects类的getClass方法为什么可以得到子类的地址的思考
2019-03-06
239. 滑动窗口最大值
2019-03-06
Android | 教你如何开发一个证件照DIY小程序
2019-03-06