• java web 验证码生成


    先看看效果图:

    一般需求应该够用了,有线条干扰线,斑点,字体小幅度的旋转。。

    还没有与session挂钩上,所以只能用于简单的测试用例。

    直接上代码,看代码注释。。。

      1 package com.act262.demo;
      2 
      3 import java.awt.Color;
      4 import java.awt.Font;
      5 import java.awt.Graphics;
      6 import java.awt.Graphics2D;
      7 import java.awt.image.BufferedImage;
      8 import java.io.IOException;
      9 import java.io.OutputStream;
     10 import java.util.Random;
     11 
     12 import javax.imageio.ImageIO;
     13 import javax.servlet.ServletException;
     14 import javax.servlet.annotation.WebServlet;
     15 import javax.servlet.http.HttpServlet;
     16 import javax.servlet.http.HttpServletRequest;
     17 import javax.servlet.http.HttpServletResponse;
     18 
     19 import sun.java2d.loops.DrawLine;
     20 
     21 /**
     22  * 输出随机的验证码
     23  */
     24 @WebServlet({ "/CheckCode", "/checkCode.do" })
     25 public class CheckCodeServlet extends HttpServlet {
     26     private static final long serialVersionUID = 1L;
     27 
     28     /* 宽度 */
     29     private final int WIDTH = 100;
     30     /* 高度 */
     31     private final int HEIGHT = 20;
     32     /* 生成验证码的个数 */
     33     private final int COUNT = 4;
     34     /* 干扰线条数 */
     35     private final int LINE_ROW = 6;
     36 
     37     /* 输出的基本码表,如果使用中文,则使用utf-8的码表,类似 ue234 ,而且应该使用常用字,避免出现偏僻字 */
     38     private final char[] BASECODE = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
     39             'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
     40             'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
     41             'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
     42             'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7',
     43             '8', '9' };
     44     /* 随机数发生器 */
     45     private Random random;
     46     private BufferedImage image;
     47 
     48     // 写出数据
     49     private void write(OutputStream output) throws IOException {
     50         random = new Random();
     51 
     52         image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
     53         Graphics graphics = image.getGraphics();
     54 
     55         setBackground(graphics);
     56         drawBorder(graphics);
     57         drawDot(graphics);
     58         drawLine(graphics);
     59         drawString(graphics);
     60 
     61         ImageIO.write(image, "jpg", output);
     62     }
     63 
     64     //写字
     65     private void drawString(Graphics graphics) {
     66         StringBuffer sb = new StringBuffer();
     67         Font font = new Font("宋体", Font.BOLD, 18);
     68         graphics.setFont(font);
     69         graphics.setColor(Color.BLACK);
     70 
     71         for (int i = 0; i < COUNT; i++) {
     72             String ch = String
     73                     .valueOf(BASECODE[random.nextInt(BASECODE.length)]);
     74             sb.append(ch);
     75 
     76             // 设置位置
     77             int x = i * 20 + random.nextInt(12) + 10;
     78             int y = random.nextInt(HEIGHT / 3) + 12;
     79 
     80             // 旋转字体
     81             double theta = Math.PI / 180 * random.nextInt(20);
     82             // rotate(graphics, theta);
     83 
     84             graphics.drawString(ch, x, y);
     85 
     86             // 恢复。。
     87             // rotate(graphics, -theta);
     88         }
     89 
     90         System.out.println("验证码:" + sb.toString());
     91     }
     92 
     93     //旋转
     94     private void rotate(Graphics graphics, double theta) {
     95         ((Graphics2D) graphics).rotate(theta);
     96     }
     97 
     98     // 画随机线条
     99     private void drawLine(Graphics graphics) {
    100         for (int i = 0; i < LINE_ROW; i++) {
    101             int x1 = random.nextInt(WIDTH);
    102             int y1 = random.nextInt(HEIGHT);
    103             int x2 = random.nextInt(WIDTH);
    104             int y2 = random.nextInt(HEIGHT);
    105             setRandomColor(graphics);
    106             graphics.drawLine(x1, y1, x2, y2);
    107         }
    108     }
    109 
    110     // 画斑点
    111     private void drawDot(Graphics graphics) {
    112         graphics.setColor(Color.red);
    113         for (int i = 0; i < WIDTH; i++) {
    114             int x = i;
    115             int y = random.nextInt(HEIGHT);
    116             int r = random.nextInt(2);
    117             // graphics.fillOval(x, y, r, r);
    118             graphics.drawOval(x, y, r, r);
    119         }
    120     }
    121 
    122     // 画边框
    123     private void drawBorder(Graphics graphics) {
    124         graphics.setColor(Color.BLACK);
    125         graphics.drawRect(1, 1, WIDTH - 2, HEIGHT - 2);
    126     }
    127 
    128     // 设置背景
    129     private void setBackground(Graphics graphics) {
    130         graphics.setColor(Color.WHITE);
    131         graphics.fillRect(0, 0, WIDTH, HEIGHT);// 填充背景色
    132     }
    133 
    134     // 设置随机的画笔颜色
    135     private void setRandomColor(Graphics g) {
    136         g.setColor(new Color(random.nextInt(255), random.nextInt(255), random
    137                 .nextInt(255)));
    138     }
    139 
    140     protected void doGet(HttpServletRequest request,
    141             HttpServletResponse response) throws ServletException, IOException {
    142 
    143         response.setContentType("image/jpeg");
    144         response.setHeader("Expires", "-1");
    145         response.setHeader("Cache-Control", "no-cache");
    146 
    147         // 写出数据
    148         write(response.getOutputStream());
    149 
    150     }
    151 
    152     protected void doPost(HttpServletRequest request,
    153             HttpServletResponse response) throws ServletException, IOException {
    154         // TODO Auto-generated method stub
    155     }
    156 
    157 }

    不足的是,这个验证码数量与字体大小还有图片的大小之间的关系比较难搞,我是一点一点计算调试出来的,有些时候图片上的字体会跑位,这就是画图的难点了。如果修改了数量,字体大小,这些位置关系得重新计算才行。

    后续修改

    还是這个验证码

      1 package com.act262.demo;
      2 
      3 import java.awt.Color;
      4 import java.awt.Font;
      5 import java.awt.Graphics;
      6 import java.awt.Graphics2D;
      7 import java.awt.image.BufferedImage;
      8 import java.io.IOException;
      9 import java.io.OutputStream;
     10 import java.util.Random;
     11 
     12 import javax.imageio.ImageIO;
     13 import javax.servlet.ServletException;
     14 import javax.servlet.annotation.WebServlet;
     15 import javax.servlet.http.HttpServlet;
     16 import javax.servlet.http.HttpServletRequest;
     17 import javax.servlet.http.HttpServletResponse;
     18 import javax.servlet.http.HttpSession;
     19 
     20 import sun.java2d.loops.DrawLine;
     21 
     22 /**
     23  * 输出随机的验证码
     24  */
     25 @WebServlet({ "/CheckCode", "/checkCode.jpg" })
     26 public class CheckCodeServlet extends HttpServlet {
     27     private static final long serialVersionUID = 1L;
     28 
     29     /* 宽度 */
     30     private final int WIDTH = 100;
     31     /* 高度 */
     32     private final int HEIGHT = 20;
     33     /* 生成验证码的个数 */
     34     private final int COUNT = 4;
     35     /* 干扰线条数 */
     36     private final int LINE_ROW = 6;
     37 
     38     /* 输出的基本码表,如果使用中文,则使用utf-8的码表,类似 ue234 ,而且应该使用常用字,避免出现偏僻字 */
     39     private final char[] BASECODE = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
     40             'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
     41             'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
     42             'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
     43             'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7',
     44             '8', '9' };
     45 
     46     // 写出数据
     47     private void write(HttpServletRequest request, HttpServletResponse response)
     48             throws IOException {
     49         HttpSession session = request.getSession();
     50 
     51         BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
     52                 BufferedImage.TYPE_INT_RGB);
     53         Graphics graphics = image.getGraphics();
     54 
     55         setBackground(graphics);
     56         drawBorder(graphics);
     57         drawDot(graphics);
     58         drawLine(graphics);
     59         drawString(graphics, session);
     60 
     61         // 写出数据流
     62         ImageIO.write(image, "jpg", response.getOutputStream());
     63 
     64     }
     65 
     66     // 写字
     67     private void drawString(Graphics graphics, HttpSession session) {
     68         StringBuffer sb = new StringBuffer();
     69         Random random = new Random();
     70 
     71         graphics.setFont(new Font("宋体", Font.BOLD, 18));
     72         graphics.setColor(Color.BLACK);
     73 
     74         for (int i = 0; i < COUNT; i++) {
     75             String ch = String
     76                     .valueOf(BASECODE[random.nextInt(BASECODE.length)]);
     77             sb.append(ch);
     78 
     79             // 设置位置
     80             int x = i * 20 + random.nextInt(12) + 10;
     81             int y = random.nextInt(HEIGHT / 3) + 12;
     82 
     83             // 旋转字体
     84             double theta = Math.PI / 180 * random.nextInt(20);
     85             // rotate(graphics, theta);
     86 
     87             graphics.drawString(ch, x, y);
     88 
     89             // 恢复。。
     90             // rotate(graphics, -theta);
     91         }
     92         session.setAttribute("checkCode", sb.toString());
     93 
     94         System.out.println("session:" + session + "   验证码:" + sb.toString());
     95     }
     96 
     97     // 旋转
     98     private void rotate(Graphics graphics, double theta) {
     99         ((Graphics2D) graphics).rotate(theta);
    100     }
    101 
    102     // 画随机线条
    103     private void drawLine(Graphics graphics) {
    104         Random random = new Random();
    105         for (int i = 0; i < LINE_ROW; i++) {
    106             int x1 = random.nextInt(WIDTH);
    107             int y1 = random.nextInt(HEIGHT);
    108             int x2 = random.nextInt(WIDTH);
    109             int y2 = random.nextInt(HEIGHT);
    110             setRandomColor(graphics);
    111             graphics.drawLine(x1, y1, x2, y2);
    112         }
    113     }
    114 
    115     // 画斑点
    116     private void drawDot(Graphics graphics) {
    117         Random random = new Random();
    118         graphics.setColor(Color.red);
    119         for (int i = 0; i < WIDTH; i++) {
    120             int x = i;
    121             int y = random.nextInt(HEIGHT);
    122             int r = random.nextInt(2);
    123             // graphics.fillOval(x, y, r, r);
    124             graphics.drawOval(x, y, r, r);
    125         }
    126     }
    127 
    128     // 画边框
    129     private void drawBorder(Graphics graphics) {
    130         graphics.setColor(Color.BLACK);
    131         graphics.drawRect(1, 1, WIDTH - 2, HEIGHT - 2);
    132     }
    133 
    134     // 设置背景
    135     private void setBackground(Graphics graphics) {
    136         graphics.setColor(Color.WHITE);
    137         graphics.fillRect(0, 0, WIDTH, HEIGHT);// 填充背景色
    138     }
    139 
    140     // 设置随机的画笔颜色
    141     private void setRandomColor(Graphics g) {
    142         Random random = new Random();
    143         g.setColor(new Color(random.nextInt(255), random.nextInt(255), random
    144                 .nextInt(255)));
    145     }
    146 
    147     protected void doGet(HttpServletRequest request,
    148             HttpServletResponse response) throws ServletException, IOException {
    149 
    150         // 输出图片流的头信息
    151         response.setContentType("image/jpeg");
    152         response.setHeader("Expires", "-1");
    153         response.setHeader("Cache-Control", "no-cache");
    154 
    155         // 写出数据
    156         write(request, response);
    157 
    158     }
    159 
    160     protected void doPost(HttpServletRequest request,
    161             HttpServletResponse response) throws ServletException, IOException {
    162         // TODO Auto-generated method stub
    163     }
    164 
    165 }

    注册提交

    package com.act262.demo;
    
    import java.io.IOException;
    
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    /**
     * Servlet implementation class RegisterServlet
     */
    @WebServlet({ "/RegisterServlet", "/register.do" })
    public class RegisterServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        protected void doGet(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
            HttpSession session = request.getSession();
            String checkCode = (String) session.getAttribute("checkCode");
    
            String paramsName = request.getParameter("name");
            String paramsPwd = request.getParameter("pwd");
            String paramsCheckCode = request.getParameter("checkCode").trim();
    
            // do somethines
    
            //
            response.setContentType("text/html;charset=utf-8");
            if (checkCode.equalsIgnoreCase(paramsCheckCode)) {
                System.out.println("check code ok");
                response.getWriter().write(
                        "尊敬的 <bold>" + paramsName + "</bold>用户,您的密码为:" + paramsPwd
                                + "<br/>验证码 OK");
            } else {
                response.getWriter().write(
                        "尊敬的 <bold>" + paramsName + "</bold>用户,您的密码为:" + paramsPwd
                                + "<br/>验证码 not OK");
            }
        }
    
        protected void doPost(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    
    }

    jsp简单的界面

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    
    
    </head>
    <body>
    
        <div>
    
            <form action="register.do">
                <label>用户名:</label><input type="text" name="name" /></br> <label>密
                    码:</label><input type="password" name="pwd" /></br> <label>验证码:</label><input
                    type="text" name="checkCode" /><img src="checkCode.jpg"
                    onclick=flushCheckCode(this) alt="点击刷新验证码" style="cursor: hand" /></br>
                <input type="submit" value="注册" />
            </form>
    
        </div>
    
        <script type="text/javascript">
            function flushCheckCode(obj) {
                obj.src = (obj.src + '?' + new Date())
            }
        </script>
    </body>
    </html>

    這样子还是可以得,哈哈。。

  • 相关阅读:
    SASS(Syntactically Awesome Stylesheets Sass)绝对新手入门教程 java程序员
    android不同Activity之间的数据共享 java程序员
    响应式的前端框架 Groundwork java程序员
    分享网页加载速度优化的一些技巧? java程序员
    超棒的微软Metro风格Logo设计 java程序员
    删除DataTable中除指定行以外的行
    C#递归计算树形菜单 小小西
    记录我的不足一个周【当做故事看,我经常就是在圆子里找故事看的!】
    报告论文:程控交换技术的研究
    技巧心得:VBS学习心得~~
  • 原文地址:https://www.cnblogs.com/act262/p/4104387.html
Copyright © 2020-2023  润新知