本文實(shí)例為大家分享了java實(shí)現(xiàn)通過(guò)綁定郵箱找回密碼功能,供大家參考,具體內(nèi)容如下
1.輸入用戶(hù)名及驗(yàn)證碼,驗(yàn)證用戶(hù)名是否存在
(1).生成驗(yàn)證碼工具類(lèi)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
package com.utils; import java.awt.color; import java.awt.font; import java.awt.graphics; import java.awt.image.bufferedimage; import java.util.hashmap; import java.util.map; import java.util.random; /** * @title: graphicsutil.java * @copyright * @package com.utils * @description: todo(這里用一句話(huà)描述這個(gè)類(lèi)的作用) * @author mr.chen * @date 2016-11-2 下午03:31:30 */ public class graphicsutil { private font mfont = new font( "times new roman" , font.plain, 17 ); 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); } public map<string, object> getgraphics(){ int width= 100 ,height= 18 ; bufferedimage image= new bufferedimage(width, height, bufferedimage.type_int_bgr); graphics g=image.getgraphics(); random random = new random(); g.setcolor(getrandcolor( 200 , 250 )); g.fillrect( 1 , 1 , width- 1 , height- 1 ); g.setcolor( new color( 102 , 102 , 102 )); g.drawrect( 0 , 0 , width- 1 , height- 1 ); g.setfont(mfont); g.setcolor(getrandcolor( 160 , 200 )); //畫(huà)隨機(jī)線(xiàn) for ( int i= 0 ;i< 155 ;i++){ int x = random.nextint(width - 1 ); int y = random.nextint(height - 1 ); int xl = random.nextint( 6 ) + 1 ; int yl = random.nextint( 12 ) + 1 ; g.drawline(x,y,x + xl,y + yl); } //從另一方向畫(huà)隨機(jī)線(xiàn) for ( int i = 0 ;i < 70 ;i++){ int x = random.nextint(width - 1 ); int y = random.nextint(height - 1 ); int xl = random.nextint( 12 ) + 1 ; int yl = random.nextint( 6 ) + 1 ; g.drawline(x,y,x - xl,y - yl); } //生成隨機(jī)數(shù),并將隨機(jī)數(shù)字轉(zhuǎn)換為字母 string srand= "" ; for ( int i= 0 ;i< 6 ;i++){ int itmp = random.nextint( 26 ) + 65 ; char ctmp = ( char )itmp; srand += string.valueof(ctmp); g.setcolor( new color( 20 +random.nextint( 110 ), 20 +random.nextint( 110 ), 20 +random.nextint( 110 ))); g.drawstring(string.valueof(ctmp), 15 *i+ 10 , 16 ); } g.dispose(); map<string, object> map= new hashmap<string, object>(); map.put( "rand" , srand); map.put( "image" , image); return map; } } |
(2).生成驗(yàn)證碼action
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
/** * @description: 獲取驗(yàn)證碼 * @author mr.chen * @date 2016-11-2 下午03:45:28 */ public void getcode(){ try { httpservletresponse response = servletactioncontext.getresponse(); httpservletrequest request= servletactioncontext.getrequest(); response.setheader( "pragma" , "no-cache" ); response.setheader( "cache-control" , "no-cache" ); response.setdateheader( "expires" , 0 ); //表明生成的響應(yīng)是圖片 response.setcontenttype( "image/jpeg" ); map<string, object> map= new graphicsutil().getgraphics(); system.out.println(map.get( "rand" )); request.getsession().setattribute( "rand" , map.get( "rand" )); imageio.write((renderedimage) map.get( "image" ), "jpeg" , response.getoutputstream()); } catch (ioexception e) { e.printstacktrace(); } } ( 3 ).驗(yàn)證用戶(hù)名是否存在 /** * @description: 檢查用戶(hù)名是否存在 * @author mr.chen * @date 2016-11-2 下午04:49:02 */ public void checkusernumber(){ try { httpservletresponse response = servletactioncontext.getresponse(); httpservletrequest request= servletactioncontext.getrequest(); string usernumber = request.getparameter( "usernumber" ); studentinfo stu= studentinfoservice.getstudentinfobyusernumber(usernumber); response.setcontenttype( "text/plain; charset=utf-8" ); response.setcharacterencoding( "utf-8" ); if (stu== null ){ response.getwriter().print( "false" ); } else { if (!stringutils.isblank(stu.getemail())){ response.getwriter().print( "true" ); } else { response.getwriter().print( "notemail" ); } } response.getwriter().flush(); response.getwriter().close(); } catch (ioexception e) { e.printstacktrace(); } } |
2.用戶(hù)名驗(yàn)證通過(guò)后往綁定郵箱發(fā)送郵件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
/** * @description: 發(fā)送郵件 * @author mr.chen * @date 2016-11-2 下午05:06:24 */ @suppresswarnings ( "static-access" ) public string tofindpassword2(){ httpservletrequest request= servletactioncontext.getrequest(); string usernumber = request.getparameter( "usernumber" ); studentinfo stu= studentinfoservice.getstudentinfobyusernumber(usernumber); try { properties prop = new properties(); prop.setproperty( "mail.transport.protocol" , "smtp" ); prop.setproperty( "mail.smtp.host" , "smtp.qq.com" ); prop.setproperty( "mail.smtp.auth" , "true" ); prop.put( "mail.smtp.port" , "587" ); prop.setproperty( "mail.debug" , "true" ); //驗(yàn)證寫(xiě)信者郵箱,此處使用第三方授權(quán)碼登陸,使用密碼不知道為什么登錄不上 authenticator authenticator = new popauthenticator( "123456789@qq.com" , "**************" ); //創(chuàng)建會(huì)話(huà) session session = session.getinstance(prop,authenticator); //填寫(xiě)信封寫(xiě)信 message msg = new mimemessage(session); //設(shè)置發(fā)郵件的原地址 msg.setfrom( new internetaddress( "123456789@qq.com" )); //設(shè)置接收人 msg.setrecipient(recipienttype.to, new internetaddress(stu.getemail())); msg.setsubject( "找回密碼!" ); msg.settext( this .createlink(stu)); //驗(yàn)證用戶(hù)名密碼發(fā)送郵件 transport transport = session.gettransport(); transport.send(msg); request.setattribute( "stu" , stu); return success; } catch (exception e){ e.printstacktrace(); } return error; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
/** * @description: 生成郵箱鏈接地址 * @author mr.chen * @date 2016-11-3 下午01:50:14 */ public string createlink(studentinfo stu){ //生成密鑰 string secretkey=uuid.randomuuid().tostring(); //設(shè)置過(guò)期時(shí)間 date outdate = new date(system.currenttimemillis() + 30 * 60 * 1000 ); // 30分鐘后過(guò)期 system.out.println(system.currenttimemillis()); long date = outdate.gettime() / 1000 * 1000 ; // 忽略毫秒數(shù) mysql 取出時(shí)間是忽略毫秒數(shù)的 //此處應(yīng)該更新studentinfo表中的過(guò)期時(shí)間、密鑰信息 stu.setoutdate(date); stu.setvalidatacode(secretkey); studentinfoservice.updatestudentinfo(stu); //將用戶(hù)名、過(guò)期時(shí)間、密鑰生成鏈接密鑰 string key =stu.getusernumber() + "$" + date + "$" + secretkey; string digitalsignature = md5util.getmd5(key); // 數(shù)字簽名 httpservletrequest request= servletactioncontext.getrequest(); string path=request.getcontextpath(); string basepath=request.getscheme()+ "://" +request.getservername()+ ":" +request.getserverport()+path; string resetpasshref = basepath + "/tofindpassword3.action?sid=" + digitalsignature + "&id=" +stu.getid(); string emailcontent = "請(qǐng)勿回復(fù)本郵件.點(diǎn)擊下面的鏈接,重設(shè)密碼,本郵件超過(guò)30分鐘,鏈接將會(huì)失效,需要重新申請(qǐng)找回密碼." + resetpasshref; return emailcontent; } |
3.郵件發(fā)送成功后進(jìn)入郵箱,通過(guò)該鏈接進(jìn)入修改密碼請(qǐng)求,鏈接驗(yàn)證通過(guò)后進(jìn)入修改密碼頁(yè)面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
/** * @description: 該方法用于處理從郵箱鏈接過(guò)來(lái)的修改密碼請(qǐng)求 * @author mr.chen * @date 2016-11-3 下午02:24:17 */ public string tofindpassword3(){ string message= "" ; httpservletrequest request= servletactioncontext.getrequest(); //獲取鏈接中的加密字符串 string sid=request.getparameter( "sid" ); //獲取鏈接中的用戶(hù)名 string id=request.getparameter( "id" ); if (stringutils.isblank(sid)||stringutils.isblank(id)){ system.out.println( "請(qǐng)求的鏈接不正確,請(qǐng)重新操作." ); message= "請(qǐng)求的鏈接不正確,請(qǐng)重新操作." ; } studentinfo stu=studentinfoservice.getstudentinfobyid( long .parselong(id)); if (stu!= null ){ //獲取當(dāng)前用戶(hù)申請(qǐng)找回密碼的過(guò)期時(shí)間 //找回密碼鏈接已經(jīng)過(guò)期 if (stu.getoutdate()<=system.currenttimemillis()){ system.out.println( "鏈接已經(jīng)過(guò)期" ); message= "鏈接已經(jīng)過(guò)期" ; } //獲取當(dāng)前登陸人的加密碼 string key = stu.getusernumber()+ "$" +stu.getoutdate()/ 1000 * 1000 + "$" +stu.getvalidatacode(); //數(shù)字簽名 string digitalsignature = md5util.getmd5(key); // 數(shù)字簽名 if (!digitalsignature.equals(sid)){ system.out.println( "鏈接加密密碼不正確" ); message= "鏈接加密密碼不正確" ; } else { //驗(yàn)證成功,跳入到修改密碼界面 request.setattribute( "stu" , stu); } } else { system.out.println( "用戶(hù)信息不存在" ); message= "用戶(hù)信息不存在" ; request.setattribute( "message" , message); } return success; } |
4.輸入新密碼,驗(yàn)證成功后即修改成功
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/u012498149/article/details/53022135