网友写的验证码生成方案,可防止绝大多数机械识别。_.NET_编程开发_程序员俱乐部
网友写的验证码生成方案,可防止绝大多数机械识别。
- 摘要:web.xmlXml代码<!DOCTYPEweb-appPUBLIC"-//SunMicrosystems,Inc.//DTDWebApplication2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd"><web-app><!--********************************************--><!-
- 标签:
web.xml
class="Apple-converted-space">
Xml代码
- <!DOCTYPE web-app PUBLIC
- "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
- "http://java.sun.com/dtd/web-app_2_3.dtd" >
-
- <web-app>
-
-
-
- <servlet>
- <servlet-name>CaptchaServlet</servlet-name>
- <servlet-class>com.lowca.activity.web.servlet.CaptchaServlet</servlet-class>
- <init-param>
- <param-name>width</param-name>
- <param-value>150</param-value>
- </init-param>
- <init-param>
- <param-name>height</param-name>
- <param-value>100</param-value>
- </init-param>
- <init-param>
- <param-name>bgColor</param-name>
- <param-value>255,255,255</param-value>
- </init-param>
- <init-param>
- <param-name>fontColor</param-name>
- <param-value>0,255,0</param-value>
- </init-param>
- <init-param>
- <param-name>fontSize</param-name>
- <param-value>44</param-value>
- </init-param>
- <init-param>
- <param-name>fontCount</param-name>
- <param-value>4</param-value>
- </init-param>
- <init-param>
- <param-name>fontName</param-name>
- <param-value>宋体</param-value>
- </init-param>
- <init-param>
- <param-name>chars</param-name>
-
- <param-value>ABCDEFGHJKLMNPQRSTUVWXYabcdefghjkmnpqrstuvwxy3456789</param-value>
- </init-param>
- <init-param>
- <param-name>sessionKey</param-name>
- <param-value>com.lowca.activity.captcha</param-value>
- </init-param>
- <init-param>
- <param-name>maxRotateAngle</param-name>
- <param-value>40</param-value>
- </init-param>
- <init-param>
- <param-name>fontPadding</param-name>
- <param-value>-15</param-value>
- </init-param>
- </servlet>
-
- <servlet-mapping>
- <servlet-name>CaptchaServlet</servlet-name>
- <url-pattern>/captcha.jpg</url-pattern>
- </servlet-mapping>
- </web-app>
servlet类文件
Java代码
- package com.lowca.activity.web.servlet;
-
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics2D;
- import java.awt.geom.AffineTransform;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
-
- 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 org.apache.commons.lang.StringUtils;
- import org.apache.commons.lang.math.NumberUtils;
-
- public class CaptchaServlet extends HttpServlet {
-
- private static final long serialVersionUID = 1L;
-
- private static final String CONTENT_TYPE = "image/jpeg";
-
-
-
- private int width = 200;
-
- private int height = 150;
-
- private Color bgColor = Color.WHITE;
-
- private Color fontColor = Color.GREEN;
-
- private int fontSize = 42;
-
- private int fontCount = 4;
-
- private int fontPadding = -15;
-
- private int maxRotateAngle = 40;
-
- private String fontName = "宋体";
-
- private String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
-
- private String sessionKey = "com.lowca.activity.captcha";
-
-
-
- private Font font;
-
- private char[] seedArray;
-
- private int offsetX = 0;
-
- private int offsetY = 0;
-
-
- protected void service(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, java.io.IOException {
- BufferedImage buffImg = new BufferedImage(width, height,
- BufferedImage.TYPE_INT_RGB);
- Graphics2D g2d = buffImg.createGraphics();
- g2d.setColor(bgColor);
- g2d.fillRect(0, 0, width, height);
- g2d.setPaint(fontColor);
- g2d.setFont(font);
-
-
- StringBuffer randCode = new StringBuffer();
- for (int i = 0; i < fontCount; i++) {
- g2d.setStroke(new BasicStroke((float) (Math.random() * 5)));
- double angle = Math.random() * maxRotateAngle * Math.PI / 180;
- int p = (int) (Math.random() * chars.length());
- String word = String.valueOf(seedArray[p]);
- randCode.append(word);
- int x = offsetX + i * (fontSize + fontPadding);
-
- AffineTransform origXform = g2d.getTransform();
- AffineTransform newXform = (AffineTransform) (origXform.clone());
- newXform.rotate(angle, x, offsetY);
- g2d.setTransform(newXform);
- g2d.drawString(word, x, offsetY);
- g2d.setTransform(origXform);
- }
- g2d.setStroke(new BasicStroke(2.0f));
- int x = offsetX - (int) (20 * Math.random());
- int y = offsetY - (int) (20 * Math.random());
- int w = (int) (width - 10 - Math.random() * 6);
- int h = (int) (height - 10 - Math.random() * 6);
- int startAngle = (int) (Math.random() * 16 + 45);
- int arcAngle = (int) (Math.random() * 45 + 90);
- g2d.drawArc(x, y, w, h, startAngle, arcAngle);
- g2d.dispose();
-
-
- HttpSession session = req.getSession();
- session.setAttribute(sessionKey, randCode);
-
-
- resp.setHeader("Prama", "no-cache");
- resp.setHeader("Coche-Control", "no-cache");
- resp.setDateHeader("Expires", 0);
-
-
- ServletOutputStream out = resp.getOutputStream();
- ImageIO.write(buffImg, "jpeg", out);
- out.close();
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType(CONTENT_TYPE);
- }
-
- public void init() throws ServletException {
-
- String param = getInitParameter("width");
- if (NumberUtils.isDigits(param)) {
- width = Integer.parseInt(param);
- }
- param = getInitParameter("height");
- if (NumberUtils.isDigits(param)) {
- height = Integer.parseInt(param);
- }
- param = getInitParameter("bgColor");
- if (param != null && param.matches("^\\d+,\\d+,\\d+$")) {
- bgColor = getColor(param);
- }
- param = getInitParameter("fontColor");
- if (param != null && param.matches("^\\d+,\\d+,\\d+$")) {
- fontColor = getColor(param);
- }
- param = getInitParameter("fontSize");
- if (NumberUtils.isDigits(param)) {
- fontSize = Integer.parseInt(param);
- }
- param = getInitParameter("maxRotateAngle");
- if (NumberUtils.isDigits(param)) {
- maxRotateAngle = Integer.parseInt(param);
- }
- param = getInitParameter("fontPadding");
- if (param != null && param.matches("-?\\d+")) {
- fontPadding = Integer.parseInt(param);
- }
- param = getInitParameter("fontName");
- if (StringUtils.isNotBlank(param)) {
- fontName = param;
- }
- param = getInitParameter("chars");
- if (StringUtils.isNotBlank(param)) {
- chars = param;
- }
- param = getInitParameter("sessionKey");
- if (StringUtils.isNotBlank(sessionKey)) {
- sessionKey = param;
- }
-
- font = new Font(fontName, Font.ITALIC, fontSize);
- seedArray = chars.toCharArray();
- offsetX = width / 2 - fontCount * fontSize / 2 - (fontCount - 1)
- * fontPadding / 2 - 8;
- offsetY = height / 2 + fontSize / 2 - 8;
- }
-
- private Color getColor(String rgb) {
- String[] array = rgb.split(",");
- return new Color(Integer.parseInt(array[0]),
- Integer.parseInt(array[1]), Integer.parseInt(array[2]));
- }
-
- }
在我的机器上,这个程序每秒钟可以产生大约83张验证码。验证码图样如下
- 大小: 2.6 KB
- 大小: 2.5 KB
- 大小: 2.4 KB
- 大小: 2.4 KB
-
相关文章