Spring Boot 验证码的生成和验证详解
程序员文章站
2023-12-17 17:40:28
前言
本文介绍的imagecode方法是一个生成图形验证码的请求,checkcode方法实现了对这个图形验证码的验证。从验证码的生成到验证的过程中,验证码是通过sessi...
前言
本文介绍的imagecode方法是一个生成图形验证码的请求,checkcode方法实现了对这个图形验证码的验证。从验证码的生成到验证的过程中,验证码是通过session来保存的,并且设定一个验证码的最长有效时间为5分钟。验证码的生成规则是从0~9的数字中,随机产生一个4位数,并增加一些干扰元素,最终组合成为一个图形输出
1、验证码生成类
import java.awt.*; import java.awt.image.bufferedimage; import java.io.outputstream;import java.util.hashmap; import java.util.map;import java.util.random; public class imagecode { private static char maptable[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; public static map<string, object> getimagecode(int width, int height, outputstream os) { map<string,object> returnmap = new hashmap<string, object>(); if (width <= 0) width = 60; if (height <= 0) height = 20; bufferedimage image = new bufferedimage(width, height, bufferedimage.type_int_rgb); // 获取图形上下文 graphics g = image.getgraphics(); //生成随机类 random random = new random(); // 设定背景色 g.setcolor(getrandcolor(200, 250)); g.fillrect(0, 0, width, height); //设定字体 g.setfont(new font("times new roman", font.plain, 18)); // 随机产生168条干扰线,使图象中的认证码不易被其它程序探测到 g.setcolor(getrandcolor(160, 200)); for (int i = 0; i < 168; i++) { int x = random.nextint(width); int y = random.nextint(height); int xl = random.nextint(12); int yl = random.nextint(12); g.drawline(x, y, x + xl, y + yl); } //取随机产生的码 string strensure = ""; //4代表4位验证码,如果要生成更多位的认证码,则加大数值 for (int i = 0; i < 4; ++i) { strensure += maptable[(int) (maptable.length * math.random())]; // 将认证码显示到图象中 g.setcolor(new color(20 + random.nextint(110), 20 + random.nextint(110), 20 + random.nextint(110))); //直接生成 string str = strensure.substring(i, i + 1); g.drawstring(str, 13 * i + 6, 16); } // 释放图形上下文 g.dispose(); returnmap.put("image",image); returnmap.put("strensure",strensure); return returnmap; } //给定范围获得随机颜色 static color getrandcolor(int fc, int bc) { random random = new random(); if (fc > 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); } }
2、获取验证码api
@requestmapping(value = "/images/imagecode") public string imagecode(httpservletrequest request, httpservletresponse response) throws exception { outputstream os = response.getoutputstream(); map<string,object> map = imagecode.getimagecode(60, 20, os); string simplecaptcha = "simplecaptcha"; request.getsession().setattribute(simplecaptcha, map.get("strensure").tostring().tolowercase()); request.getsession().setattribute("codetime",new date().gettime()); try { imageio.write((bufferedimage) map.get("image"), "jpeg", os); } catch (ioexception e) { return ""; } return null; }
3、验证验证码api
@requestmapping(value = "/checkcode") @responsebody public string checkcode(httpservletrequest request, httpsession session) throws exception { string checkcode = request.getparameter("checkcode"); object cko = session.getattribute("simplecaptcha") ; //验证码对象 if(cko == null){ request.setattribute("errormsg", "验证码已失效,请重新输入!"); return "验证码已失效,请重新输入!"; } string captcha = cko.tostring(); date now = new date(); long codetime = long.valueof(session.getattribute("codetime")+""); if(stringutils.isempty(checkcode) || captcha == null || !(checkcode.equalsignorecase(captcha))) { request.setattribute("errormsg", "验证码错误!"); return "验证码错误!"; } else if ((now.gettime()-codetime)/1000/60>5) { //验证码有效时长为5分钟 request.setattribute("errormsg", "验证码已失效,请重新输入!"); return "验证码已失效,请重新输入!"; }else { session.removeattribute("simplecaptcha"); return "1"; } }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。