验证码工具类

验证码


验证码实现流程图:

验证码工具类_第1张图片

验证码工具类:

package cn.itsource._02_randomcode;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *  验证码工具类
 * */   
public class RandomCodeUtil {

	private RandomCodeUtil(){}
	
	/**
	 *   判断用户输入的验证码与session中的是否一致
	 *   codeName:表示session中存的验证码名称
	 * */
	public static Boolean equalsRandomCode(HttpServletRequest req,String codeName){
		String code = req.getParameter("code");
		String sessionCode = (String)req.getSession().getAttribute(codeName);
		//删除服务器中的session,删除了不占用内存
		req.getSession().removeAttribute(codeName);
		return code.equals(sessionCode);
		
	}
	
	
	/**
	 *  生成验证码的方法
	 * */
	public static  void createRandomCode(HttpServletRequest req, HttpServletResponse resp,String codeName)
			throws IOException{
		
		//生成随机数
		String randomCode = UUID.randomUUID().toString().substring(0, 5);

		//把随机数放进Session中
		req.getSession().setAttribute(codeName, randomCode);

		//创建图片对象
		int width = 100;
		int height = 40;
		int imageType = BufferedImage.TYPE_INT_RGB;
		BufferedImage image = new BufferedImage(width, height, imageType);

		//画板
		Graphics g = image.getGraphics();
		g.setColor(Color.WHITE);
		//绘制一个实心的矩形
		g.fillRect(1, 1, width - 2, height - 2);

		//把随机数画进图片中
		g.setColor(Color.BLACK);//设置随机数的颜色
		Font font = new Font("宋体", Font.BOLD + Font.ITALIC, 20);
		g.setFont(font);//设置随机数的字体和大小
		g.drawString(randomCode, 10, 28);
		//干扰线
		g.setColor(Color.GRAY);
		Random r = new Random();
		for (int i = 0; i < 100; i++) {
			g.fillRect(r.nextInt(width), r.nextInt(height), 2, 2);
		}

		//关闭
		g.dispose();
		//把图片对象以流的方式保存出去
		ImageIO.write(image, "jpg", resp.getOutputStream());
	
	}
}

调用验证码工具类:

package cn.itsource._02_randomcode;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/randomCode")
public class RandomCodeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//调用验证码工具类
		RandomCodeUtil.createRandomCode(req, resp,"RANDOMCODE_IN_SESSION");
	}
}


验证码的校验功能:

package cn.itsource._02_randomcode;

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;

@WebServlet("/login")
public class LoginServlet extends HttpServlet{

		private static final long serialVersionUID = 1L;

		@Override
		protected void service(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException {
			req.setCharacterEncoding("UTF-8");
			/**
			 *  1.从请求中拿到用户输入的验证码
			 *  2.从session中拿到真实的验证码
			 *  3.判断两个验证码是否相等
			 *  	如果不相等(登录失败),返回登录页面
			 *    相等    正常执行代码
			 * */
//			String code = req.getParameter("code");
//			String sessionCode = (String)req.getSession().getAttribute("RANDOMCODE_IN_SESSION");
//			//删除服务器中的session,删除了不占用内存
//			req.getSession().removeAttribute("RANDOMCODE_IN_SESSION");
			if(!RandomCodeUtil.equalsRandomCode(req,"RANDOMCODE_IN_SESSION")){
				//提示信息
				req.setAttribute("errorMsg", "您输入的验证码有误");
				req.getRequestDispatcher("/login.jsp").forward(req, resp);
				return;
			}
			
			System.out.println("正常执行代码");
		}
}

login.jsp文件:

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




登录





	${errorMsg}
	
姓名:
密码:
验证码:




你可能感兴趣的:(验证码工具类)