url

  1. http://localhost:8080/admin/getCode
  2. http://localhost:8080/admin/checkCode

controller

  1. package com.blaze.controller;
  2.  
  3. import com.blaze.util.CodeUtil;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6.  
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import javax.servlet.http.HttpSession;
  10. import java.io.IOException;
  11.  
  12. /**
  13. * create by zy 2019/4/11 9:17
  14. * TODO:
  15. */
  16. @Controller
  17. @RequestMapping("/admin")
  18. public class CodeController {
  19.  
  20. @RequestMapping("/getCode")
  21. public void getCode(HttpServletRequest req, HttpServletResponse resp) throws IOException {
  22. CodeUtil.getCode(req, resp);
  23. HttpSession session = req.getSession();
  24. System.out.println("------------" + session.getAttribute("code") + "------------");
  25. }
  26.  
  27. @RequestMapping("/checkCode")
  28. public void checkCode(HttpServletRequest req, HttpServletResponse resp) throws IOException {
  29. HttpSession session = req.getSession();
  30. System.out.println("************" + session.getAttribute("code") + "************");
  31. System.out.println(req.getParameter("code"));
  32. }
  33.  
  34. }

util

  1. package com.blaze.util;
  2.  
  3. import javax.imageio.ImageIO;
  4. import javax.servlet.ServletOutputStream;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.HttpSession;
  8. import java.awt.*;
  9. import java.awt.image.BufferedImage;
  10. import java.io.IOException;
  11. import java.util.Random;
  12.  
  13. /**
  14. * create by zy 2019/4/11 8:54
  15. * TODO:
  16. */
  17. public class CodeUtil {
  18. private CodeUtil() {
  19. }
  20.  
  21. private static int width = 90;// 定义图片的width
  22. private static int height = 20;// 定义图片的height
  23. private static int codeCount = 4;// 定义图片上显示验证码的个数
  24. private static int xx = 15;
  25. private static int fontHeight = 18;
  26. private static int codeY = 16;
  27. static char[] codeSequence = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
  28. 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
  29.  
  30. public static void getCode(HttpServletRequest req, HttpServletResponse resp) throws IOException {
  31. // 定义图像buffer
  32. BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  33. Graphics gd = buffImg.getGraphics();
  34. // 创建一个随机数生成器类
  35. Random random = new Random();
  36. // 将图像填充为白色
  37. gd.setColor(Color.WHITE);
  38. gd.fillRect(0, 0, width, height);
  39. // 创建字体,字体的大小应该根据图片的高度来定。
  40. Font font = new Font("Fixedsys", Font.BOLD, fontHeight);
  41. // 设置字体。
  42. gd.setFont(font);
  43. // 画边框。
  44. gd.setColor(Color.BLACK);
  45. gd.drawRect(0, 0, width - 1, height - 1);
  46. // 随机产生40条干扰线,使图象中的认证码不易被其它程序探测到。
  47. gd.setColor(Color.BLACK);
  48. for (int i = 0; i < 40; i++) {
  49. int x = random.nextInt(width);
  50. int y = random.nextInt(height);
  51. int xl = random.nextInt(12);
  52. int yl = random.nextInt(12);
  53. gd.drawLine(x, y, x + xl, y + yl);
  54. }
  55. // randomCode用于保存随机产生的验证码,以便用户登录后进行验证。
  56. StringBuffer randomCode = new StringBuffer();
  57. int red = 0, green = 0, blue = 0;
  58. // 随机产生codeCount数字的验证码。
  59. for (int i = 0; i < codeCount; i++) {
  60. // 得到随机产生的验证码数字。
  61. String code = String.valueOf(codeSequence[random.nextInt(codeSequence.length - 1)]);
  62. // 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
  63. red = random.nextInt(255);
  64. green = random.nextInt(255);
  65. blue = random.nextInt(255);
  66. // 用随机产生的颜色将验证码绘制到图像中。
  67. gd.setColor(new Color(red, green, blue));
  68. gd.drawString(code, (i + 1) * xx, codeY);
  69. // 将产生的四个随机数组合在一起。
  70. randomCode.append(code);
  71. }
  72. // 将四位数字的验证码保存到Session中。
  73. HttpSession session = req.getSession();
  74. //System.out.print(randomCode);
  75. session.setAttribute("code", randomCode.toString());
  76. // 禁止图像缓存。
  77. resp.setHeader("Pragma", "no-cache");
  78. resp.setHeader("Cache-Control", "no-cache");
  79. resp.setDateHeader("Expires", 0);
  80. resp.setContentType("image/jpeg");
  81. // 将图像输出到Servlet输出流中。
  82. ServletOutputStream sos = resp.getOutputStream();
  83. ImageIO.write(buffImg, "jpeg", sos);
  84. sos.close();
  85. }
  86. }

springmvc后台生成验证码的更多相关文章

  1. C#后台生成验证码

    https://www.cnblogs.com/vchenpeng/archive/2013/05/12/3074887.html /// <summary>          /// 获 ...

  2. java生成验证码结合springMVC

    在用户登录的时候,为了防止机器人攻击都会设置输入验证码,本篇文章就是介绍java如何生成验证码并使用在springMVC项目中的. 第一步:引入生成图片验证码的工具类 import java.awt. ...

  3. SpringMvc项目中使用GoogleKaptcha 生成验证码

    前言:google captcha 是google生成验证码的一个工具类,其原理是将随机生成字符串保存到session中,同时以图片的形式返回给页面,之后前台页面提交到后台进行对比. 1.jar包准备 ...

  4. PHP-仿ecshop生成验证码

    <?php /* 生成验证码 */ // 1.创建画布(基于已有图像) $n = mt_rand(1,5); $im = imagecreatefromjpeg('./images/captch ...

  5. Asp.net mvc生成验证码

    1.生成验证码类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

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

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

  7. 【开发技术】Java生成验证码

    Java生成验证码 为了防止用户恶意,或者使用软件外挂提交一些内容,就得用验证码来阻止,虽然这个会影响用户体验,但为了避免一些问题很多网站都使用了验证码;今天下午参考文档弄了一个验证码,这里分享一下; ...

  8. spring mvc 使用kaptcha配置生成验证码实例

    SpringMVC整合kaptcha(验证码功能) 一.依赖 <dependency> <groupId>com.github.penggle</groupId> ...

  9. Dede织梦验证码不显示,织梦后台登陆验证码不显示解决方法

    关于"织梦验证码不显示"的解决方法 "织梦验证码无法显示出来"的问题分析? 1.之前显示正常,但是换了服务器后就不能够正常显示:(这种通常是网站程序经过迁移后所 ...

随机推荐

  1. 关于java泛型的使用方式。。。。

    转自:http://onewebsql.com/blog/generics-extends-super 以下基本够用了 Today we continue our mini-series on Jav ...

  2. bzoj1293 生日礼物

    Description 小西有一条很长的彩带,彩带上挂着各式各样的彩珠.已知彩珠有N个,分为K种.简单的说,可以将彩带考虑为x轴,每一个彩珠有一个对应的坐标(即位置).某些坐标上可以没有彩珠,但多个彩 ...

  3. 通过shell进行数学计算

    对于基本运算,可以使用let, $(())和$[] 对于高级运算,使用expr和bc这两个工具 [hupeng@hupeng-vm shell]$n1= [hupeng@hupeng-vm shell ...

  4. autoit 中文API

    中文API 参考地址: http://www.jb51.net/shouce/autoit/ 虫师的selelnium里面也有简单的说 环境搭建+上传弹窗的小案例

  5. eclipse JDK 下载 and 安装 and 环境配置

    eclipse和JDK软件下载 链接:https://pan.baidu.com/s/1bpRHVIhNtK9_FMVbi34YUQ 密码:y3xr eclipse和JDK这两个软件是配套使用的,适用 ...

  6. ZooKeeper 集群的安装、配置---Dubbo 注册中心

    ZooKeeper 集群的安装.配置.高可用测试 Dubbo 注册中心集群 Zookeeper-3.4.6 Dubbo 建议使用 Zookeeper 作为服务的注册中心. Zookeeper 集群中只 ...

  7. 中国标准时间改为formatTime格式

    1.toLocaleDateString (根据本地时间把Date 对象的日期部分转换为字符串): var time = new Date(); var formatTime = time.toLoc ...

  8. UVA-839-二叉树-一个有意思的题目

    题意: 一颗二叉树可以看成一个杠杆,左右俩边有重量,有到支点长度,判断整个树是否平衡(根据杠杆原理),如果当前结点有左孩子,那么当前左边的重量就是左孩子的总和,右边同理 递归,发现scanf和cin的 ...

  9. HTML5 Canvas ( 矩形的绘制 ) rect, strokeRect, fillRect

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. 任务调度的方式:Timer、ScheduledExecutorService、spring task、quartz、XXL-JOB、Elastic-Job

    任务调度 定时任务调度:基于给定的时间点.给定的时间间隔.给定的执行次数自动执行的任务. Timer 介绍 Timer,简单无门槛,一般也没人用. Timer位于java.util包下,其内部包含且仅 ...