SSH邮箱验证与激活

下面是我写的email验证和激活:

自己瞎写的,能用,不喜欢勿喷

    action中regist方法中代码

 1 /**
 2          * 
 3          * 发送邮件的方法
 4          */
 5         StringBuffer sb=new StringBuffer("点击下面链接激活账号,48小时生效,否则重新注册账号,链接只能使用一次,请尽快激活!<br>");
 6         sb.append("http://localhost:**********action?email="); 
 7         sb.append(user.getEmail());
 8         sb.append("&code=");
 9         sb.append(user.getCode());
10         sb.append("");
11          
12         //发送邮件
13         SendEmail.send(user.getEmail(), sb.toString());

    action中处理激活的方法

 1 /**
 2      * 处理激活
 3      */
 4     public String active() throws ServiceException, ParseException {
 5         //获得原生态rquest
 6         HttpServletRequest request = (HttpServletRequest) ActionContext
 7                 .getContext()
 8                 .get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
 9         service.processActivate(request.getParameter("email"), request.getParameter("code"));
10         this.addActionMessage("激活成功,请点击去登录");
11         return "succ";
12     }

    service层处理激活的方法(我的代码激活清空了激活码,你也可以选择不清空激活码)

 1 /**
 2      * 处理激活
 3      * @throws ParseException 
 4      */
 5       ///传递激活码和email过来
 6     public void processActivate(String email , String code)throws ServiceException, ParseException{  
 7          //数据访问层,通过email获取用户信息
 8         List<User> list=dao.findByEmail(email);
 9         User users=list.get(0);
10         //验证用户是否存在 
11         if(users!=null) {  
12             //验证用户激活状态  
13             if(users.getState()==0) { 
14                 ///没激活
15                 Date currentTime = new Date();//获取当前时间  
16                 //验证链接是否过期 
17                 currentTime.before(users.getRegisterTime());
18                 if(currentTime.before(users.getLastActivateTime())) {  
19                     //验证激活码是否正确  
20                     if(code.equals(users.getCode())) {  
21                         //激活成功, //并更新用户的激活状态,为已激活 
22                         users.setState(1);//把状态改为激活
23                         users.setCode("");//把激活码清空
24                         dao.update(users);
25                     } else {  
26                        throw new ServiceException("激活码不正确");  
27                     }  
28                 } else { throw new ServiceException("激活码已过期!");  
29                 }  
30             } else {
31                throw new ServiceException("邮箱已激活,请登录!");  
32             }  
33         } else {
34             throw new ServiceException("该邮箱未注册(邮箱地址不存在)!");  
35         }  
36            
37     }

    Service层中的涉及到的update方法,下面的代码写在dao层,service层中还有一个findByEmail()方法我就不贴出来了

1 @Override
2      public void update(User user)
3         {
4             Session session = sessionFactory.getCurrentSession();
5             session.update(user);
6         }
原文地址:https://www.cnblogs.com/xing-12/p/6179211.html