版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第Java实现图片验证码功能在实现登录功能时,一般为了安全都会设置验证码登录,为了防止某个用户用特定的程序暴力破解方式进行不断的尝试登录。常见验证码分为图片验证码和短信验证码,还有滑动窗口模块和选中指定物体验证方式。下面通过Java来实现图片验证码实例。
如上图所示,图片验证码由4个数字和一些彩色的干扰线段组成,点击图片可以更新验证码,只有输入的验证码与图片中的数字一致才能通过登录,否则将会重新刷新验证码,重新输入正确的验证码。
1、controller
@RestController
publicclassValidateCodeController{
@GetMapping("/getCodeImg")
publicvoidgetCodeImage(HttpServletRequestrequest,HttpServletResponseresponse,HttpSessionhttpSession)throwsIOException,InterruptedException{
BufferedImageimage=newBufferedImage(80,32,BufferedImage.TYPE_3BYTE_BGR);
//编辑图像
//获取绘图对象
Graphicsg=image.getGraphics();
g.setColor(newColor(239,239,239));
g.fillRect(0,0,80,32);
//设置字体颜色
g.setColor(newColor(49,49,49));
//设置字体
g.setFont(newFont("SimSong",Font.ITALIC,20));
//绘制字符串;
Stringtext="";
for(inti=0;ii++){
text+=(int)(Math.random()*10);
}
//字符串输出内容,水平起始坐标,垂直起始坐标。
g.drawString(text,17,24);
//画线条
for(inti=0;ii++){
g.setColor(newColor((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
g.drawLine((int)(Math.random()*50),(int)(Math.random()*30),(int)(Math.random()*80),(int)(Math.random()*80));
}
//设置session
httpSession.setAttribute("code",text);
//输出图像
//ImageIO.write(image,"png",newFileOutputStream("C:/Users/H/Desktop/"+tet+".png"));
ImageIO.write(image,"png",response.getOutputStream());
g.dispose();
}
//获取保存在session中的验证码
@GetMapping("/getCode")
publicStringgetCode(HttpSessionhttpSession){
return(String)httpSession.getAttribute("code");
}
}
2、登录页面
body
divid="container"
!--登录--
divid="login"
h3登陆/h3
span{{msg}}/span
formth:action="@{/}"method="get"@submit.prevent="check()"ref="export"
div
label登录名/label
div
inputtype="text"name="username"required
lay-verify="required"placeholder="请输入登录名"autocomplete="off"v-model="userName"@blur="loseFocus()"@focus="getFocus()"
/div
/div
div
label密码/label
div
inputtype="password"required
lay-verify="required"placeholder="请输入密码"autocomplete="off"v-model="userPwd"name="password"@focus="getFocus()"
/div
/div
div
label验证码/label
div
inputtype="text"required
lay-verify="required"placeholder="请输入验证码"autocomplete="off"v-model="code"name="code"@blur="validateCode()"@focus="getFocus()"
/div
imgsrc="/getCodeImg"@click="changeCode()"id="codeImg"
/div
div
div
buttontype="submit"lay-submitlay-filter="formDemo"登录/button
buttontype="button"id="reg"注册/button
/div
/div
/form
/div
!--
注册--
divid="register"
h3注册/h3
span{{msg2}}/span
formth:action="@{/login}"method="post"@submit.prevent="check()"ref="export"
div
label登录名/label
div
inputtype="text"name="username"required
lay-verify="required"placeholder="请输入登录名"autocomplete="off"v-model="userName"@blur="loseFocus()"@focus="getFocus()"
/div
/div
div
label密码/label
div
inputtype="password"required
lay-verify="required"placeholder="请输入密码"autocomplete="off"v-model="userPwd"name="password"@focus="getFocus()"
/div
/div
div
label确认密码/label
div
inputtype="password"required
lay-verify="required"placeholder="请再次输入密码"autocomplete="off"v-model="rePassword"
name="rePassword"@focus="getFocus()"
/div
/div
div
label邮箱/label
div
inputtype="email"required
lay-verify="required"placeholder="请输入邮箱"autocomplete="off"v-model="userEmail"name="userEmail"@blur="validateCode()"@focus="getFocus()"
/div
/div
div
div
buttontype="button"lay-submitlay-filter="formDemo"@click="addUser()"注册/button
buttontype="reset"lay-submitlay-filter="formDemo"重置/button
/div
/div
/form
/div
/div
/body
3、JavaScript
varvm=newVue({
el:'#container',
data:{
userName:'',
userPwd:'',
userEmail:'',
rePassword:'',
msg:'',
msg2:'',
code:'',
text:''
},
methods:{
changeCode:function(){
//如果src里的图片链接不变的话,会直接用缓存的图片,加上Math.random()是为了让src的图片链接改变重新去渲染图片
document.getElementById("codeImg").src="/getCodeImg"+Math.random();
},
validateCode:function(){
vm.$http.get('/getCode').then((response)={
this.text=response.data
})
},
addUser:function(){
if(vm.userPwd!==vm.rePassword){
vm.msg2="确认密码不正确!"
}else{
vm.$http.post('/user/add/'+vm.userName+'/'+vm.userPwd+'/'+vm.userEmail).then((response)={
window.location.href='/'
})
}
},
loseFocus:function(){
vm.$http.get('/user/queryUserByName/'+vm.userName).then((response)={
if(response.data.userName!==vm.userName){
vm.msg="该用户名不存在!"
}
})
},
getFocus:function(){
vm.msg=""
},
check:function(){
if(vm.userName===""){
vm.msg="用户名为空!"
return
}
if(vm.userPwd===""){
vm.msg="密码为空!"
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 事业单位法律岗面试易错题题库试卷及参考答案2026
- 2027年备考会计基础 【零基础学员必练】全真模拟考试试题卷含答案
- 2027年备考会计基础 【零基础学员必练】通关提分题库及完整答案
- 2026年湖南生物机电职业技术学院高职单招笔试职业适应性测验历年参考题库含答案解析
- 2026年湖南外贸职业学院高职单招笔试语文历年参考题库含答案解析
- 2026年渤海理工职业学院高职单招笔试英语历年参考题库含答案解析
- 2026年海南外国语职业学院高职单招笔试综合素质历年参考题库含答案解析
- 2026年河北劳动关系职业学院高职单招笔试职业技能测验历年参考题库含答案解析
- 2026年江西财经职业学院高职单招笔试数学历年参考题库含答案解析
- 2026年江西制造职业技术学院高职单招笔试数学历年参考题库含答案解析
- 中医红外热像采集与分析技术规范
- 定语从句专项训练50题可打印
- 重复经颅磁刺激治疗与护理
- 四川省甘孜州泸定县冷碛镇冷碛小学-主题班会-好好说话,与爱同行【课件】
- 火灾与爆炸的预防措施
- 电力工程竣工验收报告及流程
- CFSLM治疗先天性重度上睑下垂的临床体会
- 2025年中国联通校园招聘笔试参考题库含答案解析
- 经导管主动脉瓣置换术围手术期共病诊治中国专家共识
- 钢结构监理实施细则
- 高填深挖路基监控量测施工方案 - 副本
评论
0/150
提交评论