用户注册使用邮件验证进行

This commit is contained in:
LittleBoy 2021-10-25 16:30:24 +08:00
parent 613e41f8fb
commit 9466c1f262
9 changed files with 163 additions and 13 deletions

View File

@ -6,7 +6,8 @@ import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
// 主要是产生响应的消息队列及交换机
// 并且将两者绑定器来
@Configuration
public class RabbitConfig {
/**

View File

@ -1,7 +1,7 @@
package org.example.shop.config;
public class RabbitMQConfig {
public static final String RABBITMQ_DEFAULT_TOPIC = "default_message_topic";
public static final String RABBITMQ_DEFAULT_DIRECT_EXCHANGE = "default_message_exchange";
public static final String RABBITMQ_DEFAULT_DIRECT_ROUTING = "default_message_routing";
public static final String RABBITMQ_DEFAULT_TOPIC = "send_email_topic";
public static final String RABBITMQ_DEFAULT_DIRECT_EXCHANGE = "send_email_exchange";
public static final String RABBITMQ_DEFAULT_DIRECT_ROUTING = "send_email_routing";
}

View File

@ -2,19 +2,28 @@ package org.example.shop.consumer;
import lombok.extern.slf4j.Slf4j;
import org.example.shop.config.RabbitMQConfig;
import org.example.shop.service.MailService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Map;
@Component
@RabbitListener(queues = RabbitMQConfig.RABBITMQ_DEFAULT_TOPIC)
@Component // 1.让spring管理类
@RabbitListener(queues = RabbitMQConfig.RABBITMQ_DEFAULT_TOPIC) // 2.设置要订阅的主题
@Slf4j
public class RabbitMessageConsumer {
// 绑定处理
@Resource
private MailService mailService;
// 3.绑定处理 队列中的消息来了后 的处理方法
// 注意 方法的参数 发送的数据类型和此处的参数类型要一致
@RabbitHandler
public void process(Map<String, Object> map) {
public void process(Map<String, String> map) {
log.info("获取到一条消息:" + map.toString());
// 需要发送验证邮件
boolean b = mailService.sendVerifyEmail(map.get("email"), map.get("token"));
log.info("发送验证邮件成功了?" + b);
}
}

View File

@ -98,12 +98,27 @@ public class UserController {
UserInfo userInfo = new UserInfo();
userInfo.setUsername(userReg.getUsername()).setPassword(userReg.getPassword());
userInfo.setStatus(1);
try{
userInfo = userService.save(userInfo);
}catch (Exception e){
e.printStackTrace();
return res.setCode(1).setMessage(e.getMessage());
}
if (null != userInfo) {
userInfo.setPassword(null);
}
// 需要发送验证码 ?
// 发送需求到消息队列中
// 实际token应该随机生成 并且保存到redis或者数据库中 用于下一步的验证
mailService.sendVerifyEmailMessage(userReg.getUsername(), "xxakjdafjefiuebiuadifareg");
return res.setCode(0).setData(userInfo);
}
@RequestMapping("/verify")
public String verify(String token) {
// 原则 应该查询redis或者数据库值类的 判断token是否正确
// 已经更新用户的状态
return "success," + token;
}
}

View File

@ -2,4 +2,18 @@ package org.example.shop.service;
public interface MailService {
boolean sendVerifyCode(String to, String code);
/**
* 给指定邮箱发送一个验证链接
* @param to 邮箱
* @param token 验证的token
*/
boolean sendVerifyEmail(String to, String token);
/**
* 将发送邮件的消息发送队列中
* @param to
* @param token
*/
void sendVerifyEmailMessage(String to,String token);
}

View File

@ -1,6 +1,11 @@
package org.example.shop.service.impl;
import org.example.shop.config.RabbitMQConfig;
import org.example.shop.service.MailService;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
@ -8,17 +13,26 @@ import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
@Service
public class MailServiceImpl implements MailService {
private JavaMailSender javaMailSender;
@Value("${spring.mail.from}")
private String from;
@Value("${server.port}")
private int port;
@Autowired
public void setJavaMailSender(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
@Resource
private RabbitTemplate rabbitTemplate;
@Override
public boolean sendVerifyCode(String to, String code) {
SimpleMailMessage message = new SimpleMailMessage();
@ -35,4 +49,46 @@ public class MailServiceImpl implements MailService {
}
return false;
}
@Override
public boolean sendVerifyEmail(String to, String token) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);//谁发的
message.setTo(to); //谁要接收
message.setSubject("验证邮箱 - 如果不是你的操作请忽略");//邮件标题
message.setText("请点击下面的链接验证您的邮箱\n" +
"http://localhost:" + port + "/user/verify?token=" + token);//邮件内容
try {
// 发送邮件
javaMailSender.send(message);
return true;
} catch (MailException e) {
e.printStackTrace();
}
return false;
}
public void sendVerifyEmailMessage(String to, String token) {
// 发送的数据
Map<String, String> data = new HashMap<>();
data.put("email", to);
data.put("token", token);
int delay = 10000;
// 发送
rabbitTemplate.convertAndSend(
RabbitMQConfig.RABBITMQ_DEFAULT_DIRECT_EXCHANGE, // 消息发送给谁
RabbitMQConfig.RABBITMQ_DEFAULT_DIRECT_ROUTING, // 消息通过哪个路由发送
data);
}
private MessagePostProcessor getDelay(int delay) {
return new MessagePostProcessor() {
@Override
public Message postProcessMessage(Message message) throws AmqpException {
//给消息设置延迟毫秒值
message.getMessageProperties().setExpiration(String.valueOf(delay));
return message;
}
};
}
}

View File

@ -8,6 +8,7 @@ spring:
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
show-sql: true
# 消息队列的配置
rabbitmq:
host: 127.0.0.1
port: 5672

View File

@ -8,9 +8,13 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.Assert;
import javax.annotation.Resource;
@SpringBootTest
@RunWith(SpringRunner.class)
public class TestEmailSend {
private MailService mailService;
@Autowired
@ -23,4 +27,11 @@ public class TestEmailSend {
boolean b = mailService.sendVerifyCode("yaclty@qq.com", "123323");
Assert.isTrue(b, "发送邮件失败");
}
@Test
public void testSendQueue() {
mailService.sendVerifyEmail("yaclty@qq.com", "123323");
}
}

View File

@ -1,10 +1,53 @@
<template>
<h1>页面bBBBBBBBBBBBBBBBB</h1>
<div style="width: 400px;margin:auto">
<a-form-item label="邮箱地址">
<a-input v-model:value="username"/>
</a-form-item>
<a-form-item label="密码">
<a-input v-model:value="password"/>
</a-form-item>
<a-button @click="create">注册</a-button>
</div>
</template>
<script>
import {message} from "ant-design-vue";
import axios from "axios";
export default {
name: "PageB"
name: "PageB",
data() {
return {
username: 'yaclty@qq.com',
password: '123123',
code: '983584',
sendLoading: false,
saveLoading: false
}
},
methods: {
create() {
this.saveLoading = true
axios.post('http://localhost:9001/user/create', {
username: this.username,
password: this.password,
code: this.code
})
.then(function (response) {
const result = response.data;
if (result.code == 0) {
message.info("注册成功", 5)
} else {
message.error("注册失败:" + result.message, 5)
}
})
.catch(function (e) {
message.error(e.message, 5)
})
.finally(() => this.saveLoading = false)
}
}
}
</script>