Servlet 验证码

http://blog.csdn.net/ayhlay/article/details/13625275

1.验证码及其作用

验证码:就是将一串随机产生的数字或符号,生成一幅图片, 图片里加上一些干扰象素(防止OCR),由用户肉眼识别其中的验证码信息,输入表单提交网站验证,验证成功后才能使用某项功能。

作用:验证码一般是防止有人利用机器人自动批量注册、对特定的注册用户用特定程序暴力破解方式进行不断的登陆、灌水。因为验证码是一个混合了数字或符号的图片,人眼看起来都费劲,机器识别起来就更困难。像百度贴吧未登录发贴要输入验证码大概是防止大规模匿名回帖的发生。

开发步骤

1、开发servletpackage com.controller;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class SafeCode extends HttpServlet {public static final char[] CHARS = { '1', '2', '3', '4', '5', '6', '7','8', '9', 'k', 'A', 'Q', 'x', 'E', 'R', 'T', 'G', 'D', 'S', 'W','G', 'H', 'C', 'B', 'a', 'w', 'e', 'r', 't', 'd', 'F' };// 随机字符的字典public static Random random = new Random();// 随机数public static String getRandomString() {// 字符的缓存StringBuffer buf = new StringBuffer();for (int i = 0; i <4; i++) {// 循环 六次buf.append(CHARS[random.nextInt(CHARS.length)]);}return buf.toString();}public static Color getRandomColor() {return new Color(random.nextInt(255), random.nextInt(255),random.nextInt(255));}public static Color getReverseColor(Color c) {return new Color(255 - c.getRed(), 255 - c.getGreen(),255 - c.getBlue());}public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("image/jpeg");// 设置输出的类型String randomString = getRandomString();// 得到返回的字符集request.getSession(true).setAttribute("randomString", randomString);int with = 80;int hight =36;// 生成图片的大小Color color = getRandomColor();// 用于背景色Color reverse = getReverseColor(color);// 用于前景色BufferedImage bi = new BufferedImage(with, hight,BufferedImage.TYPE_INT_RGB);// 创建一个彩色的图片Graphics2D g = bi.createGraphics();// 获取到绘图对象g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 18));// 设置字体g.setColor(color);// 设置颜色g.fillRect(0, 0, with, hight);// 绘制背景g.setColor(reverse);// 设置颜色g.drawString(randomString, 18, 25);// 绘制随机字符for (int i = 0, n = random.nextInt(18); i < n; i++) {// 画最多100个噪音点g.drawRect(random.nextInt(with), random.nextInt(hight), 1, 1);// 随机噪音点}ServletOutputStream out = response.getOutputStream();// 转换图片格式JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);encoder.encode(bi);// 对图片进行编码out.flush();// 输出}protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {doGet(req, resp);}}

2、在jsp页面中开发<input id="yzm"

name="yzm" type="text" class="form-control x164 in"><img alt="验证码" src="SafeCode" id="Identity">

请填写图片中的字符,不区分大小写<a href="javascript:go()">看不清楚?换张图片</a><script type="text/javascript">function go(){

document.getElementById("Identity").src = 'SafeCode?dddd='

+ new Date().getTime();</script>

}3开发servletString jl = request.getParameter("jl");String yzm = request.getParameter("yzm");String randomString = (String)request.getSession().getAttribute("randomString");

if(yzm.equalsIgnoreCase(randomString)){out.print("验证通过:"+user+"

"+jl);}else{out.print("验证非法!!!!");}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,200评论 19 139
  • 小编费力收集:给你想要的面试集合 1.C++或Java中的异常处理机制的简单原理和应用。 当JAVA程序违反了JA...
    八爷君阅读 4,712评论 1 114
  • 在探索架构、微服务、Spring Cloud之路上发现的好文,特收录于此以便自己查阅,也望能为同路中人提供帮助。 ...
    简单的土豆阅读 1,214评论 0 16
  • 一天又匆忙的过去了 没有时间缅怀 没有时间遗憾 躺在床上久久不舍道晚安 似乎还有许多的书要看 许多的话要说 许多的...
    爱蔻严玲阅读 209评论 0 1
  • 公司里总有那么几个人,做起事来认真负责,也总有那些偷奸耍滑之辈,事到临头脚底抹油,溜之大吉。 年前年后,似乎已经有...
    少年长志阅读 517评论 0 0