JAVA工具类(15)----验证码工具类的创建、配置、使用及后台验证
发布日期:2021-06-30 22:41:07
浏览次数:2
分类:技术文章
本文共 4615 字,大约阅读时间需要 15 分钟。
1、创建
package com.gcloud.common;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder; import org.apache.commons.lang.StringUtils;import javax.imageio.ImageIO;
import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; 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.util.Random;/**
* 验证码 * Created by charlin on 2017/9/10. */ public class ValidateCodeServlet extends HttpServlet { private static final long serialVersionUID = 1L; public static String VALIDATE_CODE = “validateCode”; // 验证码图片的宽度。 private int width = 60; // 验证码图片的高度。 private int height = 20; // 验证码字符个数 private int codeCount = 4;// 字体高度private int fontHeight;private int codeX = 0;private int codeY;//char[] codeSequence = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };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'};/** * 初始化验证图片属性 * * @throws ServletException */public void init() throws ServletException { // 宽度 String strWidth = this.getInitParameter("width"); // 高度 String strHeight = this.getInitParameter("height"); // 字符个数 String strCodeCount = this.getInitParameter("codeCount"); try { if (StringUtils.isEmpty(strWidth)) width = Integer.parseInt(strWidth); if (StringUtils.isEmpty(strHeight)) height = Integer.parseInt(strHeight); if (StringUtils.isEmpty(strCodeCount)) codeCount = Integer.parseInt(strCodeCount); } catch (NumberFormatException e) { e.printStackTrace(); } fontHeight = height - 2; codeX = width / (codeCount + 1); codeY = height - 4;}protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { //1、定义图像buffer BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //2、创建图形 Graphics2D g = buffImg.createGraphics(); //3、创建一个随机数生成器类 Random random = new Random(); //4、将图像填充为白色 g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); //5、创建字体,字体的大小应该根据图片的高度来定。 Font font = new Font("Fixedsys", Font.PLAIN, fontHeight); //6设置字体。 g.setFont(font); //7、画边框。 g.setColor(Color.BLACK); g.drawRect(0, 0, width - 1, height - 1); //8、随机产生160条干扰线,使图象中的认证码不易被其它程序探测到。 g.setColor(Color.BLACK); for (int i = 0; i < 3; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x, y, x + xl, y + yl); } //9、randomCode用于保存随机产生的验证码,以便用户登录后进行验证。 StringBuffer randomCode = new StringBuffer(); int red = 0, green = 0, blue = 0; //10、随机产生codeCount数字的验证码。 for (int i = 0; i
}
2、在web.xml配置------------
validateCode com.gcloud.common.ValidateCodeServlet 0 validateCode /validateCode.jpg
3、前台使用------
function randomcode_refresh() {
("#randomCode_img").attr('src','{baseurl}validateCode.jpg?time’ + new Date().getTime()); }``` 验证码: 刷新
4、在SSM框架后台验证
@RequestMapping("/login.action") public @ResponseBody SubmitResultInfo login(HttpServletRequest request, String randomCode) throws Exception { //session对象 HttpSession session = request.getSession(); return ResultUtil.createSubmitResult(ResultUtil.createWarning(Config.MESSAGE,112, null)); } if (session.getAttribute(ValidateCodeServlet.VALIDATE_CODE) == null) { return ResultUtil.createSubmitResult(ResultUtil.createWarning(Config.MESSAGE,112, null)); } String validateCode = session.getAttribute(ValidateCodeServlet.VALIDATE_CODE) .toString(); if (!randomcode.equals(validateCode)) { return ResultUtil.createSubmitResult(ResultUtil.createWarning(Config.MESSAGE,113, null)); } }
—————————————————————————————————————————————————–
java 架构师全套教程,共760G, 让你从零到架构师,每月轻松拿3万
下载地址:
https://item.taobao.com/item.htm?spm=686.1000925.0.0.4a155084hc8wek&id=555888526201
01.高级架构师四十二个阶段高
02.Java高级系统培训架构课程148课时 03.Java高级互联网架构师课程 04.Java互联网架构Netty、Nio、Mina等-视频教程 05.Java高级架构设计2016整理-视频教程 06.架构师基础、高级片 07.Java架构师必修linux运维系列课程 08.Java高级系统培训架构课程116课时 (送:hadoop系列教程,java设计模式与数据结构, Spring Cloud微服务, SpringBoot入门)01内容详情:
—————————————————————————————————————————————————–转载地址:https://lovoo.blog.csdn.net/article/details/77926306 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
表示我来过!
[***.240.166.169]2024年04月14日 22时40分54秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
leetcode 122 买卖股票的最佳时机II
2021-07-03
leetcode 309 最佳买卖股票含冷冻期
2021-07-03
leetcode 714 买卖股票的最佳时机含手续费
2021-07-03
leetcode3 无重复字符的最长子串
2021-07-03
leetcode 1143. 最长公共子序列
2021-07-03
leetcode 83. 删除排序链表中的重复元素
2021-07-03
Python 之 histogram直方图
2021-07-03
Python实现决策树 Desision Tree & 可视化
2021-07-03
决策树 Decision tree
2021-07-03
nominal和ordinal & 数据处理中四种基本数据类型
2021-07-03
Grid SearchCV(网格搜索)& Python实现
2021-07-03
单目深度估计 monodepth2模型 代码
2019-04-30
位图索引Bitmap indexes
2019-04-30
YOLO算法(二)—— Yolov2 & yolo9000
2019-04-30
Python的__future__模块
2019-04-30