java web激活邮箱并找回密码
几乎每个网站或论坛之类的用户注册后都需要通过发送邮件到邮箱激活用户,如何激活邮箱呐?
设计激活步骤:
1、发送激活操作链接地址至用户邮箱。
2、用户至邮箱查收邮件。
3、用户点击链接,跳转至成功页面(修改激活状态),激活成功。
实现:
/** * 发送邮件 * * @param request * @return */ @RequestMapping(value = "/sendEmail.jhtml") @ResponseBody public String sendEmail(HttpServletRequest request) { DynamicParams params = new DynamicParams(request); StringBuilder builder = new StringBuilder(); StringBuffer url = new StringBuffer(); String type = params.getString("type"); Long uid = params.getLong("id"); User user = userManager.find(uid); String subject = ""; // 判断是否已激活 if ("1".equals(String.valueOf(user.getEmailStatus()))) { return errMsg(); } String contextPath = request.getContextPath(); String rUrl = String.valueOf(request.getRequestURL()); url.append(rUrl.substring(0, rUrl.indexOf(contextPath))); url.append(contextPath + "/account"); // 邮箱激活 // url.append("/activateEmail.jhtml?email=" + user.getEmail() + // "&id=" + uid + "&mode=activate"); url.append("/activateEmail.jhtml?id=" + uid + "&mode=activate"); // 正文 builder.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" </head><body>"); builder.append("请点击下方链接激活您的邮箱,完成激活邮箱的操作!"); builder.append("<br/><br/>"); builder.append("<a href=\"" + url + "\">"); builder.append(url); builder.append("</a>"); builder.append("</body></html>"); subject = "邮箱地址激活 - xxxx"; MailSender.mailSimple(user.getEmail(), subject, builder.toString(), false, null); return successMsg(); } /** * 激活邮箱 * * @param request * @return */ @RequestMapping(value = "/activateEmail.jhtml") public String activateEmail(HttpServletRequest request, Model model) { DynamicParams params = new DynamicParams(request); // 激活之前查询 User user = userManager.find(params.getLong("id")); if ("1".equals(String.valueOf(user.getEmailStatus()))) { // 已激活 model.addAttribute("flag", false); } else { // 未激活 userManager.activateEmail(params); // 激活之后查询 user = userManager.find(params.getLong("id")); request.getSession().setAttribute("sessionUser", user); model.addAttribute("mode", params.getString("mode")); model.addAttribute("flag", true); } return "site/modules/account/activateSuccess"; }
MailSender.mailSimple(user.getEmail(), subject, builder.toString(),false, null);即为发送邮件工具类,如有不明白可参考我之前的文章(spring mail发送邮件)
忘记密码(重置密码)
背景:同样是各大网站都会拥有的一个功能,通过发送验证码到用户注册的邮箱重置密码
设计:1)输入用户名/手机号码/邮箱,验证码。
2)用户选择手机验证或邮箱验证,发送验证码到手机或邮箱。如果用户邮箱未激活,则不允许邮箱验证。
3)用户输入验证码成功,进入密码重新设置页面。
4)重置密码
流程:
实现:
/** * 发送邮件 * * @param request * @return */ @RequestMapping(value = "/sendEmail.jhtml") @ResponseBody public String sendEmail(HttpServletRequest request) { DynamicParams params = new DynamicParams(request); StringBuilder builder = new StringBuilder(); StringBuffer url = new StringBuffer(); String type = params.getString("type"); Long uid = params.getLong("id"); User user = userManager.find(uid); String subject = ""; // type = forget 密码重置 String verifyCode = VerifyCodeUtils.generateVerifyCode(4); request.getSession().setAttribute("resetCertCode", verifyCode); url.append("<font color='red'>" + verifyCode + "</font>"); // 正文 builder.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /></head><body>"); builder.append("要使用新的密码, 请将已下字符输入验证框中,完成重置密码的操作!"); builder.append("<br/><br/>"); builder.append("<div>" + url + "</div>"); builder.append("</body></html>"); subject = "密码重置 - xxxx"; MailSender.mailSimple(user.getEmail(), subject, builder.toString(), false, null); return successMsg(); }
至此,激活邮箱和忘记密码功能已初步完成!希望对大家的学习有所帮助,大家可以动手实现激活邮箱并找回密码。
赞 (0)