【來源網絡 http://www.2cto.com/kf/201502/376374.html】
一般大型網站我們登錄的時候,密碼忘了都有個功能可以找回密碼。
細數下大致的方法:
1.直接把密碼發送到你的郵箱去。一般是臨時密碼。 2.短信驗證,成本較高。 3.密保問題 4.發送一個鏈接到你郵箱點擊即可更改密碼。
個人認為第四種方法最經濟實惠,這次也主要都是在搞這個。
搞了一個晚上,單單郵件發送功能寫了快300行,雖然很多是注釋和空格,被舍友一說,用python只寫了20幾行,不禁膜拜PYTHON了!不過不管怎樣寫出來了,封裝好了,下次要用就方便了。代碼大部分是參考網上的和一些自己寫的。
首先說下思路:
參考:http://blog.yidongzhifu.net/2014/03/07/郵箱找回密碼功能的實現/ 用戶填寫自己的郵箱時,需要查看該郵箱是否與用戶ID綁定的郵箱想匹配,只有當匹配的時候才會發送郵件。 這封郵件中最重要的是一個鏈接地址: url = baseUrl + “?uid=” + uid + “&validkey=” + validkey; 這個地址含有兩個參數,id用戶的id,validkey驗證code,這是一個通過MD5加密過的字符串。 效驗MD5就是用來確保文件在傳輸過程中未被修改用的,這個加密過的字符串應該包含用戶的id+過期時間+隨機數 validkey=md5(uid + “|” + outdate + “|” + secretKey);
說白了,就是你要找回密碼,你就得先輸入你的帳號和郵箱。然后系統去判斷,帳號和郵箱是匹配的。那么就在這一瞬間再在另外一張表寫下信息(usrId,outdate,url)即你的ID,過期的時間(用現在的時間加上X分鍾的有效期),URL即發送的鏈接。
發送的鏈接 = baseUrl + “?uid=” + uid + “&validkey=” + validkey;
比如說我在本地的地址是localhost:8080/homeSeller/resetPassword.jsp這個是我重置密碼的頁面地址。那我再在地址欄加上’?’ 再在后面填寫傳入的屬性與對應的值即可傳值。 比如我的一次鏈接: localhost:8080/homeSeller/resetPassword.jsp?uid=zhuang123&validkey=36B0F10812DE6D2B0D3B2DC044F9A27D 意思就是傳入id,以及vaildkey vaildkey在之前我們已經寫入數據庫了!
填寫成功后,利用JAVAMAIL發送郵件到指定郵箱。然后你點擊那個鏈接,並且傳值。這個時候就在JSP中判斷一下對應userId的validkey是不是和數據庫中的一樣,以及currentTime是不是比outdate大即是否過期。如果都滿足的話就跳轉到更改密碼的頁面。更改密碼就是簡單的SQL,這里就不講了。
1 ok,下面講下代碼: 2 首先是 3 (1)數據庫層(DAO): 上面說的把信息插入數據庫的代碼(代碼都是挺簡單的增刪改查 4 5 //找回密碼,插入信息 ,這里的date是java.sql.Date 6 public int insertInfor(Connection con,String userId,String email,Timestamp date,String signature) throws SQLException 7 { 8 String sql = insert into findPass(userId,email,outdate,signature) values(?,?,?,?); 9 PreparedStatement pstmt = con.prepareStatement(sql); 10 pstmt.setString(1, userId); 11 pstmt.setString(2, email); 12 pstmt.setTimestamp(3,date); 13 pstmt.setString(4, signature); 14 15 int res = pstmt.executeUpdate(); 16 pstmt.close(); 17 con.close(); 18 return res; 19 } 20 21 //找回密碼,查詢是否可以修改密碼 22 public boolean isChangePass(String userId,String validkey) throws Exception 23 { 24 DbUtil dbUtil = new DbUtil(); 25 Connection con = dbUtil.getCon(); 26 String sql = select * from findPass where userId = ?; 27 PreparedStatement pstmt = con.prepareStatement(sql); 28 pstmt.setString(1, userId); 29 ResultSet res = pstmt.executeQuery(); 30 if(res.last()) 31 { 32 String signature = res.getString(signature); 33 if(!validkey.equals(signature)){ 34 pstmt.close(); 35 con.close(); 36 return false; 37 } 38 else{ 39 long current = System.currentTimeMillis(); 40 long time = res.getTimestamp(outdate).getTime(); 41 if(current> time){ 42 pstmt.close(); 43 con.close(); 44 return false; 45 } 46 else{ 47 pstmt.close(); 48 con.close(); 49 return true; 50 } 51 } 52 } 53 else{ 54 pstmt.close(); 55 con.close(); 56 return false; 57 } 58 }
1 (2)Servlet代碼 就是根據思路對應的處理 2 3 public void doPost(HttpServletRequest request, HttpServletResponse response) 4 throws ServletException, IOException { 5 6 //分割處理 7 String method = request.getParameter(method); 8 9 if(method.equals(find)){ 10 String userId = request.getParameter(userId); 11 String userEmail = request.getParameter(userEmail); 12 13 Connection con = null; 14 try { 15 con = dbUtil.getCon(); 16 } catch (Exception e) { 17 // TODO Auto-generated catch block 18 e.printStackTrace(); 19 } 20 21 boolean flag = false; 22 try { 23 flag = userDao.judgeUserEamil(userId, userEmail); 24 } catch (Exception e1) { 25 // TODO Auto-generated catch block 26 e1.printStackTrace(); 27 } 28 29 if(flag){ 30 long currentTime = System.currentTimeMillis() + 120000; 31 Date time = new Date(currentTime); 32 Timestamp ts = new Timestamp(time.getTime()); 33 Random random = new Random(); 34 String key = userId + | + ts + | + random.nextInt(); 35 String signature = MD5Util.MD5(key); 36 37 try { 38 int res = userDao.insertInfor(con, userId, userEmail, ts, signature); 39 if(res==1) 40 { 41 SendMail sendmail = new SendMail(); 42 String url = localhost:8080/homeSeller/resetPassword.jsp+?uid= + userId + &validkey= + signature; 43 sendmail.send(userEmail, url); 44 } 45 } catch (SQLException e) { 46 // TODO Auto-generated catch block 47 e.printStackTrace(); 48 } catch (AddressException e) { 49 // TODO Auto-generated catch block 50 e.printStackTrace(); 51 } catch (MessagingException e) { 52 // TODO Auto-generated catch block 53 e.printStackTrace(); 54 } 55 } 56 else 57 { 58 request.setAttribute(error, 用戶名和郵箱不匹配,請重新輸入!); 59 } 60 } 61 62 63 64 //重置密碼 65 else if (method.equals(reset)){ 66 67 String userId = request.getParameter(userid); 68 String password = request.getParameter(password1); 69 try { 70 Connection con = dbUtil.getCon(); 71 userDao.updatePassword(con, userId,password); 72 request.setAttribute(error, 修改成功,請重新登錄!); 73 request.getRequestDispatcher(login.jsp).forward(request, response); 74 } catch (Exception e) { 75 // TODO Auto-generated catch block 76 e.printStackTrace(); 77 } 78 79 80 }
1 (3)工具類 2 工具類一共有兩類 MD5加密,實現上面說的validkey的加密處理防止人工識別出來。 3 4 package com.homeSeller.util; 5 import java.security.MessageDigest; 6 import java.security.MessageDigest; 7 public class MD5Util { 8 public final static String MD5(String s) { 9 char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; 10 try { 11 byte[] btInput = s.getBytes(); 12 // 獲得MD5摘要算法的 MessageDigest 對象 13 MessageDigest mdInst = MessageDigest.getInstance(MD5); 14 // 使用指定的字節更新摘要 15 mdInst.update(btInput); 16 // 獲得密文 17 byte[] md = mdInst.digest(); 18 // 把密文轉換成十六進制的字符串形式 19 int j = md.length; 20 char str[] = new char[j * 2]; 21 int k = 0; 22 for (int i = 0; i < j; i++) { 23 byte byte0 = md[i]; 24 str[k++] = hexDigits[byte0 >>> 4 & 0xf]; 25 str[k++] = hexDigits[byte0 & 0xf]; 26 } 27 return new String(str); 28 } catch (Exception e) { 29 e.printStackTrace(); 30 return null; 31 } 32 } 33 public static void main(String[] args) { 34 System.out.println(MD5Util.MD5(20121221)); 35 System.out.println(MD5Util.MD5(加密)); 36 } 37 } 38 39 40 41 42 43 44 45 第二類是郵件發送類 46 參考http://www.cnblogs.com/codeplus/archive/2011/10/30/2229391.html JAVA MAIL是利用現有的郵件賬戶發送郵件的工具,比如說,我在網易注冊一個郵箱賬戶,通過JAVA Mail的操控,我可以不親自登錄網易郵箱,讓程序自動的使用網易郵箱發送郵件。這一機制被廣泛的用在注冊激活和垃圾郵件的發送等方面。 JavaMail可以到http://www.oracle.com/technetwork/java/javamail/index-138643.html進行下載,並將mail.jar添加到classpath即可。 47 48 JAVA郵件發送的大致過程是這樣的的: 49 50 1、構建一個繼承自javax.mail.Authenticator的具體類,並重寫里面的getPasswordAuthentication()方法。此類是用作登錄校驗的,以確保你對該郵箱有發送郵件的權利。 51 52 2、構建一個properties文件,該文件中存放SMTP服務器地址等參數。 53 54 3、通過構建的properties文件和javax.mail.Authenticator具體類來創建一個javax.mail.Session。Session的創建,就相當於登錄郵箱一樣。剩下的自然就是新建郵件。 55 56 4、構建郵件內容,一般是javax.mail.internet.MimeMessage對象,並指定發送人,收信人,主題,內容等等。 57 58 5、使用javax.mail.Transport工具類發送郵件。 59 60 61 62 這里我參考寫出來的郵件類是只支持SMTP 而不支持另外兩種的,所以也沒發時間去寫工廠類了。不過SMTP應該就夠了吧。 今天TX有BUG發送不出去換了個郵箱就OK了。 63 1、首先是繼承自javax.mail.Authenticator的一個具體類。getPasswordAuthentication()方法也就是構建一個PasswordAuthentication對象並返回,有點費解JAVA Mail這樣的設計意圖,可能是javax.mail.Authenticator為我們提供了附加的保證安全的驗證措施吧。 64 65 package com.homeSeller.util; 66 import javax.mail.Authenticator; 67 import javax.mail.PasswordAuthentication; 68 69 70 public class MailAuthenticator extends Authenticator{ 71 72 private String username; 73 74 75 private String password; 76 77 78 public MailAuthenticator(String username,String password) 79 { 80 this.username = username; 81 this.password = password; 82 } 83 84 85 String getPassword(){ 86 return password; 87 } 88 89 @Override 90 protected PasswordAuthentication getPasswordAuthentication(){ 91 return new PasswordAuthentication(username,password); 92 } 93 94 95 String getUsername(){ 96 return username; 97 } 98 99 100 public void setPassword(String password){ 101 this.password = password; 102 } 103 104 105 public void setUsername(String username){ 106 this.username = username; 107 } 108 }
1 2、郵件發送類,剩下的步驟都是在這個類實現的。代碼中的SimpleMail是封裝了郵件主題和內容的一個POJO。覺得在一個方法參數中既包含主題又包含內容,不太合適,故重載了此方法。還有就是因為大多數郵箱的SMTP服務器地址都是可以通過郵箱地址算出來,簡單起見,提供了一個不需要SMTP服務器地址的構造器。 2 3 package com.homeSeller.util; 4 import java.util.List; 5 import java.util.Properties; 6 7 import javax.mail.MessagingException; 8 import javax.mail.Session; 9 import javax.mail.Transport; 10 import javax.mail.internet.AddressException; 11 import javax.mail.internet.InternetAddress; 12 import javax.mail.internet.MimeMessage; 13 import javax.mail.internet.MimeMessage.RecipientType; 14 15 public class SimpleMailSender { 16 17 /* 18 * 簡單郵件發送器,可單發,群發 19 */ 20 21 22 /** 23 * 24 * 發送郵件的props文件 25 */ 26 27 28 private final transient Properties props = System.getProperties(); 29 30 /* 31 * 郵件服務器登錄驗證 32 */ 33 34 private transient MailAuthenticator authenticator; 35 36 /** 37 * 郵箱session 38 */ 39 40 private transient Session session; 41 42 /** 43 * 初始化郵件發送器 44 * 45 * @param smtpHostName 46 * SMTP郵件服務器地址 47 * @param username 48 * 發送郵件的用戶名(地址) 49 * @param password 50 * 發送郵件的密碼 51 */ 52 53 public SimpleMailSender(final String smtpHostName,final String username,final String password) 54 { 55 init(username,password,smtpHostName); 56 } 57 58 59 /** 60 * 初始化郵件發送器 61 * 62 * @param username 63 * 發送郵件的用戶名(地址),並以此解析SMTP服務器地址 64 * @param password 65 * 發送郵件的密碼 66 * 67 */ 68 69 public SimpleMailSender(final String username,final String password){ 70 //通過郵箱地址解析出smtp服務器,對大多數郵箱都管用 71 final String smtpHostName = smtp.+username.split(@)[1]; 72 init(username,password,smtpHostName); 73 } 74 75 76 /** 77 * 初始化 78 * 79 * @param username 80 * 發送郵件的用戶名(地址) 81 * @param password 82 * 密碼 83 * @param smtpHostName 84 * SMTP主機地址 85 */ 86 87 private void init(String username,String password,String smtpHostName) 88 { 89 //初始化 props 90 91 props.put(mail.smtp.auth,true); 92 props.put(mail.smtp.host,smtpHostName); 93 94 //驗證 95 authenticator = new MailAuthenticator(username,password); 96 97 //創建session 98 session = Session.getInstance(props,authenticator); 99 } 100 101 102 /** 103 * 發送郵件 104 * 105 * @param recipient 106 * 收件人郵箱地址 107 * @param subject 108 * 郵件主題 109 * @param content 110 * 郵件內容 111 * 112 * @throws AddressException 113 * @throws MessagingException 114 */ 115 116 public void send(String recipient,String subject,Object content) throws AddressException,MessagingException{ 117 118 //創建mime類型郵件 119 final MimeMessage message = new MimeMessage(session); 120 121 //設置發信人 122 message.setFrom(new InternetAddress(authenticator.getUsername())); 123 124 //設置收件人 125 126 message.setRecipient(RecipientType.TO,new InternetAddress(recipient)); 127 128 //設置主題 129 message.setSubject(subject); 130 131 //設置郵件內容 132 message.setContent(content.toString(),text/html;charset=utf-8); 133 134 //發送 135 Transport.send(message); 136 } 137 138 /** 139 * 140 * 群發郵件 141 * 142 * @param recipients 143 * 收件人們 144 * @param subject 145 * 主題 146 * @param content 147 * 內容 148 * throws AddressException 149 * throws MessagingException 150 */ 151 152 public void send(List recipients,String subject ,Object content) throws AddressException ,MessagingException{ 153 154 //創建Mime類型郵件 155 final MimeMessage message = new MimeMessage(session); 156 157 //設置發信人 158 message.setFrom(new InternetAddress(authenticator.getUsername())); 159 160 //設置收信人們 161 final int num = recipients.size(); 162 InternetAddress[] addresses = new InternetAddress[num]; 163 164 for(int i=0;i recipients,SimpleMail mail) throws AddressException,MessagingException 165 { 166 send(recipients,mail.getSubject(),mail.getContent()); 167 } 168 } 169 170 171 172 173 174 3.POJO:SimpleMail 175 176 package com.homeSeller.util; 177 /* 178 * SimpleMail 179 * PROJ 180 */ 181 public class SimpleMail { 182 private String Content; 183 private String Subject; 184 185 186 public String getContent() { 187 return Content; 188 } 189 public void setContent(String Content) { 190 this.Content = Content; 191 } 192 public String getSubject() { 193 return Subject; 194 } 195 public void setSubject(String Subject) { 196 this.Subject = Subject; 197 } 198 199 }
1 4.最終的用來發送的類 2 3 package com.homeSeller.util; 4 import java.util.List; 5 import java.util.ArrayList; 6 7 import javax.mail.MessagingException; 8 import javax.mail.internet.AddressException; 9 10 11 public class SendMail { 12 13 14 public void send(String email,String url) throws AddressException, MessagingException 15 { 16 SimpleMailSender sms = new SimpleMailSender(tianxia00115@163.com”,”password); 17 String recipients = email; 18 sms.send(recipients, HomeSeller找回密碼,尊敬的HomeSeller用戶,為了找回您的密碼,請在兩分鍾之內點擊以下連接:+url+ 如果不是您本人操作,請忽略此消息。); 19 } 20 21 22 23 public static void main(String[] args) throws AddressException, MessagingException{ 24 SimpleMailSender sms = new SimpleMailSender(tianxia00115@163.com,383160100033); 25 ArrayList recipients = new ArrayList(); 26 recipients.add(2867870421@qq.com); 27 28 for(String recipient:recipients){ 29 sms.send(recipients, test測試,hello hrwhisper.); 30 } 31 } 32 }
到這里基本OK 所有東西串接起來,就很好的實現了密碼找回了。
以前不懂原理,看到郵件發送來的都不知道是什么東西,現在懂了自然高興,學習應該建立在這種不斷學習不斷滿足的過程!
