java实现邮件验证实现程序代码

作者:袖梨 2022-06-29

在之前总结了如何用java发送邮件,java实现数据库连接池和java的properties类的使用,对于实现这个需求我们就有了足够的技术储备,现在我们需要的就是如何把这些技术综合起来。


需求:我们的用户忘了密码,需要发送一个修改密码的链接到他的邮箱里去,用户点击链接便可

以修改密码。


总体思路:

当用户申请邮箱验证时,生成一个和用户名和当前时间绑定的md5值,存入验证数据库,同时将这个值拼接成验证连接发送到用户邮箱,当用户链接时和验证数据库的md5值比对,如果一致就给予用户修改密码的权限,同时删掉验证数据库中的数据。

详细设计:

采用MVC模型

数据库方面:

数据库: mail

表: verify,student

用户申请重置密码时,将数据写入verify,在邮箱验证时和verify比对,通过验证后,将信息添加到student,删除verify中内容.

java:(部分)

Model: student.java, md5.java, random.java

Control:reset.java, mailReset.java, mailVerify.java

View: reset.jsp, result.jsp, Exception.jsp

实现:(只贴出关键代码)

假设我们已经完成了连接池建设,邮件发送也已经配置完毕。

前边提到的数据库:

 代码如下 复制代码

Transact-SQL
create database if not exists mail;
use mail;
drop table if exists student;
create table student(
stu_name varchar(20) primary key,
stu_password varchar(50) not null,
stu_email varchar(50) not null
);
use mail;
drop table if exists verify;
create table verify(
stu_name varchar(20) primary key,
stu_nameMd5 varchar(50) not null,
randMd5 varchar(50) not null
);

然后是对verify表的连接和写入删除md5值的操作

 代码如下 复制代码

op con Java
package bean;
import java.sql.*;
public class ConDB {
 private Connection con;
 private Statement sta;
 private ResultSet res;
 private PreparedStatement pres;
 public ConDB() throws SQLException,ClassNotFoundException{
  Class.forName("com.mysql.jdbc.Driver");
   con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mail",
     "root","123456");
   sta=con.createStatement();
 }
    public void addStudentTemp(String stu_name,String stu_password,String stu_email)
    throws SQLException{
     String sql="insert into studentTemp values(?,?,?)";
     pres=con.prepareStatement(sql);
     pres.setString(1,stu_name);
     pres.setString(2,stu_password);
     pres.setString(3,stu_email);
     pres.executeUpdate();
    }
    public void addVerify(String stu_name,String stu_nameMd5,String randMd5)
    throws SQLException{
     String sql="insert into verify values(?,?,?)";
     pres=con.prepareStatement(sql);
     pres.setString(1,stu_name);
     pres.setString(2,stu_nameMd5);
     pres.setString(3,randMd5);
     pres.executeUpdate();
    }
    public void delVerify(String stu_name)
    throws SQLException{
     String sql="delete from verify where stu_name='"+stu_name+"'";
     sta.executeUpdate(sql);
    }
    public String getVerify(String stu_nameMd5,String randMd5)
    throws SQLException{
     String stu_name=null;
     String sql="select stu_name from verify where stu_nameMd5=? and randMd5=?";
     pres=con.prepareStatement(sql);
     pres.setString(1,stu_nameMd5);
     pres.setString(2,randMd5);
     res=pres.executeQuery();
     while(res.next()){
      stu_name=res.getString("stu_name");
     }
     return stu_name;
    }
    public void addStudent(String stu_name)
    throws SQLException{
     String sql="insert into student select * from studentTemp where stu_name='"+stu_name+"'";
     sta.executeUpdate(sql);
    }
    public String getStu_name(String stu_email)
    throws SQLException{
     String sql="select stu_name from student where stu_email='"+stu_email+"'";
     String stu_name=null;
     res=sta.executeQuery(sql);
     while(res.next()){
      stu_name=res.getString("stu_name");
     }
    return stu_name;
    }
    public void resetPassword(String stu_name,String stu_password)
    throws SQLException{
     String sql="update student set stu_password=? where stu_name=?";
     pres=con.prepareStatement(sql);
     pres.setString(1,stu_password);
     pres.setString(2,stu_name);
     pres.executeUpdate();
    }
    public boolean existStudent(String stu_name,String stu_password)
    throws SQLException{
     boolean exist=false;
     String sql="select * from student where stu_name=? and stu_password=?";
     pres=con.prepareStatement(sql);
     pres.setString(1,stu_name);
     pres.setString(2,stu_password);
     res=pres.executeQuery();
     if(res.next()){
      exist=true;
     }
     return exist;
    }
    public void addReset(String stu_name,String stu_nameMd5,
      String randMd5)
    throws SQLException {
     String sql="insert into reset(stu_name,stu_nameMd5,randMd5) values(?,?,?)";
      pres=con.prepareStatement(sql);
     pres.setString(1,stu_name);
     pres.setString(2,stu_nameMd5);
     pres.setString(3,randMd5);
     pres.executeUpdate();
    }
    public void delReset(String stu_name)
    throws SQLException {
     String sql="delete from reset where stu_name='"+stu_name+"'";
     sta.execute(sql);
    }
    public String getLegalReset(String stu_nameMd5,String randMd5)
    throws SQLException {
     String stu_name=null;
     String sql="select stu_name, timestampdiff(hour,sent_time,now()) as hours from reset where stu_nameMd5=? and randMd5=?";
     pres=con.prepareStatement(sql);
     pres.setString(1,stu_nameMd5);
     pres.setString(2,randMd5);
     res=pres.executeQuery();
     if(res.next()){
      stu_name=res.getString("stu_name");
      int h=res.getInt("hours");
      if(h<=24){
       return stu_name;
      }
     }
     return stu_name;
    }
 public void close()
 throws SQLException {
 {
  if (res != null) {
    res.close();
  }
  if (sta != null) {
    sta.close();
  }
  if (con != null) {
    con.close();
    con = null;
  }
 }
 }
}

发送包含构造的验证字符串的邮件:

 代码如下 复制代码

sendVerifyMail Java
package bean;
import java.util.*;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import javax.mail.Message.RecipientType;
import javax.mail.Transport;
import javax.mail.MessagingException;
public class sendMail {
 String username="[email protected]";
 private Message getMessage(){
  Properties p=new Properties();
  p.put("mail.transport.protocol","smtp");
  p.put("mail.smtp.host","smtp.163.com");
  p.put("mail.smtp.port","25");
  p.put("mail.smtp.auth","true");
  String password="20083111tian";
  MyAuthor auth=new MyAuthor(username,password);
  Session session=Session.getDefaultInstance(p,auth);
  Message message=new MimeMessage(session);
  return message;
 }
 public void sendVerify(String stu_email,String stu_nameMd5,String randMd5)
 throws MessagingException {
  Message message=getMessage();
   message.setFrom(new InternetAddress(username));
   message.setRecipient(RecipientType.TO,new InternetAddress(stu_email));
   message.setSentDate(new Date());
   message.setSubject("MailVerify");
   String m="" +
     "http:/127.0.01:8080/Mail/mailVerify?stu_nameMd5="+stu_nameMd5+"&randMd5="+randMd5+"
";
   message.setContent(m,"text/html;charset=gb2312");
   Transport.send(message);
  }
 public void sendReset(String stu_email,String stu_nameMd5,String randMd5)
 throws MessagingException {
  Message message=getMessage();
  message.setFrom(new InternetAddress(username));
  message.setRecipient(RecipientType.TO,new InternetAddress(stu_email));
  message.setSentDate(new Date());
  message.setSubject("MailVerify");
  String m="" +
    "http://127.0.01:8080/Mail/mailReset?stu_nameMd5="+stu_nameMd5+"&randMd5="+randMd5+"
";
  message.setContent(m,"text/html;charset=gb2312");
  Transport.send(message);
 }
}

 

对传入数据进行校验:

 代码如下 复制代码

mailVerify Java
package servlet;
import java.io.IOException;
import javax.servlet.http.*;
import javax.servlet.ServletException;
import java.sql.SQLException;
import bean.*;
public class mailVerify extends HttpServlet {
 public void doGet(HttpServletRequest request,
   HttpServletResponse response)
 throws ServletException,IOException {
  doPost(request,response);
 }
 public void doPost(HttpServletRequest request,
   HttpServletResponse response)
 throws ServletException,IOException {
  ConDB con;
  String stu_name;
  String msg;
  HttpSession session=request.getSession();
  String stu_nameMd5=request.getParameter("stu_nameMd5");
  String randMd5=request.getParameter("randMd5");
  try {
   con=new ConDB();
   stu_name=con.getVerify(stu_nameMd5,randMd5);
   if(stu_name!=null){
    msg="注册成功,请返回登录页面";
    con.addStudent(stu_name);
    con.delStudentTemp(stu_name);
    con.delVerify(stu_name);
   }else{msg="错误";}
   con.close();
   session.setAttribute("msg",msg);
   response.sendRedirect("result.jsp");
  }catch(ClassNotFoundException e){
   throw new ServletException(e.fillInStackTrace());
  }catch(SQLException e){
   throw new ServletException(e.fillInStackTrace());
  }
 }
}

如果校验成功,将修改后的数据插入数据库(关键方法):

 

 代码如下 复制代码

resetInfo Java
public void delVerify(String stu_name)
    throws SQLException{
     String sql="delete from verify where stu_name='"+stu_name+"'";
     sta.executeUpdate(sql);
    }
    public String getVerify(String stu_nameMd5,String randMd5)
    throws SQLException{
     String stu_name=null;
     String sql="select stu_name from verify where stu_nameMd5=? and randMd5=?";
     pres=con.prepareStatement(sql);
     pres.setString(1,stu_nameMd5);
     pres.setString(2,randMd5);
     res=pres.executeQuery();
     while(res.next()){
      stu_name=res.getString("stu_name");
     }
     return stu_name;
    }
    public void addStudent(String stu_name)
    throws SQLException{
     String sql="insert into student select * from studentTemp where stu_name='"+stu_name+"'";
     sta.executeUpdate(sql);
    }
    public String getStu_name(String stu_email)
    throws SQLException{
     String sql="select stu_name from student where stu_email='"+stu_email+"'";
     String stu_name=null;
     res=sta.executeQuery(sql);
     while(res.next()){
      stu_name=res.getString("stu_name");
     }
    return stu_name;
    }
    public void resetPassword(String stu_name,String stu_password)
    throws SQLException{
     String sql="update student set stu_password=? where stu_name=?";
     pres=con.prepareStatement(sql);
     pres.setString(1,stu_password);
     pres.setString(2,stu_name);
     pres.executeUpdate();
    }
    public boolean existStudent(String stu_name,String stu_password)
    throws SQLException{
     boolean exist=false;
     String sql="select * from student where stu_name=? and stu_password=?";
     pres=con.prepareStatement(sql);
     pres.setString(1,stu_name);
     pres.setString(2,stu_password);
     res=pres.executeQuery();
     if(res.next()){
      exist=true;
     }
     return exist;
    }

这样就能实现邮件验证的功能了

相关文章

精彩推荐