日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用邮箱注册帐户以及激活

發布時間:2023/12/10 编程问答 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用邮箱注册帐户以及激活 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

思路:

1.注冊帳戶時把用戶存入數據庫并且設置用戶狀態不可用,同時給注冊的郵箱發郵件。

2.郵箱的內容應該是鏈接到項目的激活方法,并且傳入參數(注冊的郵箱和驗證碼)。(http://localhost:8080/email/user/register?action=activate&email=1434244213@qq.com&validateCode=b4dc9b79b75d9aa7d6c332e780a375c2)

3.點擊鏈接會對郵箱、驗證碼、激活時間進行驗證,如果激活成功,更改用戶狀態為可用。

?

service層代碼

import java.text.ParseException; import java.util.Date;import javax.servlet.http.HttpServletRequest;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.app.dao.UserDao; import com.app.tools.MD5Util; import com.app.tools.SendEmail; import com.app.tools.SendMail; import com.app.tools.ServiceException; import com.code.model.UserModel;/*** * @author BuNuo*/ @Service public class RegisterValidateService {@Autowiredprivate UserDao userDao;@Autowiredprivate HttpServletRequest request;/*** 處理注冊*/public void processregister(String email){UserModel user=new UserModel();Long as=5480l;user.setId(as);user.setName("BuNuo");user.setPassword("111111");user.setEmail(email);user.setRegisterTime(new Date());user.setStatus(0);///如果處于安全,可以將激活碼處理的更復雜點,這里我稍做簡單處理//user.setValidateCode(MD5Tool.MD5Encrypt(email)); user.setValidateCode(MD5Util.encode2hex(email));userDao.save(user);//保存注冊信息///郵件的內容StringBuffer sb=new StringBuffer("點擊下面鏈接激活賬號,48小時生效,否則重新注冊賬號,鏈接只能使用一次,請盡快激活!</br>");String url = request.getScheme() //當前鏈接使用的協議+"://" + request.getServerName()//服務器地址 + ":" + request.getServerPort() //端口號 + request.getContextPath(); //應用名稱,如果應用名稱為sb.append("<a href="+url+"/user/register?action=activate&email=");sb.append(email); sb.append("&validateCode="); sb.append(user.getValidateCode());sb.append("\">http://localhost:8088/email/user/register?action=activate&email="); sb.append(email);sb.append("&validateCode=");sb.append(user.getValidateCode());sb.append("</a>");//發送郵件//new SendMail().sendMail(email, sb.toString());new SendEmail().send(email, sb.toString());System.out.println("發送郵件");}/*** 處理激活* @throws ParseException *////傳遞激活碼和email過來public void processActivate(String email , String validateCode)throws ServiceException, ParseException{ //數據訪問層,通過email獲取用戶信息UserModel user=userDao.find(email);//驗證用戶是否存在 if(user!=null){ //驗證用戶激活狀態 if(user.getStatus()==0){ ///沒激活Date currentTime = new Date();//獲取當前時間 //驗證鏈接是否過期 currentTime.before(user.getRegisterTime());if(currentTime.before(user.getLastActivateTime())) { //驗證激活碼是否正確 if(validateCode.equals(user.getValidateCode())) { //激活成功, //并更新用戶的激活狀態,為已激活 System.out.println("==sq==="+user.getStatus());user.setStatus(1);//把狀態改為激活System.out.println("==sh==="+user.getStatus());userDao.update(user);} else { System.out.println("激活碼不正確"); } } else { System.out.println("激活碼已過期!"); } } else {System.out.println("郵箱已激活,請登錄!"); } } else {System.out.println("該郵箱未注冊(郵箱地址不存在)!"); } } }

?

MD5Util.java

import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class MD5Util {/*** 將源字符串使用MD5加密為字節數組* @param source* @return*/public static byte[] encode2bytes(String source) {byte[] result = null;try {MessageDigest md = MessageDigest.getInstance("MD5");md.reset();md.update(source.getBytes("UTF-8"));result = md.digest();} catch (NoSuchAlgorithmException e) {e.printStackTrace();} catch (UnsupportedEncodingException e) {e.printStackTrace();}return result;}/*** 將源字符串使用MD5加密為32位16進制數* @param source* @return*/public static String encode2hex(String source) {byte[] data = encode2bytes(source);StringBuffer hexString = new StringBuffer();for (int i = 0; i < data.length; i++) {String hex = Integer.toHexString(0xff & data[i]);if (hex.length() == 1) {hexString.append('0');}hexString.append(hex);}return hexString.toString();}/*** 驗證字符串是否匹配* @param unknown 待驗證的字符串* @param okHex 使用MD5加密過的16進制字符串* @return 匹配返回true,不匹配返回false*/public static boolean validate(String unknown , String okHex) {return okHex.equals(encode2hex(unknown));}}

?

SendEmail.java ? ?發送郵件的方法,調用此方法傳入郵箱和發送內容即可(new SendEmail().send(email, content);)

package com.app.tools; import java.util.Date; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage;/*** * @author BuNuo*/ public class SendEmail {public static final String HOST = "smtp.163.com";public static final String PROTOCOL = "smtp"; public static final int PORT = 8080;public static final String FROM = "";//發件人的emailpublic static final String PWD = "";//發件人密碼/*** 獲取Session* @return*/private static Session getSession() {Properties props = new Properties();props.put("mail.smtp.host", HOST);//設置服務器地址//props.put("mail.store.protocol" , PROTOCOL);//設置協議//props.put("mail.smtp.port", PORT);//設置端口props.put("mail.smtp.auth" , "true");Authenticator authenticator = new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(FROM, PWD);}};Session session = Session.getDefaultInstance(props , authenticator);return session;}public void send(String toEmail , String content) {Session session = getSession();try {System.out.println("--send--"+content);// Instantiate a messageMessage msg = new MimeMessage(session);//Set message attributesmsg.setFrom(new InternetAddress(FROM));InternetAddress[] address = {new InternetAddress(toEmail)};msg.setRecipients(Message.RecipientType.TO, address);msg.setSubject("賬號激活郵件");msg.setSentDate(new Date());msg.setContent(content , "text/html;charset=utf-8");//Send the message Transport.send(msg);}catch (MessagingException mex) {mex.printStackTrace();}} }

?demo地址:http://download.csdn.net/detail/qq_33347991/9711788

轉載于:https://www.cnblogs.com/bunuo/p/6095050.html

總結

以上是生活随笔為你收集整理的使用邮箱注册帐户以及激活的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。