java 验证码生成与校验

java绘图相关类

验证码工具类

package dt2008.util;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

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

    //定义随机类
    private Random random = new Random();

    public void code(HttpServletRequest request, HttpServletResponse response){
        //1.创建缓存图片类
        //构造方法:new BufferedImage(int width,int height,int model)
        BufferedImage image = new BufferedImage(150,50,BufferedImage.TYPE_INT_RGB);

        //2.获取画笔
        Graphics g = image.getGraphics();

        //3.设置画笔颜色
        g.setColor(Color.white);//白色

        //4.填充矩形
        //g.fillRect(x,y,width,height); x,y 起点的坐标,width矩形宽度,height矩形的高度
        g.fillRect(0,0,150,50);

        //5.画矩形边框
        //设置画笔颜色(边框的颜色)
        g.setColor(Color.blue);//蓝色
        //画边框的语法:g.drawRect(x,y,width,height); x,y 起点的坐标,width边框宽度,height边框的高度
        g.drawRect(1,1,147,47);

        //6.画4条干扰线
        //画直线语法:g.drawLine(x1,y1,x2,y2);  x1,y1 线的起点坐标,x2,y2先的终点坐标
        for (int i = 0; i < 4; i++) {

            //设置画笔为随机颜色
            g.setColor(getRandomColor());

            //随机获取2个点
            int x1 = random.nextInt(147)+1;
            int y1 = random.nextInt(47)+1;

            int x2 = random.nextInt(147)+1;
            int y2 = random.nextInt(47)+1;

            //画直线
            g.drawLine(x1,y1,x2,y2);
        }


        //7.画验证码字符串
        String checkCodeScope = "qwertyupasdfghjkzxcvbnm";

        //定义接收拼接随机获取到的验证码字符变量
        StringBuilder checkCodeBuilder = new StringBuilder();

        //循环随机获取4个验证码字符
        for (int i = 0; i < 4; i++) {
            //获取随机位置
            int index = random.nextInt(checkCodeScope.length());
            //根据位置获取字符
            char c = checkCodeScope.charAt(index);
            //checkCodeBuilder拼接获取的随机字符
            checkCodeBuilder.append(c);

            //设置画笔的字体
            //字符构造方法:new Font(字体,样式,大小)
            g.setFont(new Font("微软雅黑",Font.BOLD,20));

            //将每个字符画到图片上
            //语法:g.drawString(字符串,x,y);
            g.drawString(c+"",30+i*30,30);
        }

        request.getSession().setAttribute("SESSION_CHECKCODE", checkCodeBuilder.toString());


        //8.将验证码图片输出给浏览器
        //输出缓存图片的语法:ImageIO.write(缓存图片,图片的格式,输出给浏览器的字节流);
        try {
            ImageIO.write(image,"png",response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //获取随机的颜色
    private Color getRandomColor(){

        int r = random.nextInt(256);
        int g = random.nextInt(256);
        int b = random.nextInt(256);

        //构造方法语法:new Color(int r,int g,int b) //由红、绿、蓝组成, 每个颜色值范围0~255
        return new Color(r,g,b);
    }

}

Controller(生成验证码)

package yingshi.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import yingshi.util.RandomValidateCodeUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 登录以及验证码
 */
@RestController
@RequestMapping("login/admin")
public class LoginController {

    /**
     * 生成验证码
     */
    @RequestMapping("getVerify")
    public void getVerify(HttpServletRequest request, HttpServletResponse response) {
        RandomValidateCodeUtil randomValidateCodeUtil = new RandomValidateCodeUtil();
        randomValidateCodeUtil.code(request, response);
    }


}

 

这时,可以访问验证码了,可以在前端那写个js点击切换验证码

接下来就是校验验证码(注意:是分大小写的)

package yingshi.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import yingshi.util.RandomValidateCodeUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 登录以及验证码
 */
@RestController
@RequestMapping("login/admin")
public class LoginController {

    /**
     * 校验后台登录
     */
    @RequestMapping("checkVerify")
    public String checkVerify(String username, String password, String verifyInput, HttpServletRequest request, HttpServletResponse response) {
        //验证码校验
        //获取session中的验证码
        String sessionCheckCode = (String) request.getSession().getAttribute("SESSION_CHECKCODE");
        //判断验证码是否正确
        if(!verifyInput.equals(sessionCheckCode)){
            return "错误";
        }else{
            return "正确";
        }

    }

}

 

希望这篇文章对您有帮助!

本站资源除特别声明外,转载文章请声明文章出处
东泰博客 » java 验证码生成与校验

发表评论