其找回密码的流程如下:
第一步:输入用户名,系统查找是否存在该用户,如果存在则进行下一步,并给出用户的邮箱;否则提示"不存在该用户"
第二步:存在该用户后,则进行用户的邮箱验证,每个用户绑定一个QQ邮箱,点击"发送验证码"按钮,系统会给该邮箱发送一条包含验证码的邮件,发送成功,系统会在前端显示"验证码已经发送到你的邮箱,请查看"。用户填入该验证码,并且两者相匹配,则可以进行下一步,否则无法进行下一步,会提示"输入的邮箱验证码错误"
第三步:查看邮箱收到的邮件,复制验证码,填入系统第二步框中,进行第三步验证。通过前两步的验证,已经可以验证,操作者就是本人,所以第三步,用户可以直接修改密码,覆盖原密码
第四步:成功找回密码,是一个新的密码,用户可以返回登录页面进行登录
部分源码:
MailUtil.java(发送邮件工具类)
package com.utils;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
/*
* 管理员发送邮件的配置类
* 作者:海哥
* 时间:2017年8月20日
*/
public class MailUtil {
public void SendMessage(String receiver, String title, String content)
throws AddressException,MessagingException{
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");// 连接协议
properties.put("mail.smtp.host", "smtp.qq.com");// 主机名
properties.put("mail.smtp.port", 465);// 端口号
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true");//设置是否使用ssl安全连接 ---一般都使用
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.socketFactory.port", "465");
properties.put("mail.debug", "true");//设置是否显示debug信息 true 会在控制台显示相关信息
//得到回话对象
Session session = Session.getInstance(properties);
// 获取邮件对象
Message message = new MimeMessage(session);
//设置发件人邮箱地址
message.setFrom(new InternetAddress("linhaiyun4571@qq.com"));
//设置收件人地址
message.setRecipients(RecipientType.TO,new InternetAddress[] { new InternetAddress(receiver) });
//设置邮件标题
message.setSubject(title);
//设置邮件内容
message.setText(content);
//得到邮差对象
Transport transport = session.getTransport();
//连接自己的邮箱账户
transport.connect("linhaiyun4571@qq.com", "输入你自己的授权码");//密码为刚才得到的授权码
//发送邮件
transport.sendMessage(message, message.getAllRecipients());
System.out.println("邮件正在发送!");
}
}
SendMessage.java(触发发送邮件的控制类)
package com.servlet;
import java.io.IOException;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bean.User;
import com.utils.MailUtil;
@WebServlet("/SendMessage")
public class SendMessage extends HttpServlet {
private static final long serialVersionUID = 1L;
public SendMessage() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//设置编码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Type","text/html; charset=utf-8");
try {
MailUtil mail = new MailUtil();
int num = (int)((Math.random()*9+1)*100000); //生成六位验证码随机数
request.getSession().setAttribute("num", num); //设置东西保存验证码
User user = (User) request.getSession().getAttribute("finduser"); //得到修改密码对象的Object
String receiver = user.getUser_mail();
String title = "员工找回密码";
String content = "这是高级验证系统发来的邮件,你正在执行该系统找回密码的操作,你的邮箱验证码为:"+ String.valueOf(num) + ",请不要告诉别人哦!";
mail.SendMessage(receiver, title, content);
request.getSession().setAttribute("success","验证码已经发送到你的邮箱,请查看!"); //设置东西保存验证码
} catch (AddressException e) {
// TODO Auto-generated catch block
System.out.println("验证码发送失败");
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.getRequestDispatcher("SecondSteps.jsp").forward(request, response);
}
}
Validation.java(邮箱验证码匹配验证)
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bean.User;
@WebServlet("/Validation")
public class Validation extends HttpServlet {
private static final long serialVersionUID = 1L;
public Validation() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//设置编码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Type","text/html; charset=utf-8");
String email = request.getParameter("email");
String fycodes = request.getParameter("fycodes"); //邮箱验证码
int code = Integer.parseInt(fycodes); //字符串转换为整形
int num = (int) request.getSession().getAttribute("num"); //得到刚发送的邮箱验证码
System.out.println("你的邮箱验证码为:"+num);
if(code == num){ //判断输入的验证码是否和邮箱收到的一致
System.out.println("验证成功!");
// request.getSession().removeAttribute("num");
request.getSession().removeAttribute("success");
request.getRequestDispatcher("ThirdSteps.jsp").forward(request, response);
}
else{
request.getSession().setAttribute("ValidaError", "输入的邮箱验证码错误!"); //设置东西保存验证码
request.getRequestDispatcher("SecondSteps.jsp").forward(request, response);
}
}
}