您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 资本运营 > Java注册邮箱激活验证实现代码
Java注册邮箱激活验证实现代码这篇文章主要介绍了Java注册邮箱激活验证实现代码,有需要的朋友可以参考一下最近从项目分离出来的注册邮箱激活功能,整理一下,方便下次使用RegisterValidateService.java复制代码代码如下:packagecom.app.service.impl;importjava.text.ParseException;importjava.util.Date;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importcom.app.dao.UserDao;importcom.app.tools.MD5Tool;importcom.app.tools.MD5Util;importcom.app.tools.SendEmail;importcom.app.tools.ServiceException;importcom.code.model.UserModel;/****@authorQixuan.Chen*/@ServicepublicclassRegisterValidateService{@AutowiredprivateUserDaouserDao;/***处理注册*/publicvoidprocessregister(Stringemail){UserModeluser=newUserModel();Longas=5480l;user.setId(as);user.setName(xiaoming);user.setPassword(324545);user.setEmail(email);user.setRegisterTime(newDate());user.setStatus(0);///如果处于安全,可以将激活码处理的更复杂点,这里我稍做简单处理//user.setValidateCode(MD5Tool.MD5Encrypt(email));user.setValidateCode(MD5Util.encode2hex(email));userDao.save(user);//保存注册信息///邮件的内容StringBuffersb=newStringBuffer(点击下面链接激活账号,48小时生效,否则重新注册账号,链接只能使用一次,请尽快激活!/br);sb.append(ahref=\=activate&email=);sb.append(email);sb.append(&validateCode=);sb.append(user.getValidateCode());sb.append(\=activate&email=);sb.append(email);sb.append(&validateCode=);sb.append(user.getValidateCode());sb.append(/a);//发送邮件SendEmail.send(email,sb.toString());System.out.println(发送邮件);}/***处理激活*@throwsParseException*////传递激活码和email过来publicvoidprocessActivate(Stringemail,StringvalidateCode)throwsServiceException,ParseException{//数据访问层,通过email获取用户信息UserModeluser=userDao.find(email);//验证用户是否存在if(user!=null){//验证用户激活状态if(user.getStatus()==0){///没激活DatecurrentTime=newDate();//获取当前时间//验证链接是否过期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{thrownewServiceException(激活码不正确);}}else{thrownewServiceException(激活码已过期!);}}else{thrownewServiceException(邮箱已激活,请登录!);}}else{thrownewServiceException(该邮箱未注册(邮箱地址不存在)!);}}}RegisterController.java复制代码代码如下:packagecom.app.web.controller;importjava.text.ParseException;importjavax.annotation.Resource;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.servlet.ModelAndView;importcom.app.service.impl.RegisterValidateService;importcom.app.tools.ServiceException;@ControllerpublicclassRegisterController{@ResourceprivateRegisterValidateServiceservice;@RequestMapping(value=/user/register,method={RequestMethod.GET,RequestMethod.POST})publicModelAndViewload(HttpServletRequestrequest,HttpServletResponseresponse)throwsParseException{Stringaction=request.getParameter(action);System.out.println(-----r----+action);ModelAndViewmav=newModelAndView();if(register.equals(action)){//注册Stringemail=request.getParameter(email);service.processregister(email);//发邮箱激活mav.addObject(text,注册成功);mav.setViewName(register/register_success);}elseif(activate.equals(action)){//激活Stringemail=request.getParameter(email);//获取emailStringvalidateCode=request.getParameter(validateCode);//激活码try{service.processActivate(email,validateCode);//调用激活方法mav.setViewName(register/activate_success);}catch(ServiceExceptione){request.setAttribute(message,e.getMessage());mav.setViewName(register/activate_failure);}}returnmav;}}UserDao.java(这里个人没有做入库操作,只是利用集合,做过效果出来0_0)复制代码代码如下:packagecom.app.dao;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Date;importjava.util.HashMap;importorg.springframework.stereotype.Repository;importcom.code.model.UserModel;/****@authorQixuan.Chen*/@RepositorypublicclassUserDao{publicHashMapString,Stringmap=newHashMapString,String();/***@保存注册信息*privateLongid;privateStringname;privateStringpassword;privateStringemail;//注册账号privateintstatus;//激活状态privateStringvalidateCode;//激活码privateDateregisterTime;//注册时间*/publicvoidsave(UserModeluser){System.out.println(cicicici);map.put(id,String.valueOf(user.getId()));map.put(email,user.getEmail());map.put(validateCode,user.getValidateCode());SimpleDateFormatsdf=newSimpleDateFormat(yyyyMMddhhmmss);Stringtime=sdf.format(user.getRegisterTime());map.put(registerTime,time);intstatus=user.getStatus();map.put(status,String.valueOf(status));map.put(name,user.getName());Stringt2=sdf.format(user.getLastActivateTime());map.put(activeLastTime,String.valueOf(t2));System.out.println(=======s=========+status);}/***@更新user*/publicvoidupdate(UserModeluser){map.put(email,user.getEmail());map.put(validateCode,user.getValidateCode());Datetime=user.getRegisterTime();map.put(registerTime,String.valueOf(time
本文标题:Java注册邮箱激活验证实现代码
链接地址:https://www.777doc.com/doc-2881146 .html