SpringBoot+Redis+Cookie实现登陆验证码校验功能

一、处理逻辑图
SpringBoot+Redis+Cookie实现登陆验证码校验功能_第1张图片

二、代码部分

项目中封装了Jedis工具类进行redis的数据存取操作,也使用了Cookie工具类进行cookie中的数据存取操作,该篇文件主要介绍验证码校验相关的内容,这里不再介绍java使用Jedis操作redis的方法,如果想了解java使用redis的方法,请参考 这里。

1、前端页面form表单内容

2、后端服务
1、controller代码

@Controller
@CrossOrigin
public class WebController {
	/**
	 * 生成验证码
	 */
	public static final String VERIFY_ID = "verid";

	@RequestMapping("/getCodeImg")
	public void getCodeImg(HttpServletRequest request, HttpServletResponse response) {
		try {
			Object[] objs = VerifyUtil.createImage();
			String code = objs[0].toString();
			String verid = UUID.randomUUID().toString().replaceAll("-", "");
			
			Cookie cookie=new Cookie(VERIFY_ID, verid);        //将uuid串存入cookie
	        response.addCookie(cookie);
	        StringBuilder verifyKey = new StringBuilder();
	        verifyKey.append(verid);
	        JedisUtil.setStringValue(verifyKey.toString(), code, 3600);     //将验证码存入redis
	        /**
	         * log.info("verid:"+verid);
			log.info("code:"+code);
			*/
			BufferedImage image = (BufferedImage) objs[1];
			response.setContentType("image/png");
			OutputStream os = response.getOutputStream();
			ImageIO.write(image, "png", os);         //输出验证码图片文件流
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 登陆验证
	 */
	@RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
     public LoginResult login(HttpServletRequest request,
                           HttpServletResponse response,
                           @RequestParam("username") String username,
                           @RequestParam("password") String password,
                           @RequestParam("vercode") String vercode) {

     LoginResult result1=new LoginResult();

        //Verification Code
        if(!StringUtils.hasText(vercode)){
            result1.setResultCode(400);
            result1.setResultMessage("请输入验证码");
            return result1;
        }
        String verid= CookieUtil.getValue(request,"verid");      //从cookie获取uuid串
        String redisVerCode=null;
        if(StringUtils.hasText(verid)){
            redisVerCode= JedisUtil.getStringValue(verid);       //从redis获取验证码
            JedisUtil.del("verid"+verid);
        }
        if(verid==null || redisVerCode==null){
            result1.setResultCode(400);
            result1.setResultMessage("验证码已过期,请刷新验证码");
            return result1;
        }
        CookieUtil.remove(request,response,"verid");
        if(!redisVerCode.equalsIgnoreCase(vercode)){     //比对验证码
            result1.setResultCode(ConstantsInit.PARAMS_ERROR);
            result1.setResultMessage("验证码错误");
            return result1;
        }
        
		//验证码校验通过
		//开始验证用户名密码,过程省略。。。
		return result1;
}

2、验证码生成工具类

public class VerifyUtil {
	// 验证码字符集
	private static final char[] chars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E',
			'F', 'G', 'H',  'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
	// 字符数量
	private static final int SIZE = 4;
	// 干扰线数量
	private static final int LINES = 10;
	// 宽度
	private static final int WIDTH = 90;
	// 高度
	private static final int HEIGHT = 40;
	// 字体大小
	private static final int FONT_SIZE = 30;

	/**
	 * 生成随机验证码及图片 Object[0]:验证码字符串; Object[1]:验证码图片。
	 */
	public static Object[] createImage() {
		StringBuffer sb = new StringBuffer();
		// 1.创建空白图片
		BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
		// 2.获取图片画笔
		Graphics graphic = image.getGraphics();
		// 3.设置画笔颜色
		graphic.setColor(Color.LIGHT_GRAY);
		// 4.绘制矩形背景
		graphic.fillRect(0, 0, WIDTH, HEIGHT);
		// 5.画随机字符
		Random ran = new Random();
		for (int i = 0; i < SIZE; i++) {
			// 取随机字符索引
			int n = ran.nextInt(chars.length);
			// 设置随机颜色
//			graphic.setColor(getRandomColor(101, 111, 121));
			graphic.setColor(getRandomColor(150, 150, 150));
			// 设置字体大小
			graphic.setFont(new Font(null, Font.BOLD + Font.ITALIC, FONT_SIZE));
			// 画字符
			graphic.drawString(chars[n] + "", i * WIDTH / SIZE, HEIGHT * 2 / 3);
			// 记录字符
			sb.append(chars[n]);
		}
		// 6.画干扰线
		for (int i = 0; i < LINES; i++) {
			// 设置随机颜色
			graphic.setColor(getRandomColor(255, 255, 255));
			// 随机画线
			graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT), ran.nextInt(WIDTH), ran.nextInt(HEIGHT));
		}
		
		/**
		 * //扭曲
		shearX(graphic, WIDTH, HEIGHT, Color.LIGHT_GRAY);
		shearY(graphic, WIDTH, HEIGHT, Color.LIGHT_GRAY);
		*/
		
		// 7.返回验证码和图片

		return new Object[] { sb.toString(), image };
	}
	

	/**
	 * 随机取色
	 */
	public static Color getRandomColor(int r, int g, int b) {
		Random ran = new Random();
		Color color = new Color(ran.nextInt(r), ran.nextInt(g), ran.nextInt(b));
		return color;
	}
	/**
	 * fc:110 bc:133
	 * @param fc
	 * @param bc
	 * @return
	 */
	public static Color getRandColor(int fc, int bc) {
		if (fc > 255)
			fc = 255;
		if (bc > 255)
			bc = 255;
		Random random = new Random();
		int r = fc + random.nextInt(bc - fc - 16);
		int g = fc + random.nextInt(bc - fc - 14);
		int b = fc + random.nextInt(bc - fc - 18);
		return new Color(r, g, b);
	}
	
	private static void shearX(Graphics g, int w1, int h1, Color color) {
        Random random=new Random();
        int period = 2;
 
        boolean borderGap = true;
        int frames = 1;
       int phase = random.nextInt(2);

       for (int i = 0; i < h1; i++) {
           double d = (double) (period >> 1)* Math.sin((double) i / (double) period + (2.2831853071795862D * (double) phase)/ (double) frames);
           g.copyArea(0, i, w1, 1, (int) d, 0);
           if (borderGap) {
               g.setColor(color);
               g.drawLine((int) d, i, 0, i);
               g.drawLine((int) d + w1, i, w1, i);
           }
       }

   }

   private static void shearY(Graphics g, int w1, int h1, Color color) {
       Random random=new Random();
       int period = random.nextInt(40) + 10; // 50;

       boolean borderGap = true;
       int frames = 20;
       int phase = random.nextInt(2);
       for (int i = 0; i < w1; i++) {
           double d = (double) (period >> 1)
                   * Math.sin((double) i / (double) period
                           + (2.2831853071795862D * (double) phase)/ (double) frames);
           g.copyArea(i, 0, 1, h1, 0, (int) d);
           if (borderGap) {
               g.setColor(color);
               g.drawLine(i, (int) d, i, 0);
               g.drawLine(i, (int) d + h1, i, h1);
           }

       }

   }

	public static void main(String[] args) throws IOException {
		Object[] objs = createImage();
		BufferedImage image = (BufferedImage) objs[1];
		OutputStream os = new FileOutputStream("d:/1.png");
		ImageIO.write(image, "png", os);
		os.close();
	}
}

你可能感兴趣的:(springboot,Redis)