spring實現(xiàn)注冊(郵箱驗證+有效時間)

spring實現(xiàn)注冊(郵箱驗證+有效時間) 實現(xiàn)邏輯

用戶注冊時,輸入郵箱地址,獲取驗證碼,我們隨機生成5位數(shù)字驗證碼發(fā)送給用戶的郵箱。同時我們把驗證碼和當前時間存儲到HttpSession對象中(格式 : 驗證碼#時間)。

眉山網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、APP開發(fā)、響應式網(wǎng)站建設等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)公司從2013年開始到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設就選創(chuàng)新互聯(lián)公司。

然后用戶通過表單提交自己的注冊信息和驗證碼。我們先判斷用戶名是否重復(為空和密碼一致就交給前端吧),然后我們從session中取出我們存儲的字符串分隔,獲取驗證碼和生成驗證碼的時間,先比較驗證碼是否正確,如果正確就比較時間是否在一段時間內(nèi),都符合才注冊(進行數(shù)據(jù)庫操作)。

代碼實現(xiàn)尊崇ssh框架結構,action,service,dao逐層調(diào)用。

1:引入jar包

spring自帶的包 spring-context-support, 就可以使用spring自帶的發(fā)送郵件API。

2:創(chuàng)建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; } //發(fā)送驗證碼的方法,to是目標郵箱地址,text是發(fā)送的驗證碼(事先生成) public boolean sendMail (String to,String text) { System.out.println("sendMail...util..."); try{ //設定mail server senderImpl.setHost("smtp.163.com"); // 設置收件人,寄件人 用數(shù)組發(fā)送多個郵件 // String[] array = new String[] {"sun111@163.com","sun222@sohu.com"}; // mailMessage.setTo(array); mailMessage.setTo(to); mailMessage.setFrom( "自己的郵箱" ); mailMessage.setSubject( "主題" ); mailMessage.setText("內(nèi)容" + text); senderImpl.setUsername("自己的郵箱"); senderImpl.setPassword("密碼"); prop.put("mail.smtp.auth","true"); prop.put("mail.smtp.timeout","25000"); senderImpl.setJavaMailProperties(prop); //發(fā)送郵件 senderImpl.send(mailMessage); System.out.println("發(fā)送郵件成功"); return true; }catch (Exception e) { System.out.println("發(fā)送郵件失敗"); return false; } } }

3:創(chuàng)建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); //在獲取現(xiàn)在的時間 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"); //生成了驗證碼并發(fā)送給了用戶 } 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

//發(fā)送郵件獲取驗證碼 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){ //發(fā)送成功,把驗證碼和時間記錄 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; } }

網(wǎng)站標題:spring實現(xiàn)注冊(郵箱驗證+有效時間)
鏈接URL:http://muchs.cn/article12/chsdgc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設計、域名注冊企業(yè)建站、Google、App設計、面包屑導航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)