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