url

http://localhost:8080/admin/getCode
http://localhost:8080/admin/checkCode

controller

package com.blaze.controller;

import com.blaze.util.CodeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException; /**
* create by zy 2019/4/11 9:17
* TODO:
*/
@Controller
@RequestMapping("/admin")
public class CodeController { @RequestMapping("/getCode")
public void getCode(HttpServletRequest req, HttpServletResponse resp) throws IOException {
CodeUtil.getCode(req, resp);
HttpSession session = req.getSession();
System.out.println("------------" + session.getAttribute("code") + "------------");
} @RequestMapping("/checkCode")
public void checkCode(HttpServletRequest req, HttpServletResponse resp) throws IOException {
HttpSession session = req.getSession();
System.out.println("************" + session.getAttribute("code") + "************");
System.out.println(req.getParameter("code"));
} }

util

package com.blaze.util;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random; /**
* create by zy 2019/4/11 8:54
* TODO:
*/
public class CodeUtil {
private CodeUtil() {
} private static int width = 90;// 定义图片的width
private static int height = 20;// 定义图片的height
private static int codeCount = 4;// 定义图片上显示验证码的个数
private static int xx = 15;
private static int fontHeight = 18;
private static int codeY = 16;
static char[] codeSequence = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; public static void getCode(HttpServletRequest req, HttpServletResponse resp) throws IOException {
// 定义图像buffer
BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics gd = buffImg.getGraphics();
// 创建一个随机数生成器类
Random random = new Random();
// 将图像填充为白色
gd.setColor(Color.WHITE);
gd.fillRect(0, 0, width, height);
// 创建字体,字体的大小应该根据图片的高度来定。
Font font = new Font("Fixedsys", Font.BOLD, fontHeight);
// 设置字体。
gd.setFont(font);
// 画边框。
gd.setColor(Color.BLACK);
gd.drawRect(0, 0, width - 1, height - 1);
// 随机产生40条干扰线,使图象中的认证码不易被其它程序探测到。
gd.setColor(Color.BLACK);
for (int i = 0; i < 40; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
gd.drawLine(x, y, x + xl, y + yl);
}
// randomCode用于保存随机产生的验证码,以便用户登录后进行验证。
StringBuffer randomCode = new StringBuffer();
int red = 0, green = 0, blue = 0;
// 随机产生codeCount数字的验证码。
for (int i = 0; i < codeCount; i++) {
// 得到随机产生的验证码数字。
String code = String.valueOf(codeSequence[random.nextInt(codeSequence.length - 1)]);
// 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);
// 用随机产生的颜色将验证码绘制到图像中。
gd.setColor(new Color(red, green, blue));
gd.drawString(code, (i + 1) * xx, codeY);
// 将产生的四个随机数组合在一起。
randomCode.append(code);
}
// 将四位数字的验证码保存到Session中。
HttpSession session = req.getSession();
//System.out.print(randomCode);
session.setAttribute("code", randomCode.toString());
// 禁止图像缓存。
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0);
resp.setContentType("image/jpeg");
// 将图像输出到Servlet输出流中。
ServletOutputStream sos = resp.getOutputStream();
ImageIO.write(buffImg, "jpeg", sos);
sos.close();
}
}

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. 杂项:BugFree

    ylbtech-杂项:BugFree BugFree是借鉴微软的研发流程和Bug管理理念,使用PHP+MySQL独立写出的一个Bug管理系统.简单实用.免费并且开放源代码(遵循GNU GPL). 命名 ...

  2. java study文件读写

    文件读写 如果在代码中写入大量的数据,会增加代码的冗余度,通过读取文件的方式,可以精简代码,便于数据的修改和代码的维护 IO流的分类:字节流和字符流 字符流 字符输出流:写文本文件的,抽象基类java ...

  3. Nginx的启动、停止、重启

    启动 启动代码格式:nginx安装目录地址 -c nginx配置文件地址 例如: [root@LinuxServer sbin]# /usr/local/nginx/sbin/nginx -c /us ...

  4. Windows 8的用户模式Shim Engine小探及利用

    转载: https://bbs.pediy.com/thread-175483.htm Windows Shim Engine,即Windows 兼容性模式实现引擎,在exe文件的属性对话框中有一个兼 ...

  5. ECCV 2018 | 给Cycle-GAN加上时间约束,CMU等提出新型视频转换方法Recycle-GAN

    CMU 和 Facebook 的研究者联合进行的一项研究提出了一种新型无监督视频重定向方法 Recycle-GAN,该方法结合了时间信息和空间信息,可实现跨域转换,同时保留目标域的风格.相较于只关注空 ...

  6. js常见兼容

    滚动条的兼容写法(谷歌chrome) document.documentElement.scrollTop || document.body.scrollTop   阻止浏览器默认行为的兼容写法  e ...

  7. IIS6.0 IIS7.5应用程序池自动停止的解决方法 搜集整理

    来源:http://www.guchengnet.com/1499.html IIS6.0 IIS7.5应用程序池自动停止的解决方法 搜集整理 发表于2016年12月14日 有2.3个月没有用本地的i ...

  8. linux更换shell外壳zsh

    linux-外壳内核与shell的关系 内核处于外壳之中,通过外壳与shell(命令行)交互 外壳可以更换 ############################################## ...

  9. Mysql-两表的连接,copy表,select的各种用法

    -- 连接:外连接,内连接 两个表之间 外连接:right join    left join -- left join 左标为主 一般以值少的为主 select * from table1 left ...

  10. mock单测

    mockMvc执行流程总结: 整个过程:1.mockMvc.perform执行一个请求:2.MockMvcRequestBuilders.get("/user/1")构造一个请求3 ...