您的位置:首页 > 编程语言 > Java开发

java_mvc实现发送邮箱验证码并验证的功能

2020-02-04 16:33 465 查看

功能说明

目标:用户注册时,输入邮箱地址Email Address,点击注册发送一封带有验证码code的邮件,用户在前台输入正确的验证码后注册成功,否则失败。

Java 代码 发送验证码

@RequestMapping({"search-user"})
public String showSearchPage(
@RequestParam(value = "username",required = false) String username,
Model model){
if(username != null){
model.addAttribute("username", username);
//Send an emailcode to the user.
String emailcode = CodeUtil.generateUniqueCode();
model.addAttribute("emailcode", emailcode);
new Thread(new MailUtil(username,emailcode)).start();;
}
}
return "login";
}

MailUtil类

import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.util.MailSSLSocketFactory;

public class MailUtil implements Runnable {
private String email;// 收件人邮箱
private String code;// 激活码

public MailUtil(String email, String code) {
this.email = email;
this.code = code;
}

public void run() {
// 创建连接对象javax.mail.Session
// 创建邮件对象 javax.mail.Message
// 发送一封激活邮件
String from = "xxx@163.com";// 发件人电子邮箱
String host = "smtp.163.com"; // 指定发送邮件的主机smtp.qq.com(QQ)|smtp.163.com(网易)

Properties properties = System.getProperties();// 获取系统属性

properties.setProperty("mail.smtp.host", host);// 设置邮件服务器
properties.setProperty("mail.smtp.auth", "true");// 打开认证

try {
//			//QQ邮箱需要下面这段代码,163邮箱不需要
//			MailSSLSocketFactory sf = new MailSSLSocketFactory();
//			sf.setTrustAllHosts(true);
//			properties.put("mail.smtp.ssl.enable", "true");
//			properties.put("mail.smtp.ssl.socketFactory", sf);

// 获取默认session对象
Session session = Session.getDefaultInstance(properties, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xxx@163.com", "******"); // 发件人邮箱账号、授权码,从邮箱设置里面获取
}
});

// 创建邮件对象
Message message = new MimeMessage(session);
// 设置发件人
message.setFrom(new InternetAddress(from,"激活邮件"));
// 设置接收人
message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
// 设置邮件主题
message.setSubject("验证码");
// 设置邮件内容
String content = "<html><head></head><body><h1>您好,您的验证码为:" + code
+ "</href></h3></body></html>";
message.setContent(content, "text/html;charset=UTF-8");
// 发送邮件
Transport.send(message);

System.out.println("邮件成功发送!");
//System.out.println("code:"+code);
} catch (Exception e) {
e.printStackTrace();
}
}
}

CodeUtil类

public class CodeUtil {
//生成唯一的激活码
public static String generateUniqueCode(){
String flag = "";
for (int i = 0; i <= 200; i++)
{
int intFlag = (int)(Math.random() * 1000000);

flag = String.valueOf(intFlag);
if (flag.length() == 6 && flag.substring(0, 1).equals("9"))
{
return(flag);
}
else
{
intFlag = intFlag + 100000;
flag = String.valueOf(intFlag);
return(flag);
}
}
return flag;
}
}

java代码 验证码正确后注册成功

先将发送的验证码code记录在jsp页面中

<input type="hidden" id = "emailco" name="emailco" value = ${emailcode } />

这样做的目的,用户输入验证码后,同时将用户输入的验证码+后台发给用户的验证码返回,在下面这段代码中进行验证。

@RequestMapping({"register"})
public String showRegisterPage(
@RequestParam(value = "username", required = true) String username,
@RequestParam(value = "emailcode", required = true) String emailcode,
@RequestParam(value = "emailco", required = true) String emailco,
@RequestParam(value = "password", required = false) String password,
Model model){

if(emailcode.equalsIgnoreCase(emailco)) {
String returnStr = userService.register(username, password);
model.addAttribute("username", username);
}
return "home";
}

jsp

    <form style = "display:hidden" action = "/demo/search-user" method = "POST" id = "post_search">
<input type = "hidden" id = "post_search_username" name = "username" value = ""/>
</form>
<form style = "display:hidden" action = "/demo/register" method = "POST" id = "post_register">
<input type = "hidden" id = "post_register_username" name = "username" value = ""/>
<input type = "hidden" id = "post_register_emailcode" name = "emailcode" value = ""/>
<input type = "hidden" id = "post_register_emailco" name = "emailco" value = ""/>
<input type = "hidden" id = "post_register_password" name = "password" value = ""/>
</form>

<p> Username:
<input type="text" name="username" id="j_username" maxlength="30">
<button id="j_search_button">Submit</button>
</p>
<p> Email Code: <input type="hidden" id = "emailco" name="emailco" value = ${emailcode } /><input type="text" name="emailcode" id="j_emailcode" maxlength="30">
</p>
<p> Password:
<input type="password" name="password" id="j_password" maxlength="30">
</p>
<button id="j_reg_button">Register</button>

js

window.onload = function(){
var button_reg = document.getElementById("j_reg_button");
button_reg.addEventListener("click", register_credential);
var button_search = document.getElementById("j_search_button");
button_search.addEventListener("click",search_credential);
}

function search_credential(){
var username = document.getElementById("j_username").value;
$("#post_search_username").val(username);
$("#post_search").submit();
}

function register_credential(){
var username = document.getElementById("j_username").value;
var password = document.getElementById("j_password").value;
var emailcode = document.getElementById("j_emailcode").value;
var emailco = document.getElementById("emailco").value;
$("#post_register_emailcode").val(emailcode);
$("#post_register_emailco").val(emailco);
$("#post_register_username").val(username);
$("#post_register_password").val(password);
$("#post_register").submit()

附上邮箱格式的正则表达式,可以自行修改:

var reg =/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;;
  • 点赞 1
  • 收藏
  • 分享
  • 文章举报
赵小小酱 发布了24 篇原创文章 · 获赞 8 · 访问量 2859 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐