dwr实现找回密码的功能

编程技术  /  houtizong 发布于 3年前   68
需求:用户根据自己注册的账号、邮箱实现可以找回密码的功能。
实现:就是根据正确的账号、邮箱,实现自动发邮件的功能。

下面看我的实现过程:

先看两个配置文件:
1.db.properties
database.driver_class=oracle.jdbc.driver.OracleDriverdatabase.url=jdbc:oracle:thin:@webnet:1521:orcldatabase.username=baseextdatabase.password=11


2.mail.properties
[email protected]=*******mail.host=smtp.gmail.com


DAO层UnionUsersDAO查询数据库得到密码
public String getPassword(String account,String email) throws Exception{    InputStream inputStream =this.getClass().getClassLoader().getResourceAsStream("db.properties");        Properties p = new Properties();    try {    p.load(inputStream);    inputStream.close();} catch (IOException e1) {        e1.printStackTrace();    }       String drivers= p.getProperty("database.driver_class");String url= p.getProperty("database.url");String user= p.getProperty("database.username");String password = p.getProperty("database.password");    Connection conn = null;    Statement stat = null;    ResultSet rs = null;    String passWord = "";    try{        Class.forName(drivers);conn = DriverManager.getConnection(url, user, password);stat = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,                     ResultSet.CONCUR_READ_ONLY);String sql = "select c_password from union_users where c_account='"+account+"' and c_email ='"+email+"'";rs = stat.executeQuery(sql);while(rs.next()){passWord = rs.getString(1);}     }catch(Exception e){          e.printStackTrace();     }finally{ if(rs!=null){rs.close();rs=null;     }         if(stat!=null){stat.close();stat=null; } if(conn!=null){conn.close();conn=null; }     }     return passWord;} 



Service层UnionUsersServiceImpl
public String getPassword(String account, String email){try {return this.getUnionUsersDao().getPassword(account, email);} catch (Exception e) {e.printStackTrace();}return "";}



applicationContext.xml中相关配置
         <!--  dao 注入--><bean id="UnionUsersDAO" class="com.ao.dao.UnionUsersDAO"><property name="sessionFactory"><ref bean="sessionFactory" /></property></bean>         <!--  service 注入--><bean id="unionUsersService"class="com.ao.service.impl.UnionUsersServiceImpl"><property name="unionUsersDao"><ref bean="UnionUsersDAO" /></property></bean>


dwr.xml中的配置
       <create creator="spring" javascript="UsersService" scope="application">      <param name="beanName" value="unionUsersService" />        </create>        <create creator="new" javascript="SendMail" scope="application">      <param name="class" value="com.ao.web.util.SendMail" />        </create>


com.ao.web.util.SendMail
package com.ao.web.util;import java.io.IOException;import java.io.InputStream;import java.util.Date;import java.util.Properties;import javax.activation.DataHandler;import javax.activation.DataSource;import javax.activation.FileDataSource;import javax.mail.Authenticator;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.Multipart;import javax.mail.PasswordAuthentication;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeBodyPart;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMultipart;public class SendMail {// Smtp服务IP;private String host = null;// 发送者邮箱;private String from = null;// 接收者邮箱;private String to = null;// 本地附件;private String fileAttachment = null;// 邮件主题;private String subject = null;// 邮件内容;private String text = null;public String getFileAttachment() {return fileAttachment;}public void setFileAttachment(String fileAttachment) {this.fileAttachment = fileAttachment;}public String getFrom() {return from;}public void setFrom(String from) {this.from = from;}public String getHost() {return host;}public void setHost(String host) {this.host = host;}public String getSubject() {return subject;}public void setSubject(String subject) {this.subject = subject;}public String getText() {return text;}public void setText(String text) {this.text = text;}public String getTo() {return to;}public void setTo(String to) {this.to = to;}public boolean sendM() {try {// system propertiesjava.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());final Properties props = new Properties();props.put("mail.transport.protocol", "smtp");props.put("mail.smtp.starttls.enable", "true");props.put("mail.smtp.host", host);props.put("mail.smtp.port", "465");props.put("mail.smtp.timeout", "25000");props.put("mail.smtp.auth", "true");props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");props.put("mail.smtp.socketFactory.fallback", "false");InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("mail.properties");Properties p = new Properties();try {p.load(inputStream);inputStream.close();} catch (IOException e1) {e1.printStackTrace();}final String account = p.getProperty("mail.account");final String password = p.getProperty("mail.password");// 获取 sessionSession sendMailSession = Session.getInstance(props,new Authenticator() {public PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(account, password);}});// 声名 messageMimeMessage message = new MimeMessage(sendMailSession);message.setFrom(new InternetAddress(from));message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));message.setSubject(subject);message.setSentDate(new Date());// 建立 message partMimeBodyPart messageBodyPart = new MimeBodyPart();// 内容;messageBodyPart.setText(text);Multipart multipart = new MimeMultipart();multipart.addBodyPart(messageBodyPart);// 附件;messageBodyPart = new MimeBodyPart();if (fileAttachment != null && !fileAttachment.equals("")) {DataSource source = new FileDataSource(fileAttachment);messageBodyPart.setDataHandler(new DataHandler(source));messageBodyPart.setFileName(fileAttachment);multipart.addBodyPart(messageBodyPart);}message.setContent(multipart);// 发送邮件;Transport.send(message);return true;} catch (MessagingException m) {m.printStackTrace();return false;}}public int send(String to, String title, String text, String path) {InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("mail.properties");Properties p = new Properties();try {p.load(inputStream);inputStream.close();} catch (IOException e1) {e1.printStackTrace();}String account = p.getProperty("mail.account");String host = p.getProperty("mail.host");SendMail sm = new SendMail();sm.setFileAttachment(path); // 本地附件;sm.setFrom(account); // 发送者邮箱;sm.setTo(to); // 接收者邮箱;sm.setHost(host); // Smtp服务IP;sm.setSubject(title); // 邮件主题sm.setText(text); // 邮件内容int i = -1;if (sm.sendM()) {i = 1;} else {i = 2;}return i;}}



getPassword.jsp中调用
<script type='text/javascript' src='<%=path%>/dwr/interface/UsersService.js'></script><script type='text/javascript' src='<%=path%>/dwr/interface/SendMail.js'></script>        <script type='text/javascript' src='<%=path%>/dwr/engine.js'></script><script type='text/javascript' src='<%=path%>/dwr/util.js'></script><script type="text/javascript">function Trim(str){             return str.replace(/^\s*|\s*$/g,"");          }function getPassword(){var account = document.getElementById("account").value;var email = document.getElementById("email").value;//var passCode = document.getElementById("getPassword_passCode").value; if(Trim(account)==""||Trim(email)==""){     alert("所填信息均不能为空!");     return ;     }     if (email !=""&&email.search(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/ ) == -1){         alert("邮箱格式不对!");     return ;     }          UsersService.getPassword(account,email,function(result){     if(result==""){     window.alert("账号或邮箱错误!");     }else {          var title = "找回密码";         var text = "你的账号密码为:"+result+",请妥善保管密码!";         var path = "";//附件路径     SendMail.send(email, title, text, path,function(date){        if(date==1){         window.alert("密码已经发送到你的邮箱!");         window.close();     }else{         window.alert("密码发送到失败,请稍后再试!");     }     });     }     });}        </script>


请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客