spring实现注册(邮箱验证+有效时间)

  • 来源:新网
  • 更新日期:2018-03-14

摘要:spring实现注册(邮箱验证+有效时间)实现逻辑 用户注册时,输入邮箱地址,获取验证码,我们随机生成5位数字验证码发送给用户的邮箱。同时我们把验证码和当前时间存储到HttpSession对象中(格式 : 验证码#时间)。 然后用户通过表单提交自己的注册信息和验证码。我们先判断用户名是否重复(为空和密码一致就交给前端吧),然后我们从session中取出我们存储的字符串分隔,获取验证码和生成验证码的

spring实现注册(邮箱验证+有效时间) 实现逻辑

用户注册时,输入邮箱地址,获取验证码,我们随机生成5位数字验证码发送给用户的邮箱。同时我们把验证码和当前时间存储到HttpSession对象中(格式 : 验证码#时间)。

t0132c42d6f6c70a5ba.png

然后用户通过表单提交自己的注册信息和验证码。我们先判断用户名是否重复(为空和密码一致就交给前端吧),然后我们从session中取出我们存储的字符串分隔,获取验证码和生成验证码的时间,先比较验证码是否正确,如果正确就比较时间是否在一段时间内,都符合才注册(进行数据库操作)。

代码实现尊崇ssh框架结构,action,service,dao逐层调用。

1:引入jar包

spring自带的包 spring-context-support, 就可以使用spring自带的发送邮件API。

2:创建MailUtil对象

import java.util.Properties; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSenderImpl; public class MailUtil { //使用对象注入的方式 记得配置文件 private JavaMailSenderImpl senderImpl; public void setSenderImpl(JavaMailSenderImpl senderImpl) { this.senderImpl = senderImpl; } private SimpleMailMessage mailMessage; public void setMailMessage(SimpleMailMessage mailMessage) { this.mailMessage = mailMessage; } private Properties prop; public void setProp(Properties prop) { this.prop = prop; } //发送验证码的方法,to是目标邮箱地址,text是发送的验证码(事先生成) public boolean sendMail (String to,String text) { System.out.println("sendMail...util..."); try{ //设定mail server senderImpl.setHost("smtp.163.com"); // 设置收件人,寄件人 用数组发送多个邮件 // String[] array = new String[] {"sun111@163.com","sun222@sohu.com"}; // mailMessage.setTo(array); mailMessage.setTo(to); mailMessage.setFrom( "自己的邮箱" ); mailMessage.setSubject( "主题" ); mailMessage.setText("内容" + text); senderImpl.setUsername("自己的邮箱"); senderImpl.setPassword("密码"); prop.put("mail.smtp.auth","true"); prop.put("mail.smtp.timeout","25000"); senderImpl.setJavaMailProperties(prop); //发送邮件 senderImpl.send(mailMessage); System.out.println("发送邮件成功"); return true; }catch (Exception e) { System.out.println("发送邮件失败"); return false; } } }

3:创建TimeUtil类

因为我们是保证有效时间的,所以就要时间工具

import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import org.junit.Test; public class TimeUtil { //获取时间 返回毫秒级时间 public String getTime() { System.out.println("getTime...util..."); //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar calendar = Calendar.getInstance(); Long date = calendar.getTime().getTime(); //获取毫秒时间 //String dateStringPaString = sdf.format(date); //System.out.println(dateStringPaString); return date.toString(); } public boolean cmpTime(String time) { System.out.println("cmpTime...util..."); long tempTime = Long.parseLong(time); System.out.println("tempTime"+tempTime); //在获取现在的时间 Calendar calendar = Calendar.getInstance(); Long date = calendar.getTime().getTime(); //获取毫秒时间 System.out.println("date"+date); if(date - tempTime > 600000 ) { //10分钟 return false; } else { return true; } } }

4:Action

//获取邮箱验证码 public String getVCode() throws IOException { System.out.println("getVCode...action..."); //获得request和response对象 HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("application/json;charset=utf-8"); response.setHeader("Access-Control-Allow-Origin", "*"); PrintWriter out = response.getWriter(); JSONObject json = new JSONObject(); try{ String email = request.getParameter("email"); boolean flag = userService.getVCode(email); if(flag == true) { json.put("msg","1"); //生成了验证码并发送给了用户 } else { json.put("msg","0"); //未获取到 } }catch (Exception e) { json.put("msg","0"); }finally { out.write(json.toString()); out.flush(); out.close(); } return null; } //用户注册 public String register() throws IOException { System.out.println("register...action..."); //获得request和response对象 HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("application/json;charset=utf-8"); response.setHeader("Access-Control-Allow-Origin", "*"); PrintWriter out = response.getWriter(); JSONObject json = new JSONObject(); try{ String username = request.getParameter("username"); String password = request.getParameter("password"); String nickname = request.getParameter("nickname"); String email = request.getParameter("email"); String vcode = request.getParameter("vcode"); //先查找该用户名是否被注册 boolean flag = userService.searchUser(username); if(flag == true) { json.put("msg","3"); //用户名重复 } else { //看验证码是否正确以及是否失效 flag = userService.cmpVCode(vcode); if(flag == true){ User user = new User(); user.setUsername(username); user.setPassword(password); user.setNickname(nickname); user.setEmail(email); boolean flag2 =userService.register(user); if(flag2 == true) { json.put("msg", "1"); //注册成功 } else { json.put("msg","0"); //注册失败 } } else { System.out.println("验证码匹配失败"); json.put("msg", "0"); //验证码匹配失败 } } } catch(Exception e) { System.out.println("注册异常"); json.put("msg", "0"); //注册 异常 } finally { out.write(json.toString()); out.flush(); out.close(); } return null; }

5:service

//发送邮件获取验证码 public boolean getVCode(String email) { System.out.println("getVCode...service..."); //随机生成5验证码 Integer x =(int)((Math.random()*9+1)*10000); String text = x.toString(); boolean flag = mailUtil.sendMail(email, text); if(flag == true){ //发送成功,把验证码和时间记录 String nowTime = timeUtil.getTime(); //存入session 验证码#时间 HttpSession session = ServletActionContext.getRequest().getSession(); session.setAttribute("vcodeTime",text+"#"+nowTime); System.out.println(session.getAttribute("vcodeTime")); return true; } else { return false; } } //比较验证码是否正确以及是否失效 public boolean cmpVCode(String vcode) { System.out.println("cmpVCode...service..."); try{ HttpSession session = ServletActionContext.getRequest().getSession(); String vcodeTime = (String) session.getAttribute("vcodeTime"); String vcodeTimeArray[] = vcodeTime.split("#"); //先比较验证码是否正确 if(vcodeTimeArray[0].equals(vcode)) { boolean flag = timeUtil.cmpTime(vcodeTimeArray[1]); if(flag == true){ return true; } } return false; } catch (Exception e) { System.out.println(e.toString()); return false; } } //注册 public boolean register(User user) { System.out.println("register...service..."); int flag = userDao.addUser(user); if(flag != 0) { HttpSession session = ServletActionContext.getRequest().getSession(); session.setAttribute("uid", flag); //用户id放入 session return true; } else { return false; } }

6:dao

//添加用户 public int addUser(User user) { System.out.println("addUser...dao..."); try{ int flag = (int)hibernateTemplate.save(user); System.out.println("save后返回的flag值" + flag); return flag; }catch (Exception e) { System.out.println(e.toString()); return 0; } }