Java 中验证码的使用

今天我们讲一下在 java 中验证码的使用。
验证码生成本效果是利用easy-captcha工具包实现,首先需要添加相关依赖到pom.xml中,代码如下:
com.github.whvcse easy-captcha 1.6.2验证码格式easy-captcha验证码工具支持gif、中文、算术等类型,分别通过下面几个实例对象实现:
speccaptcha(png类型的静态图片验证码)gifcaptcha(gif类型的图片验证码)chinesecaptcha(gif类型中文图片验证码)arithmeticcaptcha(算术类型的图片验证码)字符类型分为以下几种:
type_default:数字和字母混合typeonlynumber:纯数字typeonlychar:纯字母typeonlyupper:纯大写字母typeonlylower:纯小写字母typenumand_upper:数字和大写字母混合后端逻辑的实现package com.yanx.controller; import com.wf.captcha.speccaptcha;import com.wf.captcha.base.captcha;import org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.getmapping;import org.springframework.web.bind.annotation.requestparam;import org.springframework.web.bind.annotation.responsebody;import org.thymeleaf.util.stringutils; import javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;import java.io.ioexception; @controllerpublic class kapchacontroller { @getmapping(/kaptcha) public void defaultkaptcha(httpservletrequest httpservletrequest, httpservletresponse httpservletresponse) throws ioexception { httpservletresponse.setheader(cache-control,no-store); httpservletresponse.setheader(pragma,no-cache); httpservletresponse.setdateheader(expires,0); httpservletresponse.setcontenttype(image/gif); //三个参数分别为宽、高、位数 speccaptcha captcha=new speccaptcha(75,30,4); //设置类型为数字和字母混合 captcha.setchartype(captcha.type_default); //设置字体 captcha.setchartype(captcha.font_9); //验证码存入session httpservletrequest.getsession().setattribute(verifycode,captcha.text().tolowercase()); //输出图片流 captcha.out(httpservletresponse.getoutputstream()); } }这里控制器新增了defaultkaptcha()方法,该方法所拦截处理的路径为/kaptcha
前端逻辑的实现在static目录中新建kaptcha.html页面,代码如下:
验证码 访问后端验证码路径/kaptcha,验证码为图片形式。onclick方法为点击该标签时可以动态切换显示验证码。
启动spring boot项目,打开浏览器输入地址:
http://localhost:8080/kaptcha.html
效果如下:
验证码验证后端代码package com.yanx.controller; import com.wf.captcha.speccaptcha;import com.wf.captcha.base.captcha;import org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.getmapping;import org.springframework.web.bind.annotation.requestparam;import org.springframework.web.bind.annotation.responsebody;import org.thymeleaf.util.stringutils; import javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;import javax.servlet.http.httpsession;import java.io.ioexception; @controllerpublic class kapchacontroller { @getmapping(/kaptcha) public void defaultkaptcha(httpservletrequest httpservletrequest, httpservletresponse httpservletresponse) throws ioexception { httpservletresponse.setheader(cache-control,no-store); httpservletresponse.setheader(pragma,no-cache); httpservletresponse.setdateheader(expires,0); httpservletresponse.setcontenttype(image/gif); //三个参数分别为宽、高、位数 speccaptcha captcha=new speccaptcha(75,30,4); //设置类型为数字和字母混合 captcha.setchartype(captcha.type_default); //设置字体 captcha.setchartype(captcha.font_9); //验证码存入session httpservletrequest.getsession().setattribute(verifycode,captcha.text().tolowercase()); //输出图片流 captcha.out(httpservletresponse.getoutputstream()); } @getmapping(/verify) @responsebody public string verify(@requestparam(code) string code, httpsession session){ if(stringutils.isempty(code)){ return 验证码不能为空; } string kapchacode = session.getattribute(verifycode)+; if(stringutils.isempty(kapchacode)||!code.tolowercase().equals(kapchacode)){ return 验证码输入错误; } return 验证成功; }}前端代码 验证码验证 验证 $(function(){ //验证按钮点击事件 $('#verify').click(function(){ var code=$('#code').val(); $.ajax({ type:'get',//方法类型 url:'/verify?code='+code, success:function(result){ $('#verifyresult').html(result); }, error:function(){ alert('请求失败'); }, }); }); });效果
结束语生成验证码功能还是比较常用的,所以记录整理一下,方便以后回顾,如果有帮到你们的地方倍感荣幸,有路过的大佬还望不吝雅教!

动力电池产业现状及技术动态
用虚拟现实技术来代替全身麻痹医疗
瑞声科技遭苹果砍单 如何挽回业绩颓势
中国移动将同步开启“AI新基建 5G新机遇”云端主题论坛
MECE原则 随机约束的分类介绍
Java 中验证码的使用
idea如何运行main方法
亿纬船舶动力系列口碑产品为智慧港口建设注入新动力
一文知道边缘节点对工业物联网的重要性
工具平台对MLOps技术实践的支持
pcb分板机原理_pcb板分板机的种类
ARM处理器设计的电机电物理量采集系统
宁德时代称将线下发布“头号新品”,传为快充电池
充电桩标准是什么?国网1591文件告诉你答案
个人及入门存储市场复苏,一季度增长73.4%
环网柜基础知识介绍(开关柜、断路器柜,负荷开关柜)
点将科技自主设计和制作的TR-ZSY01蒸渗仪
浪潮发布业界首款 8颗GPU的AI服务器,可实现每秒千万亿次的AI计算性能
世界第一的芯片性能强悍,40万核心1.2万亿晶体管
28nm浪潮席卷 探究各晶圆代工厂产能状况