一、目的
在含有注册功能或者验证功能的网站中,为避免一个用户使用多个账号,通常要使用 用户——手机号,或者用户——邮箱地址这两种对应关系来避免过多的用户垃圾、僵尸用户。本篇博客讲述的就是如何在基于SringMVC的项目中实现邮箱验证功能。
二、用户使用流程
三、在项目Web层中的体现
四、实现准备
jar包JavaMail 下载链接:JavaMail API
五、实现
1.regist.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html lang="en"> <head> <meta charset="utf-8"> <script type="text/javascript" src="js/jquery-3.1.1.min.js"></script> <script type="text/javascript"> $(function(){ $("#username").blur(function(){ $.ajax({ url:"sendUserName.action", type:"GET", data:"username="+$("#username").val(), timeout:5000, success:function(data){ if(data == "error"){ //$("#usernameMsg").css("display","inline"); $("#usernameMsg").show(1000); }else{ $("#usernameMsg").slideUp(1000); } }, error:function(){ } }) }); $("#sendidcode").click(function(){ if($("#mail").val() == ""){ $("#sendidcode").attr("disabled","true"); $("#sendidcode").val("邮箱地址未填"); setTimeout(function(){ $("#sendidcode").removeAttr("disabled"); $("#sendidcode").val("发送验证码"); },900); return; } var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/; if(!myreg.test($("#mail").val())){ $("#sendidcode").attr("disabled","true"); $("#sendidcode").val("邮箱格式错误"); setTimeout(function(){ $("#sendidcode").removeAttr("disabled"); $("#sendidcode").val("发送验证码"); },900); return; } $("#sendidcode").attr("value","正在发送"); $("#sendidcode").attr("disabled","true"); $.ajax({ url:"sendIdCode.action", type:"GET", //发送数据的第一种格式,字符串... data:"mail="+$("#mail").val(), timeout:5000, success:function(data){ $("#sendidcode").attr("value","验证码已发送"); }, error:function(){ } }); }); }); </script> <style type="text/css"> .content{ border-radius: 5px; background: white; width: 550px; height: 450px; margin: 10px auto; border: 1px solid lightgray; } input[type="text"],input[type="password"],input[type="email"]{ padding-left:10px; width: 170px; height: 25px; border: solid 1px lightgrey; border-radius: 3px; } .left{ display: inline-block; text-align: right; width:170px; } .in{ line-height: 50px; width: 550px; margin: 0 20px; font-size: 10pt; } a{ font-size: 9pt; text-decoration: none; color: grey; } a:hover{ color: blue; } hr{ margin: 10px 20px; border: none; border-bottom: 1px dotted lightgrey; } .bottom{ float: right; padding-right: 20px; line-height: 60px; } input[type="submit"]{ background: rgba(22, 115, 11, 0.38); border: 0 none; width: 90px; height: 30px; border-radius: 3px; color: white; } input[type="submit"]:hover{ background: rgb(77, 140, 70); } input[type="button"]:hover{ background: rgb(77, 140, 70); } input[type="button"]{ background: rgba(22, 115, 11, 0.38); border: 0 none; width: 90px; height: 23px; border-radius: 3px; color: white; } input[type="reset"]{ background: rgb(179, 176, 176); border: 0 none; width: 90px; height: 30px; border-radius: 3px; color: white; } input[type="reset"]:hover{ background: rgb(200, 200, 200); } .tip{ text-align: center; font-size: 10pt; color: grey; background: white; width: 530px; height: 90px; margin: 0px auto; padding-left: 10px; background: lightgrey; } body{ background: lightgrey; } .warning{ color:red; } #usernameMsg{ display:none; width:200px; height:30px; } </style> </head> <body> <div class="tip"> <img src="img/logo.png" width="90px" height="90px"> </div> <div class="content"> <form action="regist.action" method="post"> <div class="in"> <br> <span class="left">用户名:</span> <input type="text" required="required" placeholder="请输入用户名" name="username" id="username"/> <span id="usernameMsg" class="warning">用户名已存在</span> </div> <div class="in"> <span class="left">密码:</span> <input type="password" required="required" placeholder="请输入密码" name="password"/> </div> <div class="in"> <span class="left">真实姓名:</span> <input type="text" required="required" placeholder="请输入真实姓名" name="realname"/> </div> <div class="in"> <span class="left">邮箱:</span> <input type="email" required="required" placeholder="请输入有效邮箱地址" name="mail" id="mail"/> <span><input type="button" value="发送验证码" id="sendidcode"></span> </div> <div class="in"> <span class="left">验证码:</span> <input type="text" required="required" placeholder="请点击按钮发送验证码" name="idcode"/> <span class="warning">${idcodeMsg }</span> </div> <hr> <div class="bottom"> <input type="submit" value="注册"> <input type="reset" value="重置"> </div> </form> </div> <div class="tip"> 提示:没有提示。 </div> </body> </html>
2. MailUtil.javapackage com.sys.utils; import java.util.Properties; import javax.mail.Address; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class MailUtil { static Properties properties; static Message msg; static Transport transport; //初始化Mail信息 public MailUtil(){ properties = new Properties(); properties.setProperty("mail.debug", "true");//调试模式发送 properties.setProperty("mail.smtp.auth", "true");//身份验证设置 properties.setProperty("mail.host", "smtp.163.com");//发件人邮箱主机名 properties.setProperty("mail.transport.protocol", "smtp");//发件协议 Session session = Session.getInstance(properties); msg = new MimeMessage(session); try { msg.setSubject("xxx网站验证邮件"); msg.setFrom(new InternetAddress("xxxxxx"));//设置发件人 transport = session.getTransport(); transport.connect("xxxxx", "xxxxx");//设置发件人在该邮箱主机上的用户名密码 } catch (MessagingException e) { e.printStackTrace(); } } /** * 得到邮箱地址邮箱内容发送。 * @param 邮箱地址 * @param 邮箱内容 * @throws AddressException * @throws MessagingException */ public void sendMail(String address,String text) throws AddressException, MessagingException{ msg.setText(text); transport.sendMessage(msg, new Address[] {new InternetAddress(address)}); transport.close(); } }
3.Controller.java
... @RequestMapping("sendIdCode.action") public void AjaxSendIdCode(String mail, HttpServletResponse response,HttpServletRequest request) throws IOException, AddressException, MessagingException{ PrintWriter out = response.getWriter(); response.setCharacterEncoding("utf-8"); int idcode = (int) (Math.random()*100000); String text = Integer.toString(idcode); request.getSession().setAttribute("idcode", text); MailUtil mailUtil = new MailUtil(); mailUtil.sendMail(mail, text); out.write("success") } ...
...@RequestMapping("sendIdCode.action")public void AjaxSendIdCode(String mail, HttpServletResponse response,HttpServletRequest request) throws IOException, AddressException, MessagingException{PrintWriter out = response.getWriter();response.setCharacterEncoding("utf-8");int idcode = (int) (Math.random()*100000);String text = Integer.toString(idcode);request.getSession().setAttribute("idcode", text);MailUtil mailUtil = new MailUtil();mailUtil.sendMail(mail, text);}...
... @RequestMapping("regist.action") public String regist(User user, String idcode,HttpServletRequest request){ if(user.getUsername()!= null){ if(!request.getSession().getAttribute("idcode").equals(idcode)){ request.getSession().setAttribute("idcodeMsg", "验证码错误!"); return "regist"; } if(userService.findUserByUserName(user.getUsername())!= null ){ request.getSession().setAttribute("usernameMsg", "用户名已存在!"); return "regist"; } System.out.println(idcode); userService.insertUser(user); return "success"; }else{ return "regist"; } } ...
六、效果