SpringBoot实现QQ邮箱注册和登录
程序员的成长之路
共 8506字,需浏览 18分钟
·
2020-12-17 06:36
阅读本文大概需要 6.5 分钟。
来自:blog.csdn.net/qq_42524288
1、登录注册思路
这是一个使用spring boot做的一个qq邮箱注册和登录的项目。
没写前端页面,使用postman测试。有截图详细。
1.1、思路
1.2、整个项目结构图
2、准备
2.1、开启邮箱POP3/SMTP服务
一定要好好保存
,在appliction.properties配置中会用到。2.2、创建一个spring boot项目的时候,一直确认,jdk选择8。
标签的全部依赖<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-mailartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.19version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
dependencies>
2.3、application.properties配置文件
spring.mail.host = smtp.qq.com
spring.mail.username = xxxxx@qq.com
spring.mail.password = xxxxxx
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.default-encoding=UTF-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/email?useSSL=true&characterEncoding=utf-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations=classpath:mapper/*.xml
2.4、创建数据库
CREATE DATABASE email;
CREATE TABLE `user` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
3、全部代码类
package com.lu.youxiang.controller;
import com.lu.youxiang.service.MailService;
import com.lu.youxiang.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
@Controller
public class UserController {
@Autowired
private MailService mailService;
@PostMapping("/sendEmail")
@ResponseBody
public String sendEmail(String email, HttpSession httpSession){
mailService.sendMimeMail(email, httpSession);
return "sucess";
}
@PostMapping("/regist")
@ResponseBody
public String regist(UserVo userVo, HttpSession session){
mailService.registered(userVo,session);
return "sucess";
}
@PostMapping("/login")
@ResponseBody
public String login(String email, String password){
mailService.loginIn(email,password);
return "sucess";
}
}
package com.lu.youxiang.mapper;
import com.lu.youxiang.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface UserMapper {
/**
* 注册,插入数据
* @param user
*/
void insertUser(User user);
/**
* 根据邮箱查询
* @param email
* @return
*/
User queryByEmail(String email);
}
package com.lu.youxiang.pojo;
public class User {
private String username;
private String password;
private String email;
//get和set方法省略了,自己生成一下
}
package com.lu.youxiang.service;
import com.lu.youxiang.mapper.UserMapper;
import com.lu.youxiang.pojo.User;
import com.lu.youxiang.vo.UserVo;
import com.lu.youxiang.vo.UserVoToUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpSession;
import java.util.Random;
public class MailService {
private JavaMailSender mailSender;//一定要用@Autowired
private UserMapper userMapper;//注入UserMapper,交给bena
//application.properties中已配置的值
private String from;
/**
* 给前端输入的邮箱,发送验证码
* @param email
* @param session
* @return
*/
public boolean sendMimeMail( String email, HttpSession session) {
try {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject("验证码邮件");//主题
//生成随机数
String code = randomCode();
//将随机数放置到session中
session.setAttribute("email",email);
session.setAttribute("code",code);
mailMessage.setText("您收到的验证码是:"+code);//内容
mailMessage.setTo(email);//发给谁
mailMessage.setFrom(from);//你自己的邮箱
mailSender.send(mailMessage);//发送
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
/**
* 随机生成6位数的验证码
* @return String code
*/
public String randomCode(){
StringBuilder str = new StringBuilder();
Random random = new Random();
for (int i = 0; i < 6; i++) {
str.append(random.nextInt(10));
}
return str.toString();
}
/**
* 检验验证码是否一致
* @param userVo
* @param session
* @return
*/
public boolean registered(UserVo userVo, HttpSession session){
//获取session中的验证信息
String email = (String) session.getAttribute("email");
String code = (String) session.getAttribute("code");
//获取表单中的提交的验证信息
String voCode = userVo.getCode();
//如果email数据为空,或者不一致,注册失败
if (email == null || email.isEmpty()){
//return "error,请重新注册";
return false;
}else if (!code.equals(voCode)){
//return "error,请重新注册";
return false;
}
//保存数据
User user = UserVoToUser.toUser(userVo);
//将数据写入数据库
userMapper.insertUser(user);
//跳转成功页面
return true;
}
/**
* 通过输入email查询password,然后比较两个password,如果一样,登录成功
* @param email
* @param password
* @return
*/
public boolean loginIn(String email, String password){
User user = userMapper.queryByEmail(email);
if(!user.getPassword().equals(password)){
return false;
}
System.out.println("登录成功:数据库密码是:"+user.getPassword());
return true;
}
}
package com.lu.youxiang.vo;
public class UserVo {
private String username;
private String password;
private String email;
// 验证码
private String code;
//省略了get和set方法,自己生成一下
}
package com.lu.youxiang.vo;
import com.lu.youxiang.pojo.User;
public class UserVoToUser {
/**
* 将表单中的对象转化为数据库中存储的用户对象(剔除表单中的code)
* @param userVo
* @return
*/
public static User toUser(UserVo userVo) {
//创建一个数据库中存储的对象
User user = new User();
//传值
user.setUsername(userVo.getUsername());
user.setPassword(userVo.getPassword());
user.setEmail(userVo.getEmail());
// 返回包装后的对象
return user;
}
}
在resources包下创建mapper包,用来放xml,然后再这个包中创建UserMapper.xml,内容如下。
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lu.youxiang.mapper.UserMapper">
<insert id="insertUser" parameterType="com.lu.youxiang.pojo.User">
insert into user (username,password,email)
values (#{username},#{password},#{email})
insert>
<select id="queryByEmail" resultType="com.lu.youxiang.pojo.User">
select *
from user
where email = #{email}
select>
mapper>
4、使用postman测试
打开后,点击左上角的file,再点击New Tab,就会出来一个页面。
(或者使用
Ctrl+T
快捷键)4.1、测试发送邮件
http://localhost:8080/sendEmail?email=123456@qq.com
如下图
4.2、测试注册
http://localhost:8080/regist
4.3、测试登录
http://localhost:8080/login?email=123456@qq.com&password=12345
推荐阅读:
微信扫描二维码,关注我的公众号
朕已阅
评论