开局移除邮件注册模块,本系统应该只使用管理员账号

This commit is contained in:
柏码の讲师 2023-11-02 23:50:21 +08:00
parent 45151fe13e
commit f8c2228aec
9 changed files with 2 additions and 247 deletions

View File

@ -2,7 +2,6 @@ package com.example.controller;
import com.example.entity.RestBean;
import com.example.entity.vo.request.ConfirmResetVO;
import com.example.entity.vo.request.EmailRegisterVO;
import com.example.entity.vo.request.EmailResetVO;
import com.example.service.AccountService;
import io.swagger.v3.oas.annotations.Operation;
@ -39,24 +38,12 @@ public class AuthorizeController {
@GetMapping("/ask-code")
@Operation(summary = "请求邮件验证码")
public RestBean<Void> askVerifyCode(@RequestParam @Email String email,
@RequestParam @Pattern(regexp = "(register|reset)") String type,
@RequestParam @Pattern(regexp = "(reset)") String type,
HttpServletRequest request){
return this.messageHandle(() ->
accountService.registerEmailVerifyCode(type, String.valueOf(email), request.getRemoteAddr()));
}
/**
* 进行用户注册操作需要先请求邮件验证码
* @param vo 注册信息
* @return 是否注册成功
*/
@PostMapping("/register")
@Operation(summary = "用户注册操作")
public RestBean<Void> register(@RequestBody @Valid EmailRegisterVO vo){
return this.messageHandle(() ->
accountService.registerEmailAccount(vo));
}
/**
* 执行密码重置确认检查验证码是否正确
* @param vo 密码重置信息

View File

@ -1,22 +0,0 @@
package com.example.entity.vo.request;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.Pattern;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
/**
* 用户注册表单信息
*/
@Data
public class EmailRegisterVO {
@Email
String email;
@Length(max = 6, min = 6)
String code;
@Pattern(regexp = "^[a-zA-Z0-9\\u4e00-\\u9fa5]+$")
@Length(min = 1, max = 10)
String username;
@Length(min = 6, max = 20)
String password;
}

View File

@ -32,10 +32,6 @@ public class MailQueueListener {
String email = data.get("email").toString();
Integer code = (Integer) data.get("code");
SimpleMailMessage message = switch (data.get("type").toString()) {
case "register" ->
createMessage("欢迎注册我们的网站",
"您的邮件注册验证码为: "+code+"有效时间3分钟为了保障您的账户安全请勿向他人泄露验证码信息。",
email);
case "reset" ->
createMessage("您的密码重置邮件",
"你好,您正在执行重置密码操作,验证码: "+code+"有效时间3分钟如非本人操作请无视。",

View File

@ -3,14 +3,12 @@ package com.example.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.entity.dto.Account;
import com.example.entity.vo.request.ConfirmResetVO;
import com.example.entity.vo.request.EmailRegisterVO;
import com.example.entity.vo.request.EmailResetVO;
import org.springframework.security.core.userdetails.UserDetailsService;
public interface AccountService extends IService<Account>, UserDetailsService {
Account findAccountByNameOrEmail(String text);
String registerEmailVerifyCode(String type, String email, String address);
String registerEmailAccount(EmailRegisterVO info);
String resetEmailAccountPassword(EmailResetVO info);
String resetConfirm(ConfirmResetVO info);
}

View File

@ -4,7 +4,6 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.dto.Account;
import com.example.entity.vo.request.ConfirmResetVO;
import com.example.entity.vo.request.EmailRegisterVO;
import com.example.entity.vo.request.EmailResetVO;
import com.example.mapper.AccountMapper;
import com.example.service.AccountService;
@ -20,7 +19,6 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;
@ -86,30 +84,6 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
}
}
/**
* 邮件验证码注册账号操作需要检查验证码是否正确以及邮箱用户名是否存在重名
* @param info 注册基本信息
* @return 操作结果null表示正常否则为错误原因
*/
public String registerEmailAccount(EmailRegisterVO info){
String email = info.getEmail();
String code = this.getEmailVerifyCode(email);
if(code == null) return "请先获取验证码";
if(!code.equals(info.getCode())) return "验证码错误,请重新输入";
if(this.existsAccountByEmail(email)) return "该邮件地址已被注册";
String username = info.getUsername();
if(this.existsAccountByUsername(username)) return "该用户名已被他人使用,请重新更换";
String password = passwordEncoder.encode(info.getPassword());
Account account = new Account(null, info.getUsername(),
password, email, Const.ROLE_DEFAULT, new Date());
if(!this.save(account)) {
return "内部错误,注册失败";
} else {
this.deleteEmailVerifyCode(email);
return null;
}
}
/**
* 邮件验证码重置密码操作需要检查验证码是否正确
* @param info 重置基本信息

View File

@ -13,10 +13,6 @@ const router = createRouter({
path: '',
name: 'welcome-login',
component: () => import('@/views/welcome/LoginPage.vue')
}, {
path: 'register',
name: 'welcome-register',
component: () => import('@/views/welcome/RegisterPage.vue')
}, {
path: 'forget',
name: 'welcome-forget',

View File

@ -2,7 +2,7 @@
<div style="width: 100vw;height: 100vh;overflow: hidden;display: flex">
<div style="flex: 1">
<el-image style="width: 100%;height: 100%" fit="cover"
src="https://img1.baidu.com/it/u=4097856652,4033702227&fm=253&fmt=auto&app=120&f=JPEG?w=1422&h=800"/>
src="https://img0.baidu.com/it/u=1201094067,1609021147&fm=253&fmt=auto&app=120&f=JPEG?w=1422&h=800"/>
</div>
<div class="welcome-title">
<div style="font-size: 30px;font-weight: bold">欢迎来到我们的学习平台</div>

View File

@ -39,12 +39,6 @@
<div style="margin-top: 40px">
<el-button @click="userLogin()" style="width: 270px" type="success" plain>立即登录</el-button>
</div>
<el-divider>
<span style="color: grey;font-size: 13px">没有账号</span>
</el-divider>
<div>
<el-button style="width: 270px" @click="router.push('/register')" type="warning" plain>注册账号</el-button>
</div>
</div>
</template>

View File

@ -1,168 +0,0 @@
<template>
<div style="text-align: center;margin: 0 20px">
<div style="margin-top: 100px">
<div style="font-size: 25px;font-weight: bold">注册新用户</div>
<div style="font-size: 14px;color: grey">欢迎注册我们的学习平台请在下方填写相关信息</div>
</div>
<div style="margin-top: 50px">
<el-form :model="form" :rules="rules" @validate="onValidate" ref="formRef">
<el-form-item prop="username">
<el-input v-model="form.username" :maxlength="8" type="text" placeholder="用户名">
<template #prefix>
<el-icon><User /></el-icon>
</template>
</el-input>
</el-form-item>
<el-form-item prop="password">
<el-input v-model="form.password" :maxlength="16" type="password" placeholder="密码">
<template #prefix>
<el-icon><Lock /></el-icon>
</template>
</el-input>
</el-form-item>
<el-form-item prop="password_repeat">
<el-input v-model="form.password_repeat" :maxlength="16" type="password" placeholder="重复密码">
<template #prefix>
<el-icon><Lock /></el-icon>
</template>
</el-input>
</el-form-item>
<el-form-item prop="email">
<el-input v-model="form.email" type="email" placeholder="电子邮件地址">
<template #prefix>
<el-icon><Message /></el-icon>
</template>
</el-input>
</el-form-item>
<el-form-item prop="code">
<el-row :gutter="10" style="width: 100%">
<el-col :span="17">
<el-input v-model="form.code" :maxlength="6" type="text" placeholder="请输入验证码">
<template #prefix>
<el-icon><EditPen /></el-icon>
</template>
</el-input>
</el-col>
<el-col :span="5">
<el-button type="success" @click="validateEmail"
:disabled="!isEmailValid || coldTime > 0">
{{coldTime > 0 ? '请稍后 ' + coldTime + ' 秒' : '获取验证码'}}
</el-button>
</el-col>
</el-row>
</el-form-item>
</el-form>
</div>
<div style="margin-top: 80px">
<el-button style="width: 270px" type="warning" @click="register" plain>立即注册</el-button>
</div>
<div style="margin-top: 20px">
<span style="font-size: 14px;line-height: 15px;color: grey">已有账号? </span>
<el-link type="primary" style="translate: 0 -2px" @click="router.push('/')">立即登录</el-link>
</div>
</div>
</template>
<script setup>
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 {get, post} from "@/net";
const form = reactive({
username: '',
password: '',
password_repeat: '',
email: '',
code: ''
})
const validateUsername = (rule, value, callback) => {
if (value === '') {
callback(new Error('请输入用户名'))
} else if(!/^[a-zA-Z0-9\u4e00-\u9fa5]+$/.test(value)){
callback(new Error('用户名不能包含特殊字符,只能是中文/英文'))
} else {
callback()
}
}
const validatePassword = (rule, value, callback) => {
if (value === '') {
callback(new Error('请再次输入密码'))
} else if (value !== form.password) {
callback(new Error("两次输入的密码不一致"))
} else {
callback()
}
}
const rules = {
username: [
{ validator: validateUsername, trigger: ['blur', 'change'] },
{ min: 2, max: 8, message: '用户名的长度必须在2-8个字符之间', trigger: ['blur', 'change'] },
],
password: [
{ required: true, message: '请输入密码', trigger: 'blur' },
{ min: 6, max: 16, message: '密码的长度必须在6-16个字符之间', trigger: ['blur', 'change'] }
],
password_repeat: [
{ validator: validatePassword, trigger: ['blur', 'change'] },
],
email: [
{ required: true, message: '请输入邮件地址', trigger: 'blur' },
{type: 'email', message: '请输入合法的电子邮件地址', trigger: ['blur', 'change']}
],
code: [
{ required: true, message: '请输入获取的验证码', trigger: 'blur' },
]
}
const formRef = ref()
const isEmailValid = ref(false)
const coldTime = ref(0)
const onValidate = (prop, isValid) => {
if(prop === 'email')
isEmailValid.value = isValid
}
const register = () => {
formRef.value.validate((isValid) => {
if(isValid) {
post('/api/auth/register', {
username: form.username,
password: form.password,
email: form.email,
code: form.code
}, () => {
ElMessage.success('注册成功,欢迎加入我们')
router.push("/")
})
} else {
ElMessage.warning('请完整填写注册表单内容!')
}
})
}
const validateEmail = () => {
coldTime.value = 60
get(`/api/auth/ask-code?email=${form.email}&type=register`, () => {
ElMessage.success(`验证码已发送到邮箱: ${form.email},请注意查收`)
const handle = setInterval(() => {
coldTime.value--
if(coldTime.value === 0) {
clearInterval(handle)
}
}, 1000)
}, undefined, (message) => {
ElMessage.warning(message)
coldTime.value = 0
})
}
</script>
<style scoped>
</style>