1.引入 kaptcha 的 Maven 依赖

  1. <dependency>
  2. <groupId>com.github.penggle</groupId>
  3. <artifactId>kaptcha</artifactId>
  4. <version>2.3.2</version>
  5. </dependency>

2.KaptchaConfig 配置文件

  1. @Component
  2. public class KaptchaConfig {
  3. @Bean
  4. public DefaultKaptcha getDefaultKaptcha() {
  5. com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();
  6. Properties properties = new Properties();
  7. // 图片边框
  8. properties.setProperty("kaptcha.border", "yes");
  9. // 边框颜色
  10. properties.setProperty("kaptcha.border.color", "105,179,90");
  11. // 字体颜色
  12. properties.setProperty("kaptcha.textproducer.font.color", "red");
  13. // 图片宽
  14. properties.setProperty("kaptcha.image.width", "110");
  15. // 图片高
  16. properties.setProperty("kaptcha.image.height", "40");
  17. // 字体大小
  18. properties.setProperty("kaptcha.textproducer.font.size", "30");
  19. // session key
  20. properties.setProperty("kaptcha.session.key", "code");
  21. // 验证码长度
  22. properties.setProperty("kaptcha.textproducer.char.length", "4");
  23. // 字体
  24. properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
  25. Config config = new Config(properties);
  26. defaultKaptcha.setConfig(config);
  27.  
  28. return defaultKaptcha;
  29. }
  30. }

3.用户注册案例验证码验证(userCotroller)

  1. // 获取图形验证码
  2. @ApiOperation(value = "获取图形验证码", notes = "获取图形验证码")
  3. @RequestMapping("/defaultKaptcha")
  4. public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
  5. throws Exception {
  6. byte[] captchaChallengeAsJpeg = null;
  7. ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
  8. try {
  9. // 生产验证码字符串并保存到session中
  10. String createText = defaultKaptcha.createText();
  11. httpServletRequest.getSession().setAttribute("rightCode", createText);
  12. // 使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
  13. BufferedImage challenge = defaultKaptcha.createImage(createText);
  14. ImageIO.write(challenge, "jpg", jpegOutputStream);
  15. } catch (IllegalArgumentException e) {
  16. httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
  17. return;
  18. }
  19.  
  20. // 定义response输出类型为image/jpeg类型,使用response输出流输出图片的byte数组
  21. captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
  22. httpServletResponse.setHeader("Cache-Control", "no-store");
  23. httpServletResponse.setHeader("Pragma", "no-cache");
  24. httpServletResponse.setDateHeader("Expires", 0);
  25. httpServletResponse.setContentType("image/jpeg");
  26. ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
  27. responseOutputStream.write(captchaChallengeAsJpeg);
  28. responseOutputStream.flush();
  29. responseOutputStream.close();
  30.  
  31. }

4.验证码的验证(usercontroller)

  1. // 用户信息注册
  2. @ApiOperation(value = "获取注册信息", notes = "获取注册信息")
  3. @PostMapping(value = "/registerinfo")
  4. @ResponseBody
  5. public Msg register(@RequestBody @Validated({ Insert.class }) User user, BindingResult bindingResult,
  6. HttpServletRequest request, HttpServletResponse response) {
  7.  
  8. if (bindingResult.hasErrors()) {
  9. return Msg.failure(bindingResult.getFieldError().getDefaultMessage());
  10. } else {
  11. //获取前端发来的验证码
  12. String tryCode = user.tryCode;
  13. //获取生成的验证码
  14. String rightCode = (String) request.getSession().getAttribute("rightCode");
  15. //比较两个验证码
  16. if (rightCode.equals(tryCode)) {
  17. try {
  18. return userSevice.insert(user);
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. return Msg.failure("注册失败,服务器忙,请稍后重试");
  22. }
  23. } else {
  24. return Msg.failure("验证码错误");
  25. }
  26. }
  27.  
  28. }

5.用户注册页面

  1. <form class="layui-form" action="" style="padding: 20px;" lay-filter="addDialogForm">
  2. <div class="layui-form-item">
  3. <label class="layui-form-label">用户名</label>
  4. <div class="layui-input-inline">
  5. <input type="text" name="uName" style="width:250px;" id="uName" placeholder="请输入用户名" autocomplete="off" class="layui-input">
  6. </div>
  7. </div>
  8.  
  9. <div class="layui-form-item">
  10. <label class="layui-form-label">密码</label>
  11. <div class="layui-input-inline">
  12. <input type="password" name="uPwd" style="width:250px;" id="uPwd" placeholder="请输入密码" autocomplete="off" class="layui-input">
  13. </div>
  14. </div>
  15.  
  16. <div class="layui-form-item">
  17. <label class="layui-form-label">邮箱</label>
  18. <div class="layui-input-inline">
  19. <input type="text" name="uEmail" style="width:250px;" id="uEmail" placeholder="请输入邮箱" autocomplete="off" class="layui-input">
  20. </div>
  21. </div>
  22.  
  23. <div class="layui-form-item">
  24. <label class="layui-form-label">手机号</label>
  25. <div class="layui-input-inline">
  26. <input type="text" name="uTel" id="uTel" style="width:250px;" placeholder="请输入手机号" autocomplete="off" class="layui-input">
  27. </div>
  28. </div>
  29.  
  30. <div class="layui-form-item">
  31. <label class="layui-form-label">权限</label>
  32. <div class="layui-input-block">
  33. <input type="radio" name="uRank" id="uRank1" value="管理员" title="管理员" checked>
  34. <input type="radio" name="uRank" id="uRank2" value="普通用户" title="普通用户">
  35. </div>
  36. </div>
  37.  
  38. <div>
  39. <div style="float:left; margin-right:5px;" >
  40. <label class="layui-form-label">验证码</label>
  41. <input type="text" name="tryCode" id="tryCode" style="width:135px;" placeholder="请输入验证码" autocomplete="off" class="layui-input">
  42. </div>
  43.  
  44. <div style="float:left ">
  45. <!-- 后面添加参数起到清除缓存作用 -->
  46. <img alt="验证码"
  47. onclick="this.src='/defaultKaptcha?d='+new Date()*1"
  48. src="/defaultKaptcha" />
  49. </div>
  50. </div>
  51.  
  52. </form>

6.效果

7.以上参考https://blog.csdn.net/larger5/article/details/79522105

SpringBoot + kaptcha 生成、校对 验证码的更多相关文章

  1. kaptcha生成java验证码

    kaptcha工作的原理是调用 com.google.code.kaptcha.servlet.KaptchaServlet,生成一个图片.同时将生成的验证码字符串放到 HttpSession中. 1 ...

  2. Spring Boot快速集成kaptcha生成验证码

    Kaptcha是一个非常实用的验证码生成工具,可以通过配置生成多样化的验证码,以图片的形式显示,从而无法进行复制粘贴:下面将详细介绍下Spring Boot快速集成kaptcha生成验证码的过程. 本 ...

  3. JAVA整合kaptcha生成验证码 (字母验证码和算术验证码)

    引入maven <!--图片验证码--> <dependency> <groupId>com.github.penggle</groupId> < ...

  4. Springboot +redis+⾕歌开源Kaptcha实现图片验证码功能

    Springboot +redis+⾕歌开源Kaptcha实现图片验证码功能 背景 注册-登录-修改密码⼀般需要发送验证码,但是容易被 攻击恶意调⽤ 什么是短信-邮箱轰炸机 手机短信轰炸机是批.循环给 ...

  5. Sping mvc 环境下使用kaptcha 生成验证码

    一.kaptcha 的简介 kaptcha 是一个非常实用的验证码生成工具.有了它,你可以生成各种样式的验证码,因为它是可配置的.kaptcha工作的原理是调用 com.google.code.kap ...

  6. 使用kaptcha生成验证码

    原文:http://www.cnblogs.com/xdp-gacl/p/4221848.html kaptcha是一个简单好用的验证码生成工具,通过配置,可以自己定义验证码大小.颜色.显示的字符等等 ...

  7. 转】使用kaptcha生成验证码

    原博文出自于: http://www.cnblogs.com/xdp-gacl/p/4221848.html 感谢! kaptcha是一个简单好用的验证码生成工具,通过配置,可以自己定义验证码大小.颜 ...

  8. 利用kaptcha生成验证码的详细教程

    kaptcha是一个简单好用的验证码生成工具,有了它,你可以生成各种样式的验证码,因为它是可配置的.kaptcha工作的原理是调用 com.google.code.kaptcha.servlet.Ka ...

  9. Spring MVC 使用kaptcha生成验证码

    Spring MVC 使用kaptcha生成验证码 1.下载kaptcha-2.3.2.jar(或直接通过该文章附件下载) http://code.google.com/p/kaptcha/downl ...

随机推荐

  1. python调用dll详解

    参考链接https://www.cnblogs.com/TQCAI/p/8881530.html https://www.jb51.net/article/52513.htm https://www. ...

  2. CentOS 7 Docker 安装

    CentOS Docker 安装 Docker支持以下的CentOS版本: CentOS 7 (64-bit) CentOS 6.5 (64-bit) 或更高的版本 本文以 CentOS 7.6 版本 ...

  3. Scratch少儿编程系列:(十)系列总结及后续计划

    一.系列文章的来由 本篇为该系列文章的一个简单总结, 从初次接触Scratch开始,在写本系列文章过程中,一边读书,一边通过例子做练习. 技术实现,对于我跟人来说,没有什么难度. 我相信,对于一个初次 ...

  4. angular - ngFor, trackby

    ngFor ngForOf指令通常使用缩写形式*ngFor为集合中的每个项呈现模板的结构指令.该指令放置在元素上,该元素将成为克隆模板的父级. <li *ngFor="let item ...

  5. Linux中安装配置KVM虚拟化

    KVM 概述: KVM 即 Kernel-based Virtual Machine 基于内核的虚拟机. KVM,是一个开源的系统虚拟化模块,自 Linux 2.6.20 之后集成在 Linux 的各 ...

  6. show slave status参数详解

    root@localhost (none)>show slave status\G *************************** 1. row ******************** ...

  7. C语言作业11

    问题 答案 这个作业属于那个课程 C语言程序设计 这个作业要求在哪里 https://www.cnblogs.com/galen123/p/11996995.html 我在这个课程的目标是 在学好C语 ...

  8. hive查询结果保存

    参考: https://blog.csdn.net/zhuce1986/article/details/39586189 一.保存结果到本地 方法1:调用hive标准输出,将查询结果写到指定的文件中 ...

  9. spring boot-4.配置文件

    官方文档的23.4章节介绍了关于配置文件的内容 springboot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的 ...

  10. (5.1.5)引擎管理——多服务器管理之中央管理服务器(CMS)

    关键词:中央管理服务器,CMS,多服务器管理 中央管理服务器 -[1]打开 视图->已注册的服务器 [2]注册中央管理服务器 右击中央管理器->注册中央管理服务器 这里输入IP.主机名都可 ...