spring boot实现验证码功能

这篇文章主要为大家详细介绍了spring boot实现验证码功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了spring boot实现验证码功能的具体代码,供大家参考,具体内容如下

流程是按照交互顺序。

1.controller层代码获取验证码,以及生成验证码图片

1.1获取html

@RequestMapping(value="/authImage",method=RequestMethod.GET) public String authImage(){ return "authImage"; }

1.2 html

验证码

看不清?

1.3.获取验证码图片

@RequestMapping(value="/getimage",method=RequestMethod.GET) public void authImage(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/jpeg"); // 生成随机字串 String verifyCode = VerifyCodeUtils.generateVerifyCode(4); // 存入会话session HttpSession session = request.getSession(true); // 删除以前的 session.removeAttribute("verCode"); session.removeAttribute("codeTime"); session.setAttribute("verCode", verifyCode.toLowerCase()); session.setAttribute("codeTime", LocalDateTime.Now()); // 生成图片 int w = 100, h = 30; OutputStream out = response.getoutputStream(); VerifyCodeUtils.outputimage(w, h, out, verifyCode); }

1.4 核对验证码 

@RequestMapping(value="validImage",method=RequestMethod.GET) public String validImage(HttpServletRequest request,HttpSession session){ String code = request.getParameter("code"); Object verCode = session.getAttribute("verCode"); if (null == verCode) { request.setAttribute("errmsg", "验证码已失效,请重新输入"); return "验证码已失效,请重新输入"; } String verCodeStr = verCode.toString(); LocalDateTime localDateTime = (LocalDateTime)session.getAttribute("codeTime"); long past = localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); long Now = LocalDateTime.Now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); if(verCodeStr == null || code == null || code.isEmpty() || !verCodeStr.equalsIgnoreCase(code)){ request.setAttribute("errmsg", "验证码错误"); return "验证码错误"; } else if((Now-past)/1000/60>5){ request.setAttribute("errmsg", "验证码已过期,重新获取"); return "验证码已过期,重新获取"; } else { //验证成功,删除存储的验证码 session.removeAttribute("verCode"); return "200"; } }

2、VerifyCodeUtils的工具类

package com.example.springboot.demo.util.varcode; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Arrays; import java.util.Random; import javax.imageio.ImageIO; public class VerifyCodeUtils{ //使用到Algerian字体,系统里没有的话需要安装字体,字体只显示大写,去掉了1,0,i,o几个容易混淆的字符 public static final String VERIFY_CODES = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ"; private static Random random = new Random(); /** * 使用系统认字符源生成验证码 * @param verifySize 验证码长度 * @return */ public static String generateVerifyCode(int verifySize){ return generateVerifyCode(verifySize, VERIFY_CODES); } /** * 使用指定源生成验证码 * @param verifySize 验证码长度 * @param sources 验证码字符源 * @return */ public static String generateVerifyCode(int verifySize, String sources){ if(sources == null || sources.length() == 0){ sources = VERIFY_CODES; } int codesLen = sources.length(); Random rand = new Random(System.currentTimeMillis()); StringBuilder verifyCode = new StringBuilder(verifySize); for(int i = 0; i 255) fc = 255; if (bc > 255) bc = 255; int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); } private static int getRandomIntColor() { int[] rgb = getRandomrgb(); int color = 0; for (int c : rgb) { color = color > 1) * Math.sin((double) i / (double) period + (6.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) { int period = random.nextInt(40) + 10; // 50; boolean borderGap = true; int frames = 20; int phase = 7; for (int i = 0; i > 1) * Math.sin((double) i / (double) period + (6.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); } } } } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

相关文章

Java中的String是不可变对象 在面向对象及函数编程语言中,不...
String, StringBuffer 和 StringBuilder 可变性 String不可变...
序列化:把对象转换为字节序列的过程称为对象的序列化. 反序...
先说结论,是对象!可以继续往下看 数组是不是对象 什么是对...
为什么浮点数 float 或 double 运算的时候会有精度丢失的风险...
面试题引入 这里引申出一个经典问题,看下面代码 Integer a ...