java实现通过绑定邮箱找回密码功能
这篇文章主要为大家详细介绍了java实现通过绑定邮箱找回密码功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下本文实例为大家分享了java实现通过绑定邮箱找回密码功能,供大家参考,具体内容如下
1.输入用户名及验证码,验证用户名是否存在
(1).生成验证码工具类
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
/**
* @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.用户名验证通过后往绑定邮箱发送邮件
/**
* @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;
}
/**
* @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.邮件发送成功后进入邮箱,通过该链接进入修改密码请求,链接验证通过后进入修改密码页面
/**
* @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.输入新密码,验证成功后即修改成功
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持CodeAE代码之家。
原文链接:https://blog.csdn.net/u012498149/article/details/53022135
http://www.zzvips.com/article/176300.html
页:
[1]