本文实例为大家分享了java实现通过绑定邮箱找回密码功能,供大家参考,具体内容如下
1.输入用户名及验证码,验证用户名是否存在
(1).生成验证码工具类
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(这里用一句话描述这个类的作用)
* @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 ));
//画随机线
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);
}
//从另一方向画随机线
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);
}
//生成随机数,并将随机数字转换为字母
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).生成验证码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: 获取验证码
* @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 );
//表明生成的响应是图片
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 ).验证用户名是否存在
/**
* @description: 检查用户名是否存在
* @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.用户名验证通过后往绑定邮箱发送邮件
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: 发送邮件
* @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" );
//验证写信者邮箱,此处使用第三方授权码登陆,使用密码不知道为什么登录不上
authenticator authenticator = new popauthenticator( "123456789@qq.com" , "**************" );
//创建会话
session session = session.getinstance(prop,authenticator);
//填写信封写信
message msg = new mimemessage(session);
//设置发邮件的原地址
msg.setfrom( new internetaddress( "123456789@qq.com" ));
//设置接收人
msg.setrecipient(recipienttype.to, new internetaddress(stu.getemail()));
msg.setsubject( "找回密码!" );
msg.settext( this .createlink(stu));
//验证用户名密码发送邮件
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();
//设置过期时间
date outdate = new date(system.currenttimemillis() + 30 * 60 * 1000 ); // 30分钟后过期
system.out.println(system.currenttimemillis());
long date = outdate.gettime() / 1000 * 1000 ; // 忽略毫秒数 mysql 取出时间是忽略毫秒数的
//此处应该更新studentinfo表中的过期时间、密钥信息
stu.setoutdate(date);
stu.setvalidatacode(secretkey);
studentinfoservice.updatestudentinfo(stu);
//将用户名、过期时间、密钥生成链接密钥
string key =stu.getusernumber() + "$" + date + "$" + secretkey;
string digitalsignature = md5util.getmd5(key); // 数字签名
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 = "请勿回复本邮件.点击下面的链接,重设密码,本邮件超过30分钟,链接将会失效,需要重新申请找回密码." + resetpasshref;
return emailcontent;
}
|
3.邮件发送成功后进入邮箱,通过该链接进入修改密码请求,链接验证通过后进入修改密码页面
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: 该方法用于处理从邮箱链接过来的修改密码请求
* @author mr.chen
* @date 2016-11-3 下午02:24:17
*/
public string tofindpassword3(){
string message= "" ;
httpservletrequest request= servletactioncontext.getrequest();
//获取链接中的加密字符串
string sid=request.getparameter( "sid" );
//获取链接中的用户名
string id=request.getparameter( "id" );
if (stringutils.isblank(sid)||stringutils.isblank(id)){
system.out.println( "请求的链接不正确,请重新操作." );
message= "请求的链接不正确,请重新操作." ;
}
studentinfo stu=studentinfoservice.getstudentinfobyid( long .parselong(id));
if (stu!= null ){
//获取当前用户申请找回密码的过期时间
//找回密码链接已经过期
if (stu.getoutdate()<=system.currenttimemillis()){
system.out.println( "链接已经过期" );
message= "链接已经过期" ;
}
//获取当前登陆人的加密码
string key = stu.getusernumber()+ "$" +stu.getoutdate()/ 1000 * 1000 + "$" +stu.getvalidatacode(); //数字签名
string digitalsignature = md5util.getmd5(key); // 数字签名
if (!digitalsignature.equals(sid)){
system.out.println( "链接加密密码不正确" );
message= "链接加密密码不正确" ;
} else {
//验证成功,跳入到修改密码界面
request.setattribute( "stu" , stu);
}
} else {
system.out.println( "用户信息不存在" );
message= "用户信息不存在" ;
request.setattribute( "message" , message);
}
return success;
}
|
4.输入新密码,验证成功后即修改成功
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/u012498149/article/details/53022135