Java Web应用设计中验证码的生成和应用方法

  在Java Web应用设计中验证码的设计是一个必不可少的环节,由于验证码技术具有随机性较强、简单的特点,能够在一定程度上阻止网络上的恶意访问,在互联网领域得到了广泛的应用,如防止破解密码、刷票、论坛灌水、刷页、注册等恶意操作。百度上对验证码的定义是:CAPTCHA“Completely Automated Public Turing test to tell Computers and Humans Apart”(全自动区分计算机和人类的图灵测试)的缩写,是一种区分用户是计算机还是人的公共全自动程序。很多Web应用系统或网站在注册登录的时候都需要验证码这个环节,验证码一般是随机产生的,有很大几率会出现无法清楚识别验证码得图片,所以一般都会有相应的提示,如“看不清,换一张”等,如果没有提示,则直接点击当前的验证码图片,就可以完成验证码的更换。

1. 生成验证码的Servlet程序(Servlet程序的Java类名:CheckCode )

package cn.hbqgy.tools;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

import java.io.IOException;

import java.io.OutputStream;

import java.io.PrintWriter;

import java.util.Random;

import javax.imageio.ImageIO;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

public class CheckCode extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        //定义验证码图片的宽和高

        int width = 95;

        int height = 32;

        //创建一个图片缓冲区,作为画布

        BufferedImage image =

                new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        //获取这个画布上画图的画笔

        Graphics2D g = (Graphics2D) image.getGraphics();

        //设置画笔的颜色背景色

        g.setColor(new Color(0xFAEBD7));

        //使用画笔绘制画布的背景

        g.fillRect(0, 0, width, height);

        //修改画笔颜色为黑色

        g.setColor(Color.black);

        //绘制画布边框

        g.drawRect(0, 0, width - 1, height - 1);

        //创建一个随机数生成对象

        Random r = new Random();

        //定义一个字符数组,作为验证码字符源

        String str="123456789";

        str=str+"qwertyuiopasdfghjklzxcvbnm";

        str=str+str.toUpperCase();

        char[] chars = str.toCharArray();

        // 在图片上生成干扰点

        for (int i = 0; i < 300; i++) {

            g.setColor(new Color(r.nextInt(150) + 50,

                    r.nextInt(150) + 50, r.nextInt(150) + 50));

            int x = r.nextInt(width);

            int y = r.nextInt(height);

            g.drawOval(x, y, 1, 1);

        }

        //创建一个字符串缓冲区,用来保存要输出到图片中的所有字符

        StringBuilder strBuilder = new StringBuilder();

        //循环往图片中写入字符

        for (int i = 0, left = 8; i < 4; i++, left += 21) {

            //从字符源中随机获取一个字符

            char ch = chars[r.nextInt(chars.length)];

            //将字符添加到字符串缓冲区中

            strBuilder.append(ch);

            //设置画笔颜色

            g.setColor(new Color(r.nextInt(100), r.nextInt(100), r.nextInt(100)));

            //设置字符的字体

            g.setFont(new Font("Arial", Font.BOLD, 24));

            //设置一个弧度

            double rot = Math.PI / 360 * (45 - r.nextInt(90));

            //旋转画笔输出的方向,旋转角度为上面获取的角度

            g.rotate(rot, left, height/2);

            //往画布中输出字符

            g.drawString(ch + "", left, 24);

            //将画笔的输出方向调整回来

            g.rotate(-rot, left, 24);

        }

        //释放画笔资源

        g.dispose();

        //将输出到画布中的字符保存到session

        HttpSession session = request.getSession();

        session.setAttribute("vCode", strBuilder.toString());

        //设置相应流的响应内容格式

        response.setContentType("image/jpeg");

        //将画布内容往响应流中输出

        //因为jsp页面执行完毕后会释放所有PageContestObject对象,并调用responsegetWriter方法,

        //response对象不能同时使用getWriter方法和getOutputStream()方法,

        //为了下面能够使用response对象的getOutputStream()方法获取输出字节流,往浏览器输出图片,

        //所以需要先对其进行处理

        //out.clear();

        //out = pageContext.pushBody();

        //获取返回给浏览器客户端的响应流

        try (OutputStream output = response.getOutputStream()) {

            //将画布内容往响应流中输出

            ImageIO.write(image, "jpeg", output);

        }

    }

    @Override

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        processRequest(request, response);

    }

    @Override

    protected void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        processRequest(request, response);

    }

    @Override

    public String getServletInfo() {

        return "Short description";

    }

}

2. 验证码显示和输入界面(JSP文件名:checkCode.jsp)

<%@page contentType="text/html;charset=gbk" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <title>JSP页面布局的例子程序--Form</title>

//css文件后加?v=1参数可以使客户端浏览器及时下载更新css样式,方便程序调试

        <link rel="stylesheet" type="text/css" href="css/form.css?v=1">

//实现验证码更新的JS程序

        <script type="text/javascript">

            function reload() {

                var vImage = document.getElementById("ckCode");

                var vDate=new Date();

                vImage.src="/Jsj-4/CheckCode?time="+vDate.getTime();

            }

        </script>

        <script type="text/javascript" src="js/form.js"></script>

    </head>

    <body>

        <table  class="formattable" align="center" >

            <form id="form1" name="form1" action="checkCodePro.jsp" method="post"  target="" align="center">

                <tr><th></th><th>学生登录界面</th><th></th></tr>

                <tr>

                    <td class="colleft">学号:</td>

                    <td class="colcenter"><input id="stuno" name="stuno" type="text" size="20"></td>

                    <td class="colright" >*必填项</td>

                </tr>

                <tr>

                    <td class="colleft">姓名:</td>

                    <td class="colcenter"><input id="stuname" name="stuname" type="text" size="20"></td>

                    <td class="colright" >*必填项</td>

                </tr>

                <tr>

                    <td class="colleft">密码:</td>

                    <td class="colcenter"><input id="password" name="password" type="password" size="20"></td>

                    <td class="colright"></td>

                </tr>

                <tr>

                    <td class="colleft">验证码:</td>

                    <td class="colcenter">

                        <input id="checkcode" name="checkcode" type="text" size="20">

                    </td>

                    <td class="colright">

                        <img height="22" id="ckCode" name="ckCode" src="/Jsj-4/CheckCode"  onclick="reload()">

                    </td>

                </tr>

                <tr>

                    <td class="colleft"></td>

                    <td align="center">

                      <input class="labelButton" id="submit" name="submit" type="submit" value="登录">

                        <input class="labelButton" id="reset" name="reset" type="reset" value="重置">

                    </td>

                    <td class="colright"></td>

                </tr>

            </form>

        </table>

    </body>

</html>

3.验证码验证程序(JSP文件名:checkCodePro.jsp)

<%@page contentType="text/html;charset=gbk" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <title>JSP应用验证码的例子</title>

    </head>

    <body>

        <%

            String stuno = request.getParameter("stuno");

            out.println("学号:"+stuno+"<br>");

            String stuname = request.getParameter("stuname");

            stuname=new String(stuname.getBytes("8859_1"),"gbk");

            out.println("学生姓名:"+stuname+"<br>");

            String password=request.getParameter("password");

            out.println("密码:"+password+"<br>");

            String vCheckCode=request.getParameter("checkcode");

            vCheckCode=vCheckCode.toLowerCase();

            out.println("输入的验证码:"+vCheckCode+"<br>");

            String vCkCode=(String)session.getAttribute("vCode");

            vCkCode=vCkCode.toLowerCase();

            out.println("生成的验证码:"+vCkCode+"<br>");

            if(!vCheckCode.equals(vCkCode)){

                out.println("输入的验证码是错误的!<br>");

                out.println("<a href='/Jsj-4/4-2checkCode.jsp'>第一种返回方式</a><br>");

                out.println("<a href='javascript:history.back(-1)'>第二种返回方式</a>");

            }

//第三种返回方式

            if(!vCheckCode.equals(vCkCode)){

                out.println("<script type='text/javascript'>");

                out.println(" alert('验证码输入错误!');");

                out.println(" window.history.back(-1);");

                out.println("</script>");

            }

        %>

    </body>

</html>

posted @ 2023-04-17 09:22  Freeland98  阅读(228)  评论(0编辑  收藏  举报