JavaWeb

JavaWeb-027-验证码

2018-01-05  本文已影响4人  53b3f4658edc

概述

一次性验证码的主要目的就是为了限制人们利用工具软件来暴力猜测密码,其原理与利用Session防止表单重复提交的原理基本一样,只是将表单标识号变成了验证码的形式,并且要求用户将提示的验证码手工填写进一个表单字段中,而不是通过表单的隐藏字段自动回传给服务器。
服务器程序接收到表单数据后,首先判断用户是否填写了正确的验证码,只有该验证码与服务器端保存的验证码匹配时,服务器程序才开始正常的表单处理流程。
密码猜测工具要逐一尝试每个密码的前题条件是先输入正确的验证码,而验证码是一次性有效的,这样基本上就阻断了密码猜测工具的自动地处理过程。

原理

原理和表单重复提交一致
a) 验证码的值提交到session中,用户输入的验证码也存储到session中,然后进行比较。
b) 其它都是一致的。

测试代码

微信公众号:JavaWeb架构师

index.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=UTF-8">
        <title>Insert title here</title>
        
        <style type="text/css">
            p {
                color : red;
            }
        </style>
    </head>
    
    <body>
    <!-- 验证码信息有误的提示 -->
    <p> <%= session.getAttribute("message") == null ? "" :  session.getAttribute("message") %> </p>
    <%
        if( session.getAttribute("message") != null ) {
            session.removeAttribute("message");
        }
    %>
    <!--  
        1.在验证码Servlet中生成验证码,并传递给session
        2.把生成的验证码发送到网页
        3.用户输入验证码
        4.在Servlet中对1和3进行比对
        5.其它和处理重复提交一样(验证码一致就清除,否则就提示重复提交)
    -->
        <form action="<%= request.getContextPath() %>/checkCodeServlet" method="post">
            <table>
                <tr>
                    <td>用户名:</td>
                    <td><input type="text" name="username"/></td>
                </tr>
                
                <tr>
                    <td>验证码:</td>
                    <td><input type="text" name="CHECK_CODE_PARAM_NAME" /></td>
                </tr>
                
                <tr>
                    <!--  图片来自validateColorServlet -->
                    <td><img src="validateColorServlet" /></td>
                    <td><input type="submit" value="提交"/></td>
                </tr>
            </table>
        </form>
            
    </body>
</html>

ValidateColorServlet.java

package com.mac.servlet; 

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.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;

/*
 * 验证码生成的Servlet
 */
public class ValidateColorServlet extends HttpServlet {
//////////////////////////////////////////////////////////////
    public static final String CHECK_CODE_KEY = "CHECK_CODE_KEY";
//////////////////////////////////////////////////////////////
    
    private static final long serialVersionUID = 1L;
    
    //设置验证图片的宽度, 高度, 验证码的个数
    private int width = 152;
    private int height = 40;
    private int codeCount = 4;
    
    //验证码字体的高度
    private int fontHeight = 4;
    
    //验证码中的单个字符基线. 即:验证码中的单个字符位于验证码图形左上角的 (codeX, codeY) 位置处
    private int codeX = 0;
    private int codeY = 0;
    
    //验证码由哪些字符组成
    char [] codeSequence = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
    
    //初始化验证码图形属性
    public void init(){
        fontHeight = height - 2;
        codeX = width / (codeCount + 2);
        codeY = height - 4;
    }

    public void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //定义一个类型为 BufferedImage.TYPE_INT_BGR 类型的图像缓存
        BufferedImage buffImg = null;
        buffImg = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
    
        //在 buffImg 中创建一个 Graphics2D 图像
        Graphics2D graphics = null;
        graphics = buffImg.createGraphics();
        
        //设置一个颜色, 使 Graphics2D 对象的后续图形使用这个颜色
        graphics.setColor(Color.WHITE);
        
        //填充一个指定的矩形: x - 要填充矩形的 x 坐标; y - 要填充矩形的 y 坐标; width - 要填充矩形的宽度; height - 要填充矩形的高度
        graphics.fillRect(0, 0, width, height);
        
        //创建一个 Font 对象: name - 字体名称; style - Font 的样式常量; size - Font 的点大小
        Font font = null;
        font = new Font("", Font.BOLD, fontHeight);
        //使 Graphics2D 对象的后续图形使用此字体
        graphics.setFont(font);
        
        graphics.setColor(Color.WHITE);
        
        //绘制指定矩形的边框, 绘制出的矩形将比构件宽一个也高一个像素
        graphics.drawRect(0, 0, width - 1, height - 1);
        
        //随机产生 15 条干扰线, 使图像中的认证码不易被其它程序探测到
        Random random = null;
        random = new Random();
        for(int i = 0; i < 55; i++){
            graphics.setColor(new Color(i*31%256, i*76%256, i*55%256));
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int x1 = random.nextInt(20);
            int y1 = random.nextInt(20);
            graphics.drawLine(x, y, x + x1, y + y1);
        }
        
        //创建 randomCode 对象, 用于保存随机产生的验证码, 以便用户登录后进行验证
        StringBuffer randomCode;
        randomCode = new StringBuffer();
        
        for(int i = 0; i < codeCount; i++){
            //得到随机产生的验证码数字
            String strRand = null;
            strRand = String.valueOf(codeSequence[random.nextInt(62)]);
            
//////////////////////////////////////////////////////////////
            //把正在产生的随机字符放入到 StringBuffer 中
            randomCode.append(strRand);
//////////////////////////////////////////////////////////////          
            
            //用随机产生的颜色将验证码绘制到图像中
            graphics.setColor(Color.PINK);
            graphics.drawString(strRand, (i + 1)* codeX, codeY);
        }
        
//////////////////////////////////////////////////////////////      
        //再把存放有所有随机字符的 StringBuffer 对应的字符串放入到 HttpSession 中
        request.getSession().setAttribute(CHECK_CODE_KEY, randomCode.toString());
//////////////////////////////////////////////////////////////      
        
        //禁止图像缓存
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        
        //将图像输出到输出流中
        ServletOutputStream sos = null;
        sos = response.getOutputStream();
        ImageIO.write(buffImg, "jpeg", sos); 
        sos.close();
    }
}

CheckCodeServlet.java

package com.mac.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class CheckCodeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    
    private boolean ignoreLetterCompare(String aux1,String aux2) {
        String temp1 = aux1.toLowerCase();
        String temp2 = aux2.toLowerCase();

        return temp1.equals(temp2);
    }
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        
        //1.获取用户输入的验证码值
        String inputCode = request.getParameter("CHECK_CODE_PARAM_NAME");
        
        //2.获取ValidateColorServlet传过来的验证码值
        String indeedCode = (String)request.getSession().getAttribute("CHECK_CODE_KEY");
        System.out.println("inputCode: " + inputCode);
        System.out.println("indeedCode: " + indeedCode);
        
        //3.进行比对
        if( inputCode != null && ignoreLetterCompare(inputCode, indeedCode) ) {
            //登录成功,跳转
            response.sendRedirect(request.getContextPath() + "/succeed.jsp");
            //清除验证码值
            request.getSession().removeAttribute("CHECK_CODE_KEY");
        } else {
            request.getSession().setAttribute("message", (String)request.getParameter("username") + "验证码有误");
            response.sendRedirect(request.getContextPath() + "/index.jsp");
        }
    }
}

succed.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=UTF-8">
        <title>Insert title here</title>
    </head>
    
    <body>
        登录成功
    </body>
</html>
微信公众号:JavaWeb架构师

其它

关注下方公众号,回复:javaweb_course.code
完整教程PDF版本下载
上一篇 下一篇

猜你喜欢

热点阅读