完成邮件验证码发送
This commit is contained in:
parent
adf87f1a8d
commit
af7b3c033b
@ -38,6 +38,14 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-mail</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
|
@ -3,6 +3,7 @@ package com.example.controller;
|
||||
import com.example.entity.RestBean;
|
||||
import com.example.service.AuthorizeService;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
import jakarta.validation.constraints.Pattern;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@ -21,8 +22,9 @@ public class AuthorizeController {
|
||||
AuthorizeService service;
|
||||
|
||||
@PostMapping("/valid-email")
|
||||
public RestBean<String> validateEmail(@Pattern(regexp = EMAIL_REGEX) @RequestParam("email") String email){
|
||||
if (service.sendValidateEmail(email))
|
||||
public RestBean<String> validateEmail(@Pattern(regexp = EMAIL_REGEX) @RequestParam("email") String email,
|
||||
HttpSession session){
|
||||
if (service.sendValidateEmail(email, session.getId()))
|
||||
return RestBean.success("邮件已发送,请注意查收");
|
||||
else
|
||||
return RestBean.failure(400, "邮件发送失败,请联系管理员");
|
||||
|
@ -3,5 +3,5 @@ package com.example.service;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
|
||||
public interface AuthorizeService extends UserDetailsService {
|
||||
boolean sendValidateEmail(String email);
|
||||
boolean sendValidateEmail(String email, String sessionId);
|
||||
}
|
||||
|
@ -4,17 +4,35 @@ import com.example.entity.Account;
|
||||
import com.example.mapper.UserMapper;
|
||||
import com.example.service.AuthorizeService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.mail.MailException;
|
||||
import org.springframework.mail.MailSender;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Service
|
||||
public class AuthorizeServiceImpl implements AuthorizeService {
|
||||
|
||||
@Value("${spring.mail.username}")
|
||||
String from;
|
||||
|
||||
@Resource
|
||||
UserMapper mapper;
|
||||
|
||||
@Resource
|
||||
MailSender mailSender;
|
||||
|
||||
@Resource
|
||||
StringRedisTemplate template;
|
||||
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
if(username == null)
|
||||
@ -30,16 +48,26 @@ public class AuthorizeServiceImpl implements AuthorizeService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sendValidateEmail(String email) {
|
||||
/**
|
||||
* 1. 先生成对应的验证码
|
||||
* 2. 把邮箱和对应的验证码直接放到Redis里面(过期时间3分钟,如果此时重新要求发邮件,
|
||||
* 那么,只要剩余时间低于2分钟,就可以重新发送一次,重复此流程)
|
||||
* 3. 发送验证码到指定邮箱
|
||||
* 4. 如果发送失败,把Redis里面的刚刚插入的删除
|
||||
* 5. 用户在注册时,再从Redis里面取出对应键值对,然后看验证码是否一致
|
||||
*/
|
||||
|
||||
return false;
|
||||
public boolean sendValidateEmail(String email, String sessionId) {
|
||||
String key = "email:" + sessionId + ":" + email;
|
||||
if(Boolean.TRUE.equals(template.hasKey(key))) {
|
||||
Long expire = Optional.ofNullable(template.getExpire(key, TimeUnit.SECONDS)).orElse(0L);
|
||||
if(expire > 120) return false;
|
||||
}
|
||||
Random random = new Random();
|
||||
int code = random.nextInt(899999) + 100000;
|
||||
SimpleMailMessage message = new SimpleMailMessage();
|
||||
message.setFrom(from);
|
||||
message.setTo(email);
|
||||
message.setSubject("您的验证邮件");
|
||||
message.setText("验证码是:"+code);
|
||||
try {
|
||||
mailSender.send(message);
|
||||
template.opsForValue().set(key, String.valueOf(code), 3, TimeUnit.MINUTES);
|
||||
return true;
|
||||
} catch (MailException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,4 +3,20 @@ spring:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://192.168.1.88:3306/study?useUnicode=true&characterEncoding=utf-8
|
||||
username: test
|
||||
password: 123456
|
||||
password: 123456
|
||||
mail:
|
||||
host: smtp.163.com
|
||||
username: javastudy111@163.com
|
||||
password: ADNMGCTHSQGDJLUE
|
||||
port: 465
|
||||
properties:
|
||||
from: javastudy111@163.com
|
||||
mail:
|
||||
smtp:
|
||||
socketFactory:
|
||||
class: javax.net.ssl.SSLSocketFactory
|
||||
data:
|
||||
redis:
|
||||
database: 0
|
||||
host: localhost
|
||||
port: 6379
|
@ -44,7 +44,7 @@
|
||||
</el-input>
|
||||
</el-col>
|
||||
<el-col :span="5">
|
||||
<el-button type="success" :disabled="!isEmailValid">获取验证码</el-button>
|
||||
<el-button type="success" @click="validateEmail" :disabled="!isEmailValid">获取验证码</el-button>
|
||||
</el-col>
|
||||
</el-row>
|
||||
</el-form-item>
|
||||
@ -65,6 +65,7 @@ import {EditPen, Lock, Message, User} from "@element-plus/icons-vue";
|
||||
import router from "@/router";
|
||||
import {reactive, ref} from "vue";
|
||||
import {ElMessage} from "element-plus";
|
||||
import {post} from "@/net";
|
||||
|
||||
const form = reactive({
|
||||
username: '',
|
||||
@ -132,6 +133,14 @@ const register = () => {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const validateEmail = () => {
|
||||
post('/api/auth/valid-email', {
|
||||
email: form.email
|
||||
}, (message) => {
|
||||
ElMessage.success(message)
|
||||
})
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
Loading…
x
Reference in New Issue
Block a user