天天看點

spring架構下的ajax生成驗證碼以及進行驗證碼驗證

一、大概思路

  1. 建立驗證碼控制類VerifyCodeController,控制類下有初始化圖檔的方法和響應前端請求的方法
  2. 編寫前端頁面
  3. 利用ajax連接配接前端和背景,進行圖檔的動态生成和驗證

實際效果圖:

spring架構下的ajax生成驗證碼以及進行驗證碼驗證

具體實作

1.驗證碼控制類VerifyCodeController

package com.bill.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * @author zkc
 */
@Controller
@RequestMapping("verify")
public class VerifyCodeController {
    // 驗證碼圖檔的寬度。
    private int width = ;
    // 驗證碼圖檔的高度。
    private int height = ;
    // 驗證碼字元個數
    private int codeCount = ;
    private int x = ;
    // 字型高度
    private int fontHeight;
    private int codeY;
    //驗證碼圖檔上會出現的字母和數字
    char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
            'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
            'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    /**
     * 初始化驗證圖檔屬性
     */
    public void init() throws ServletException {
        // 從web.xml中擷取初始資訊
        // 寬度
        String strWidth ="80";
        // 高度
        String strHeight ="30";
        // 字元個數
        String strCodeCount = "4";
        // 将配置的資訊轉換成數值
        try {
            if (strWidth != null && strWidth.length() != ) {
                width = Integer.parseInt(strWidth);
            }
            if (strHeight != null && strHeight.length() != ) {
                height = Integer.parseInt(strHeight);
            }
            if (strCodeCount != null && strCodeCount.length() != ) {
                codeCount = Integer.parseInt(strCodeCount);
            }
        } catch (NumberFormatException e) {
        }
        x = width / (codeCount + );
        fontHeight = height - ;
        codeY = height - ;
    }

    @RequestMapping(value="/code.action",method= RequestMethod.GET)
    public void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, java.io.IOException {
        init();
        // 定義圖像buffer
        BufferedImage buffImg = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g = buffImg.createGraphics();
        // 建立一個随機數生成器類
        Random random = new Random();
        // 将圖像填充為白色
        g.setColor(Color.WHITE);
        g.fillRect(, , width, height);
        // 建立字型,字型的大小應該根據圖檔的高度來定。
        Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);
        // 設定字型。
        g.setFont(font);
        // 畫邊框。
        g.setColor(Color.BLACK);
        g.drawRect(, , width - , height - );
        // 随機産生160條幹擾線,使圖象中的認證碼不易被其它程式探測到。
        g.setColor(Color.BLACK);
        for (int i = ; i < ; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xl = random.nextInt();
            int yl = random.nextInt();
            g.drawLine(x, y, x + xl, y + yl);
        }
        // randomCode用于儲存随機産生的驗證碼,以便使用者登入後進行驗證。
        StringBuffer randomCode = new StringBuffer();
        int red = , green = , blue = ;
        // 随機産生codeCount數字的驗證碼。
        for (int i = ; i < codeCount; i++) {
            // 得到随機産生的驗證碼數字。
            String strRand = String.valueOf(codeSequence[random.nextInt()]);
            // 産生随機的顔色分量來構造顔色值,這樣輸出的每位數字的顔色值都将不同。
            red = random.nextInt();
            green = random.nextInt();
            blue = random.nextInt();
            // 用随機産生的顔色将驗證碼繪制到圖像中。
            g.setColor(new Color(red, green, blue));
            g.drawString(strRand, (i + ) * x, codeY);
            // 将産生的四個随機數組合在一起。
            randomCode.append(strRand);
        }
        // 将四位數字的驗證碼儲存到Session中。
        HttpSession session = req.getSession();
        session.setAttribute("validateCode", randomCode.toString());
        // 禁止圖像緩存。
        resp.setHeader("Pragma", "no-cache");
        resp.setHeader("Cache-Control", "no-cache");
        resp.setDateHeader("Expires", );
        resp.setContentType("image/jpeg");
        // 将圖像輸出到Servlet輸出流中。
        ServletOutputStream sos = resp.getOutputStream();
        ImageIO.write(buffImg, "jpeg", sos);
        sos.close();
    }
    //響應前端請求的方法
    @RequestMapping(value="/checkVerifyCode.action",method= RequestMethod.POST)
    @ResponseBody
    public Object checkVerifyCode(@RequestParam(value = "verifyCode") String verifyCode,HttpServletRequest req, HttpServletResponse resp){
      JSONObject jsobjcet = new JSONObject();
      String flag = null;
      if(verifyCode!=null){
          HttpSession session = req.getSession();
          //擷取圖檔中的驗證碼
          String validateCode= (String) session.getAttribute("validateCode");
          //輸入的與圖檔中的進行比較
          if(validateCode!=null&&validateCode.equals(verifyCode.toUpperCase())){
              jsobjcet.put("valid", true);
              flag = "true";
          }else{
              jsobjcet.put("valid", false);
              flag = "false";
          }
      }else{
          jsobjcet.put("valid", false);
          flag = "false";
      }
      return flag;
    }
}
           

2.前端頁面代碼(jsp頁面)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <script type="text/javascript" src="js/jquery-1.11.0.js"></script>
    <script type="text/javascript">
        $(function(){
            $("form").submit(function(e){
                if($("#tel").val()==""){
                    alert("手機号不能為空");
                    e.preventDefault();
                }else if($("#pwd").val()==""){
                    alert("密碼不能為空");
                    e.preventDefault();
                }else if($("input[name=validateCode]").val()==""){
                    alert("驗證碼不能為空");
                    e.preventDefault();
                }else{
                    <!-- 擷取輸入的驗證碼 -->
                    var verifyCode=$("input[name=validateCode]").val();
                    <!-- 通過ajax方式将值傳到背景 -->
                    $.ajax({
                        url:"verify/checkVerifyCode.action",<!-- 要傳的位址 -->
                        type:"post",                        <!-- 傳值的方式 -->
                        data:{"verifyCode":verifyCode},     <!-- 傳的資料(json對象) -->
                        async:false,                        <!-- 是否異步 -->
                        success:function(result){           <!-- 成功後執行的代碼 -->
                            if(result=="false"){
                                alert("驗證碼錯誤");
                                e.preventDefault();
                            }else{
                                $("form").submit();
                            }

                        }
                    });
                }
            });
            <!-- 重新整理圖檔 -->
            $("#refresh").unbind("click").bind("click",function(){
                <!-- 傳遞一個随機數給背景 -->
                $("#verify").attr("src","verify/code.action?r"+Math.random());
            });
        });
    </script>
  </head>

  <body>
        <form action="login.action" method="post">
            <table>
                <tr>
                    <td>手機号碼:</td>
                    <td><input type="text" name="tel" id="tel"/></td>
                </tr>
                <tr>
                    <td>服務密碼:</td>
                    <td><input type="password" name="pwd" id="pwd"/></td>
                </tr>
                <tr>
                    <!-- 驗證碼 -->
                    <td>驗證碼:</td>
                    <td><input type="text" name="validateCode" id="code"/></td>
                    <td>
                        <!-- 驗證碼圖檔 -->
                        <img src="verify/code.action" id="verify"/>
                        <!-- 點選更換驗證碼圖檔 -->
                        <a id="refresh">看不清?</a>
                    </td>
                </tr>
            </table>
            <input type="submit" value="登入"/>
        </form>
  </body>
</html>
           

由于我用了spring架構,是以還需要配置一下xml檔案,下面是配置資訊

spring架構下的ajax生成驗證碼以及進行驗證碼驗證

1.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>billplatform</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <!-- Spring  -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-mybatis.xml</param-value>
  </context-param>
  <!-- Spring監聽 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- Spring MVC -->
  <servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>*.action</url-pattern>           <!-- 攔截所有.action請求 -->
  </servlet-mapping>

  <!-- 過濾器統一字元  -->
  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>utf-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>
           

2.springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:context="http://www.springframework.org/schema/context"  
 xmlns:p="http://www.springframework.org/schema/p"  
 xmlns:mvc="http://www.springframework.org/schema/mvc"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xsi:schemaLocation="http://www.springframework.org/schema/beans  
      http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
      http://www.springframework.org/schema/context  
      http://www.springframework.org/schema/context/spring-context.xsd  
      http://www.springframework.org/schema/mvc  
      http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

      <!-- 注解掃描(掃描所有包下)-->
      <context:component-scan base-package="com.bill.*"></context:component-scan>

      <!-- 配置視圖解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!--  配置視圖的字首 
            <property name="prefix" value=""></property>-->
            <!--  配置視圖的字尾
            <property name="suffix" value=".jsp"></property> -->
        </bean>
</beans>
           

以上就是我在spring架構下用ajax實作的一個簡單的驗證碼功能,第一次寫部落格,很多地方不足,歡迎大家能指出,謝謝。

繼續閱讀