版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
第Java實現(xiàn)圖片驗證碼功能在實現(xiàn)登錄功能時,一般為了安全都會設(shè)置驗證碼登錄,為了防止某個用戶用特定的程序暴力破解方式進(jìn)行不斷的嘗試登錄。常見驗證碼分為圖片驗證碼和短信驗證碼,還有滑動窗口模塊和選中指定物體驗證方式。下面通過Java來實現(xiàn)圖片驗證碼實例。
如上圖所示,圖片驗證碼由4個數(shù)字和一些彩色的干擾線段組成,點擊圖片可以更新驗證碼,只有輸入的驗證碼與圖片中的數(shù)字一致才能通過登錄,否則將會重新刷新驗證碼,重新輸入正確的驗證碼。
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);
//設(shè)置字體顏色
g.setColor(newColor(49,49,49));
//設(shè)置字體
g.setFont(newFont("SimSong",Font.ITALIC,20));
//繪制字符串;
Stringtext="";
for(inti=0;ii++){
text+=(int)(Math.random()*10);
}
//字符串輸出內(nèi)容,水平起始坐標(biāo),垂直起始坐標(biāo)。
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));
}
//設(shè)置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確認(rèn)密碼/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="確認(rèn)密碼不正確!"
}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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年寧夏體育職業(yè)學(xué)院單招綜合素質(zhì)考試模擬試題含詳細(xì)答案解析
- 2026年1月黑龍江大慶市肇州縣招聘公益性崗位人員35人考試重點試題及答案解析
- 2026年天津仁愛學(xué)院高職單招職業(yè)適應(yīng)性測試模擬試題及答案詳細(xì)解析
- 2026貴州六盤水六枝特區(qū)面向社會公開招聘事業(yè)單位工作人員35人考試重點題庫及答案解析
- 2026年景德鎮(zhèn)陶瓷職業(yè)技術(shù)學(xué)院單招職業(yè)技能考試備考試題含詳細(xì)答案解析
- 2026年西安市未央?yún)^(qū)漢城社區(qū)衛(wèi)生服務(wù)中心招聘(12人)考試重點題庫及答案解析
- 2026湖南長沙市芙蓉區(qū)教育局屬學(xué)校公開招聘小學(xué)編外合同制教師33人參考考試題庫及答案解析
- 2026年貴州經(jīng)貿(mào)職業(yè)技術(shù)學(xué)院單招職業(yè)技能考試備考題庫含詳細(xì)答案解析
- 2026年麗江市招聘事業(yè)單位工作人員(610人)參考考試試題及答案解析
- 2026年九江理工職業(yè)學(xué)院單招職業(yè)技能考試備考題庫含詳細(xì)答案解析
- 山西焦煤考試試題及答案
- GA 1812.1-2024銀行系統(tǒng)反恐怖防范要求第1部分:人民幣發(fā)行庫
- 48個國際音標(biāo)表教學(xué)資料
- 校園文化建設(shè)可行性報告
- 2025年春人教版(2024)小學(xué)數(shù)學(xué)一年級下冊教學(xué)計劃
- 特種設(shè)備生產(chǎn)(含安裝、改造、維修)單位質(zhì)量安全風(fēng)險管控清單
- 五年級下冊字帖筆順
- 非遺文化媽祖祭典文化知識
- Charter開發(fā)與立項流程(CDP)
- JTGT F20-2015 公路路面基層施工技術(shù)細(xì)則
- 七年級下冊《6.1 第3課時 平方根》課件
評論
0/150
提交評論