javaweb项目使用邮箱注册验证实现,方式一:发送**链接
程序员文章站
2024-03-20 11:21:10
...
看了主流网站注册都会通过去邮箱点击链接**实现注册登录等功能,闲暇根据自己理解想写一个简单实现,纯属瞎玩,不足之处,还请广大读者严厉批评指正。
逻辑:1,输入信息点击注册时,会往用户邮箱发送验证链接,区别与大众化的验证码
2,接受邮件,点击**链接,再次调用帐号**接口,并修改**状态
3,登录时,判断**状态
步骤:1,简单的ajax调用接口测试(替代注册表单)
//用户注册接口
function registTest() {
$.post('???/regist2', {
'email': 'aaa@qq.com',
'password1': '123456',
'password2': '123456'
}, function(rescnt) {
console.log(rescnt);
})
}
2,接口实现
/**系统用户注册接口-方式2
* @return
*/
@RequestMapping(value="/regist2",method=RequestMethod.POST)
@ResponseBody
public Object regist2(){
logBefore(logger, "XXX官网用户注册接口");
Map<String,Object> map = new HashMap<String,Object>();
PageData pd = new PageData();
pd = this.getPageData();
String code = "100";
String msg_cn = "系统错误";
try{
if(Chain8Util.checkParam("regist2", pd)){//第一步:判断请求参数是否完整(调封装的工具类方法-大致逻辑:定常量数组,对应属性长度,用标识来定位)
PageData pd2 = register8Service.findByEmail(pd);
if(pd2!=null) {//第二步:判断邮箱是否已注册
if(pd2.getString("status").equals("1")) {
code = "101";
msg_cn = "该邮箱已注册并**";
}else if(pd2.getString("status").equals("0")){//注册但未**,重新发送
String token = this.get32UUID();//获取32位uuid作为**token
if(Register8EmailUtil.sendJihuo(pd.getString("email"),token )){//第五步:发送**邮件(调用邮件工具类)
//###### 邮件成功发送后,将对应信息存入数据库 ######
pd.put("password", pd.getString("password1"));
pd.put("status", "0");
pd.put("token", token);
register8Service.edit(pd);//修改数据库数据,重新赋值
code = "300";
msg_cn = "注册成功,请前往邮箱**账号,方可登录";
}else {
code = "100";
msg_cn = "系统错误";
}
}
}else {
if(Tools.checkEmail(pd.getString("email"))) {//第三步:判断邮箱格式(调封装的工具类方法)
if(pd.getString("password1").equals(pd.getString("password2"))) {//第四步:判断两次密码是否一致
String token = this.get32UUID();//获取32位uuid作为**token
if(Register8EmailUtil.sendJihuo(pd.getString("email"),token )){//第五步:发送**邮件(调用邮件工具类)
//###### 邮件成功发送后,将对应信息存入数据库 ######
pd.put("password", pd.getString("password1"));
pd.put("status", "0");
pd.put("token", token);
register8Service.save(pd);//保存
code = "300";
msg_cn = "注册成功,请前往邮箱**账号,方可登录";
}else {
code = "100";
msg_cn = "系统错误";
}
}else {
code = "103";
msg_cn = "两次密码不一致";
}
}else {
code = "102";
msg_cn = "邮箱格式不合法";
}
}
}else {
code = "200";
msg_cn = "请填写完整信息";
}
}catch (Exception e){
logger.error(e.toString(), e);
}finally{
map.put("code", code);
map.put("msg_cn", msg_cn);
logAfter(logger);
}
return AppUtil.returnObject(new PageData(), map);
}
/**官网用户帐户**接口-
* @return
*/
@RequestMapping(value="/jihuo")
@ResponseBody
public Object jihuo(){
logBefore(logger, "XXX官网用户帐号**接口");
Map<String,Object> map = new HashMap<String,Object>();
PageData pd = new PageData();
pd = this.getPageData();
String code = "100";
String msg_cn = "系统错误";
try{
if(Chain8Util.checkParam("jihuo", pd)){//第一步:判断请求参数是否完整(调封装的工具类方法-大致逻辑:定常量数组,对应属性长度,用标识来定位)
PageData pd2 = register8Service.findByEmail(pd);
String tokenMysql = pd2.getString("token");
String token = pd.getString("token");
if(tokenMysql.equals(token)) {
pd.put("status", "1");
register8Service.edit(pd);//修改数据库帐户**状态,为登录判断用
code = "102";
msg_cn = "**成功。能登录了";
}else {
code = "101";
msg_cn = "**链接失效";
}
}else {
code = "200";
msg_cn = "请填写完整信息";
}
}catch (Exception e){
logger.error(e.toString(), e);
}finally{
map.put("code", code);
map.put("msg_cn", msg_cn);
logAfter(logger);
}
return AppUtil.returnObject(new PageData(), map);
}
3,邮件发送服务(这里使用的是AWS代理服务来发送的,可以避免好多普通邮箱的限制,方法可参考我的博客,后期会写)
//注册-方式2-邮件服务
public static boolean sendJihuo(String email,String token)throws Exception{
AWSMailSenderInfo awsMailSenderInfo = new AWSMailSenderInfo();//这个类主要是设置邮件
awsMailSenderInfo.setAWS_mailServerHost("smtp");
awsMailSenderInfo.setAWS_mailServerPort("587");
awsMailSenderInfo.setAWS_fromAddress("aws服务代理的邮箱");
awsMailSenderInfo.setToAddress(email);
awsMailSenderInfo.setAWS_validate(true);
awsMailSenderInfo.setTLS(true);
awsMailSenderInfo.setAWS_account("aws服务地址账号");//
awsMailSenderInfo.setAWS_password("服务密码");//
awsMailSenderInfo.setAWS_key("服务**");//
awsMailSenderInfo.setAWS_NAME("XXX官网");
awsMailSenderInfo.setSubject("Chain8-Mailbox verification code service(XXX官网-账号**服务提醒)");
awsMailSenderInfo.setContent("<p>您好 O(∩_∩)O~~<br><br>欢迎加入XXX!<br><br>帐户需要**才能使用,赶紧**成为XXX正式的一员吧:)<br><br>请在24小时内点击下面的链接立即**帐户:"
+"<br><a href='http://???/jihuo?token="+token+"&email="+email+"'>"
+ "http://???/jihuo?token="+token+"&email="+email+"</a></p>");
AWSSimpleMailSender sms = new AWSSimpleMailSender();
return sms.sendHtmlMail(awsMailSenderInfo);
//return sms.sendTextMail(awsMailSenderInfo);
}
4:邮件服务配置类
public class AWSSimpleMailSender extends BaseController {
/**
* 以文本格式发送邮件
* @param awsMailSenderInfo 待发送的邮件的信息
*/
public boolean sendTextMail(AWSMailSenderInfo awsMailSenderInfo) throws Exception{
Properties props = awsMailSenderInfo.getProperties();
// 根据邮件会话属性构造一个发送邮件的session
Session session = Session.getDefaultInstance(props);
logBefore(logger, "构造一个发送邮件的session");
// 根据session创建一个邮件消息
Message msg = new MimeMessage(session);
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(awsMailSenderInfo.getToAddress()));
msg.setFrom(new InternetAddress(awsMailSenderInfo.getAWS_fromAddress(),awsMailSenderInfo.getAWS_NAME()));
msg.setSubject(awsMailSenderInfo.getSubject());
// Create a transport.
Transport transport = session.getTransport();
// Send the message.
transport.connect(awsMailSenderInfo.getAWS_account(),awsMailSenderInfo.getAWS_password(), awsMailSenderInfo.getAWS_key());
// Send the email.
System.out.println("Sending Email...");
msg.setContent(awsMailSenderInfo.getContent(), "text/html;charset=utf-8");
transport.sendMessage(msg, msg.getAllRecipients());
System.out.println("Email sent!");
transport.close();
logBefore(logger, "发送成功!");
//logBefore(logger, Jurisdiction.getUsername()+"发送邮件了");
return true;
}
/**
* 以HTML格式发送邮件
* @param mailInfo 待发送的邮件信息
*/
public boolean sendHtmlMail(AWSMailSenderInfo awsMailSenderInfo) throws Exception{
Properties props = awsMailSenderInfo.getProperties();
// 根据邮件会话属性构造一个发送邮件的session
Session session = Session.getDefaultInstance(props);
logBefore(logger, "构造一个发送邮件的session");
// 根据session创建一个邮件消息
Message msg = new MimeMessage(session);
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(awsMailSenderInfo.getToAddress()));
msg.setFrom(new InternetAddress(awsMailSenderInfo.getAWS_fromAddress(),awsMailSenderInfo.getAWS_NAME()));
msg.setSubject(awsMailSenderInfo.getSubject());
// Create a transport.
Transport transport = session.getTransport();
// Send the message.
transport.connect(awsMailSenderInfo.getAWS_account(),awsMailSenderInfo.getAWS_password(), awsMailSenderInfo.getAWS_key());
// Send the email.
System.out.println("Sending Email...");
msg.setContent(awsMailSenderInfo.getContent(), "text/html;charset=utf-8");
transport.sendMessage(msg, msg.getAllRecipients());
System.out.println("Email sent!");
transport.close();
logBefore(logger, "发送成功!");
return true;
}
5:实体类
public class AWSMailSenderInfo {
// 发送邮件的服务器的IP和端口
private String AWS_mailServerHost;
private String AWS_mailServerPort = "587";
// 邮件发送者的地址
private String AWS_fromAddress;
// 邮件接收者的地址
private String toAddress;
// 是否需要身份验证
private boolean AWS_validate = true;
//是否TLS加密
private boolean TLS = true;
//AWS代理地址账号
private String AWS_account;
//AWS代理账号密码
private String AWS_password;
//AWS代理**
private String AWS_key;
//代理发件人名称
private String AWS_NAME;
// 邮件主题
private String subject;
// 邮件的文本内容
private String content;
// 邮件附件的文件名
//private String[] attachFileNames;
6:实现效果
邮件效果:
**效果:
上一篇: Appium多线程并发,其中一个设备无法定位元素问题解决
下一篇: 【实战】Java实现邮箱验证