流程图片
1、邮箱验证登录
1.1、先去邮箱设置验证
1.2 编程JAVA
1、在pom中引入发送邮件依赖
<!-- 发邮件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2、在yml文件配置发件人用户名密码
# 发送邮件配置 mail: # 发件人地址 user: 461618768@qq.com # 发件人密码 password: yioaiekdcphnbije # 项目路径 project: path: http://localhost
3、引入邮件发送工具类
/** * 发邮件工具类 */ @Component public final class MailUtils{ @Value("${mail.user}") private String USER; // 发件人邮箱地址 @Value("${mail.password}") private String PASSWORD; // 如果是qq邮箱可以使户端授权码 /** * 发送邮件 * @param to 收件人邮箱 * @param text 邮件正文 * @param title 标题 */ public boolean sendMail(String to, String text, String title){ try { final Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.host", "smtp.qq.com"); // 发件人的账号 props.put("mail.user", USER); //发件人的密码 props.put("mail.password", PASSWORD); // 构建授权信息,用于进行SMTP进行身份验证 Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { // 用户名、密码 String userName = props.getProperty("mail.user"); String password = props.getProperty("mail.password"); return new PasswordAuthentication(userName, password); } }; // 使用环境属性和授权信息,创建邮件会话 Session mailSession = Session.getInstance(props, authenticator); // 创建邮件消息 MimeMessage message = new MimeMessage(mailSession); // 设置发件人 String username = props.getProperty("mail.user"); InternetAddress form = new InternetAddress(username); message.setFrom(form); // 设置收件人 InternetAddress toAddress = new InternetAddress(to); message.setRecipient(Message.RecipientType.TO, toAddress); // 设置邮件标题 message.setSubject(title); // 设置邮件的内容体 message.setContent(text, "text/html;charset=UTF-8"); // 发送邮件 Transport.send(message); return true; }catch (Exception e){ e.printStackTrace(); } return false; } }
4.Service层(发送方法)
package com.itbaizhan.travel.service; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.itbaizhan.travel.bean.Result; import com.itbaizhan.travel.mapper.MemberMapper; import com.itbaizhan.travel.pojo.Member; import com.itbaizhan.travel.util.MailUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; import java.util.List; import java.util.UUID; @Service public class MemberService { @Autowired private MemberMapper memberMapper; @Autowired private BCryptPasswordEncoder encoder; @Autowired private MailUtils mailUtils; @Value("${project.path}") private String projectPath; // 注册 public Result register(Member member) { // 1.保存用户 // 验证用户名是否重复 QueryWrapper<Member> queryWrapper = new QueryWrapper(); queryWrapper.eq("username", member.getUsername()); List<Member> members = memberMapper.selectList(queryWrapper); if (members.size() > 0) { return new Result(false, "用户名已存在"); } // 验证手机是否重复 QueryWrapper<Member> queryWrapper1 = new QueryWrapper(); queryWrapper1.eq("phoneNum", member.getPhoneNum()); List<Member> members1 = memberMapper.selectList(queryWrapper1); if (members1.size() > 0) { return new Result(false, "手机已存在"); } // 验证邮箱是否重复 QueryWrapper<Member> queryWrapper2 = new QueryWrapper(); queryWrapper2.eq("email", member.getEmail()); List<Member> members2 = memberMapper.selectList(queryWrapper2); if (members2.size() > 0) { return new Result(false, "邮箱已存在"); } // 加密密码 String password = member.getPassword(); password = encoder.encode(password); member.setPassword(password); // 设置用户状态为false member.setActive(false); // 2.发送激活邮件 // 生成激活码 String activeCode = UUID.randomUUID().toString(); // 给用户的邮箱发送一封邮件,该邮件包含一个链接,链接中包含激活码 String activeUrl = projectPath + "/frontdesk/member/active?activeCode=" + activeCode; String text = "恭喜您注册成功!<a href = '" + activeUrl + "'>点击激活</a>完成账号认证"; mailUtils.sendMail(member.getEmail(), text, "旅游网激活邮件"); // 保存激活码,激活时比对 member.setActiveCode(activeCode); // 保存用户 memberMapper.insert(member); return new Result(true, "注册成功!"); } // 激活用户 public String active(String activeCode) { // 根据激活码查询用户 QueryWrapper<Member> queryWrapper = new QueryWrapper(); queryWrapper.eq("activeCode", activeCode); Member member = memberMapper.selectOne(queryWrapper); // 没有找到用户:激活失败 if (member == null) { return "激活失败!激活码错误!"; } else { member.setActive(true); memberMapper.updateById(member); return "激活成功,请<a href='" + projectPath + "/frontdesk/login'>登录</a>"; } } public Result login(String name, String password) { Member member = null; // 根据用户名查询 if (member == null) { QueryWrapper<Member> queryWrapper = new QueryWrapper(); queryWrapper.eq("username", name); member = memberMapper.selectOne(queryWrapper); } // 根据手机查询 if (member == null) { QueryWrapper<Member> queryWrapper = new QueryWrapper(); queryWrapper.eq("phoneNum", name); member = memberMapper.selectOne(queryWrapper); } // 根据邮箱查询 if (member == null) { QueryWrapper<Member> queryWrapper = new QueryWrapper(); queryWrapper.eq("email", name); member = memberMapper.selectOne(queryWrapper); } // 没有查询到用户 if (member == null) { return new Result(false, "用户名或密码错误"); } // 验证密码 boolean flag = encoder.matches(password, member.getPassword()); if (!flag) { return new Result(false, "用户名或密码错误"); } // 验证是否激活 if (!member.isActive()) { return new Result(false, "用户未激活,请登录邮箱激活用户"); } return new Result(true, "登录成功", member); } }
5.激活方法
激活新用户,点击发送邮箱邮件
激活方法即拿到激活码,在数据库中根据激活码找到用户,将其状态改为true即可。
1、编写MemberService
// 激活用户 public String active(String activeCode){ // 根据激活码查询用户 QueryWrapper<Member> queryWrapper = new QueryWrapper(); queryWrapper.eq("activeCode",activeCode); Member member = memberMapper.selectOne(queryWrapper); // 没有找到用户:激活失败 if(member == null){ return "激活失败!激活码错误!"; }else { member.setActive(true); memberMapper.updateById(member); return "激活成功,请<a href='"+projectPath+"/frontdesk/login'>登录</a>"; } }
2、编写MemberController
@RequestMapping("/active") public ModelAndView active(String activeCode) { ModelAndView modelAndView = new ModelAndView(); String active = memberService.active(activeCode); modelAndView.addObject("message",active); modelAndView.setViewName("/frontdesk/active_result"); return modelAndView; }
3.编写激活结果页面active_result.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8"> <title>激活结果</title> <link rel="stylesheet" type="text/css" href="/frontdesk/css/common.css"> <link rel="stylesheet" href="/frontdesk/css/register.css"> <!--导入jquery--> <script src="/frontdesk/js/jquery-3.3.1.js"></script> <style> .content { width: 100%; height: 585px; background: url(../images/register_bg2.png) no-repeat center; background-size: 100% 780px; overflow: hidden; text-align: center; font-size: 20px; } .content .box { width: 1000px; height: 550px; background-color: #fff; margin: 20px auto } .content .box p { width: 100%; height: 40px; line-height: 40px; background-color: var(--primary-color); text-align: center; color:#fff; } .content .box h4{ margin-top: 150px; font-size: 20px; line-height: 70px; } .content .glyphicon{ color: var(--primary-color); font-size: 55px; vertical-align: middle; margin-right: 20px; } </style> </head> <body> <div class="simple_header"> <div class="logo"> <img src="/frontdesk/images/logo.png" alt=""> </div> <a class="link" href="index.html"><span class="glyphicon glyphicon-home"></span>首页</a> </div> <div class="content"> <div class="box"> <p>提示</p> <h4> <span th:utext="${message}"></span> </h4> </div> </div> </body> </html>