最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java 注冊時發(fā)送激活郵件和激活的實(shí)現(xiàn)示例

 更新時間:2017年07月17日 09:06:13   作者:ganchuanpu  
這篇文章主要介紹了Java 注冊時發(fā)送激活郵件和激活的實(shí)現(xiàn)示例的相關(guān)資料,需要的朋友可以參考下

Java 注冊時發(fā)送激活郵件和激活的實(shí)現(xiàn)示例

最近從項目分離出來的注冊郵箱激活功能,整理一下,方便下次使用

1.RegisterController.java

package com.app.web.controller;
import java.text.ParseException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.app.service.impl.RegisterValidateService;
import com.app.tools.ServiceException;
 
@Controller
public class RegisterController {
 
  @Resource
  private RegisterValidateService service;
 
  @RequestMapping(value="/user/register",method={RequestMethod.GET,RequestMethod.POST})
  public ModelAndView load(HttpServletRequest request,HttpServletResponse response) throws ParseException{
    String action = request.getParameter("action");
    ModelAndView mav=new ModelAndView();
    if("register".equals(action)) {
      //注冊
      String email = request.getParameter("email");
      service.processregister(email);//發(fā)郵箱激活
      mav.addObject("text","注冊成功");
      mav.setViewName("register/register_success");
    }
    else if("activate".equals(action)) {
      //激活
      String email = request.getParameter("email");//獲取email
      String validateCode = request.getParameter("validateCode");//激活碼
      try {
        service.processActivate(email , validateCode);//調(diào)用激活方法
        mav.setViewName("register/activate_success");
      } catch (ServiceException e) {
        request.setAttribute("message" , e.getMessage());
        mav.setViewName("register/activate_failure");
      }
    }
    return mav;
  }
  
}
 

2.RegisterValidateService.java

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

3.UserModel.java

package com.code.model;
import java.beans.Transient;
import java.util.Calendar;
import java.util.Date;
 
public class UserModel {
  private Long id;
 private String name;
 private String password;
 private String email;//注冊賬號
 private int status=0;//激活狀態(tài)
 private String validateCode;//激活碼
 private Date registerTime;//注冊時間
 
   
  /////////////////
  public Long getId() {
    return id;
  }
 
  public void setId(Long id) {
    this.id = id;
  }
 
  public String getName() {
    return name;
  }
 
  public void setName(String name) {
    this.name = name;
  }
 
  public String getPassword() {
    return password;
  }
 
  public void setPassword(String password) {
    this.password = password;
  }
 
  public String getEmail() {
    return email;
  }
 
  public void setEmail(String email) {
    this.email = email;
  }
 
  public int getStatus() {
    return status;
  }
 
  public void setStatus(int status) {
    this.status = status;
  }
 
  public String getValidateCode() {
    return validateCode;
  }
 
  public void setValidateCode(String validateCode) {
    this.validateCode = validateCode;
  }
 
  public Date getRegisterTime() {
    return registerTime;
  }
 
  public void setRegisterTime(Date registerTime) {
    this.registerTime = registerTime;
  }
  /////////////////////////
  @Transient
  public Date getLastActivateTime() {
    Calendar cl = Calendar.getInstance();
    cl.setTime(registerTime);
    cl.add(Calendar.DATE , 2);
 
    return cl.getTime();
  }
 
}

4.SendEmail.java

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;
 
public class SendEmail {
 
  public static final String HOST = "smtp.163.com";
  public static final String PROTOCOL = "smtp"; 
  public static final int PORT = 25;
  public static final String FROM = "xxxxx@xx.com";//發(fā)件人的email
  public static final String PWD = "123456";//發(fā)件人密碼
 
  /**
   * 獲取Session
   * @return
   */
  private static Session getSession() {
    Properties props = new Properties();
    props.put("mail.smtp.host", HOST);//設(shè)置服務(wù)器地址
    props.put("mail.store.protocol" , PROTOCOL);//設(shè)置協(xié)議
    props.put("mail.smtp.port", PORT);//設(shè)置端口
    props.put("mail.smtp.auth" , true);
 
    Authenticator authenticator = new Authenticator() {
      @Override
      protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(FROM, PWD);
      }
 
    };
    Session session = Session.getDefaultInstance(props , authenticator);
 
    return session;
  }
 
  public static void send(String toEmail , String content) {
    Session session = getSession();
    try {
      System.out.println("--send--"+content);
      // Instantiate a message
      Message msg = new MimeMessage(session);
 
      //Set message attributes
      msg.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();
    }
  }
}

5.jsp頁面  

 registerEmailValidae.jsp

<h2>注冊激活</h2>
<form action="user/register?action=register" method="post">
   Email:<input type="text" id="email" name="email" value="" >
   <input type="submit" value="提交">
</form>

register_success.jsp

<body>
  恭喜你注冊成功!請到注冊的郵箱點(diǎn)擊鏈接激活!
 </body>

activate_success.jsp:

<body>
  賬號激活成功,點(diǎn)擊這里去登錄!
 </body>

activate_failure.jsp:

<body>
  激活失?。″e誤信息:${message }
</body>

以上就是Java 注冊時發(fā)送激活郵件和激活的簡單實(shí)例,如有疑問請留言討論,共同進(jìn)步,關(guān)于java開發(fā)的文章本站還有很多,

歡迎大家,搜索參閱,謝謝大家對本站的支持!

相關(guān)文章

  • springboot整合token的實(shí)現(xiàn)代碼

    springboot整合token的實(shí)現(xiàn)代碼

    這篇文章主要介紹了springboot整合token的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • spring boot jpa寫原生sql報Cannot resolve table錯誤解決方法

    spring boot jpa寫原生sql報Cannot resolve table錯誤解決方法

    在本篇文章里小編給大家整理的是關(guān)于spring boot jpa寫原生sql報Cannot resolve table錯誤的解決方法,需要的朋友學(xué)習(xí)下。
    2019-11-11
  • 圖文詳解Java線程和線程池

    圖文詳解Java線程和線程池

    下面小編就為大家?guī)硪黄斦凧ava的線程和線程池。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-11-11
  • Java 爬蟲工具Jsoup詳解

    Java 爬蟲工具Jsoup詳解

    這篇文章主要介紹了 Java 爬蟲工具Jsoup詳解的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Spring bean的實(shí)例化和IOC依賴注入詳解

    Spring bean的實(shí)例化和IOC依賴注入詳解

    這篇文章主要介紹了Spring bean的實(shí)例化和IOC依賴注入詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • Redis集群原理詳細(xì)分析

    Redis集群原理詳細(xì)分析

    Redis集群實(shí)現(xiàn)了對Redis的水平擴(kuò)容,即啟動N個redis節(jié)點(diǎn),將整個數(shù)據(jù)庫分布存儲在這N個節(jié)點(diǎn)中,每個節(jié)點(diǎn)存儲總數(shù)據(jù)的1/N。Redis集群通過分區(qū)來提供一定程度的可用,即使集群中有一部分節(jié)點(diǎn)失效或者無法進(jìn)行通訊,集群也可以繼續(xù)處理命令請求
    2022-12-12
  • java實(shí)現(xiàn)俄羅斯方塊

    java實(shí)現(xiàn)俄羅斯方塊

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)俄羅斯方塊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • IntelliJ IDEA各種圖標(biāo)的含義

    IntelliJ IDEA各種圖標(biāo)的含義

    這篇文章主要介紹了IntelliJ IDEA各種圖標(biāo)的含義,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • 解決springboot 無法配置多個靜態(tài)路徑的問題

    解決springboot 無法配置多個靜態(tài)路徑的問題

    這篇文章主要介紹了解決springboot 無法配置多個靜態(tài)路徑的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 深入理解jvm啟動參數(shù)

    深入理解jvm啟動參數(shù)

    JVM的啟動參數(shù)是在啟動JVM時可以設(shè)置的一些命令行參數(shù),本文詳細(xì)的介紹了深入理解jvm啟動參數(shù),具有一定的參考價值,感興趣的可以了解一下
    2023-08-08

最新評論

德阳市| 嘉兴市| 宣恩县| 霸州市| 武城县| 依兰县| 濮阳市| 比如县| 峨边| 理塘县| 隆化县| 民县| 永春县| 崇阳县| 双鸭山市| 海林市| 烟台市| 凉城县| 扶绥县| 光山县| 乐东| 尼木县| 乌兰察布市| 海盐县| 张家界市| 两当县| 泰来县| 五指山市| 平山县| 磴口县| 凤台县| 开化县| 资兴市| 南昌市| 响水县| 镇江市| 大丰市| 神池县| 额济纳旗| 云霄县| 榆社县|