springmvc后台生成验证码
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后台生成验证码的更多相关文章
- C#后台生成验证码
https://www.cnblogs.com/vchenpeng/archive/2013/05/12/3074887.html /// <summary> /// 获 ...
- java生成验证码结合springMVC
在用户登录的时候,为了防止机器人攻击都会设置输入验证码,本篇文章就是介绍java如何生成验证码并使用在springMVC项目中的. 第一步:引入生成图片验证码的工具类 import java.awt. ...
- SpringMvc项目中使用GoogleKaptcha 生成验证码
前言:google captcha 是google生成验证码的一个工具类,其原理是将随机生成字符串保存到session中,同时以图片的形式返回给页面,之后前台页面提交到后台进行对比. 1.jar包准备 ...
- PHP-仿ecshop生成验证码
<?php /* 生成验证码 */ // 1.创建画布(基于已有图像) $n = mt_rand(1,5); $im = imagecreatefromjpeg('./images/captch ...
- Asp.net mvc生成验证码
1.生成验证码类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- Sping mvc 环境下使用kaptcha 生成验证码
一.kaptcha 的简介 kaptcha 是一个非常实用的验证码生成工具.有了它,你可以生成各种样式的验证码,因为它是可配置的.kaptcha工作的原理是调用 com.google.code.kap ...
- 【开发技术】Java生成验证码
Java生成验证码 为了防止用户恶意,或者使用软件外挂提交一些内容,就得用验证码来阻止,虽然这个会影响用户体验,但为了避免一些问题很多网站都使用了验证码;今天下午参考文档弄了一个验证码,这里分享一下; ...
- spring mvc 使用kaptcha配置生成验证码实例
SpringMVC整合kaptcha(验证码功能) 一.依赖 <dependency> <groupId>com.github.penggle</groupId> ...
- Dede织梦验证码不显示,织梦后台登陆验证码不显示解决方法
关于"织梦验证码不显示"的解决方法 "织梦验证码无法显示出来"的问题分析? 1.之前显示正常,但是换了服务器后就不能够正常显示:(这种通常是网站程序经过迁移后所 ...
随机推荐
- C/C++基础----泛型算法
算法不依赖与容器(使用迭代器),但大多数依赖于元素类型.如find需要==运算符,其他算法可能要求支持<运算符. 算法永远不会执行容器的操作,永远不会改变底层容器的大小(添加或删除元素). ac ...
- POJ 1182 食物链(并查集的使用)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 81915 Accepted: 24462 Description ...
- 关于wp数据备份
差点忘记记录,之前一直用wp数据备份,但是一直发送邮件不成功,后来才发现,原来是因为发送的邮箱没有设置开通那个什么pop鬼的,不记得叫什么了,看下图. 开通这个服务,就可以正常接收wp的数据邮件备份了 ...
- mysql存储过程中遍历数组字符串的两种方式
第一种:多次使用substring_index()的方法 DELIMITER $$ DROP PROCEDURE IF EXISTS `array`$$ CREATE PROCEDURE `arra ...
- KCP 传输协议
作者:韦易笑链接:https://www.zhihu.com/question/36258781/answer/98944369来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- FDD vs TDD
双工方式 FDD vs TDD 频分双工(FDD) 收发信各占用一个频率. 优点是收.发信号同时进行,时延小,技术成熟,缺点是设备成本高. 时分双工(TDD) 收发信使用同一个频率,但使用不同 ...
- sqoop产生背景及概述
sqoop产生背景 多数是用Hadoop技术处理大数据业务的企业有大量的数据存储在传统的关系型数据库(RDBMS)中:由于缺乏工具的支持.对Hadoop和传统数据库系统中的数据进行相互传输是一件十分困 ...
- 异常处理的设计与重构 pdf
百度网盘: https://pan.baidu.com/s/1hsQIEGk
- 详解Tomcat配置及使用
2018年06月27日 23:42:34 尘埃丶落定 阅读数:2351 版权声明:本文为博主原创文章,转载请附上作者与出处. https://blog.csdn.net/longyin0528/ ...
- distinct group by
select num from test_test group by num; 比 select distinct(num) from test_test; 效率高 select count(dis ...