密码重置接口设计

This commit is contained in:
Amadeus 2024-05-25 21:58:16 +08:00
parent 9e6e155d5a
commit 505c07bde6
5 changed files with 76 additions and 1 deletions

View File

@ -1,7 +1,9 @@
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 jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
@ -11,6 +13,7 @@ import jakarta.validation.constraints.Pattern;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.function.Function;
import java.util.function.Supplier;
@Validated
@ -30,7 +33,20 @@ public class AuthorizeController {
@PostMapping("/register")
public RestBean<Void> register(@RequestBody @Valid EmailRegisterVO vo){
return this.messageHandle(()-> service.registerEmailAccount(vo));
return this.messageHandle(vo , service::registerEmailAccount);
}
@PostMapping("reset-confirm")
public RestBean<Void> resetConfirm(@RequestBody @Valid ConfirmResetVO vo){
return this.messageHandle(vo, service::resetConfirm);
}
@PostMapping("reset-password")
public RestBean<Void> resetConfirm(@RequestBody @Valid EmailResetVO vo){
return this.messageHandle(vo, service::resetEmailAccountPassword);
}
private <T> RestBean<Void> messageHandle(T vo , Function<T, String> function){
return messageHandle(()-> function.apply(vo));
}
private RestBean<Void > messageHandle(Supplier<String> action){
String message = action.get();

View File

@ -0,0 +1,15 @@
package com.example.entity.vo.request;
import jakarta.validation.constraints.Email;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
@Data
@AllArgsConstructor
public class ConfirmResetVO {
@Email
String email;
@Length(max = 6 , min = 6)
String code;
}

View File

@ -0,0 +1,15 @@
package com.example.entity.vo.request;
import jakarta.validation.constraints.Email;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
@Data
public class EmailResetVO {
@Email
String email;
@Length(min = 6 , max = 6)
String code;
@Length(min = 6 ,max = 20)
String password;
}

View File

@ -2,7 +2,9 @@ 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.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
@ -10,4 +12,6 @@ public interface AccountService extends IService<Account> , UserDetailsService {
public Account findAccountByNameOrEmail(String text);
String registerEmailVerityCode(String type , String email , String ip);
String registerEmailAccount(EmailRegisterVO v0);
String resetConfirm(ConfirmResetVO vo);
String resetEmailAccountPassword(EmailResetVO vo);
}

View File

@ -3,7 +3,9 @@ package com.example.service.impl;
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;
import com.example.utils.Const;
@ -88,6 +90,29 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper , Account> imp
}
}
@Override
public String resetEmailAccountPassword(EmailResetVO vo) {
String email = vo.getEmail();
String verity = this.resetConfirm(new ConfirmResetVO(email, vo.getCode()));
if (verity !=null) return verity;
String password = encoder.encode(vo.getPassword());
boolean update = this.update().eq("email",email).set("password",password).update();
if (update){
stringRedisTemplate.delete(Const.VERIFY_EMAIL_DATA + email);
}
return null;
}
@Override
public String resetConfirm(ConfirmResetVO vo) {
String email = vo.getEmail();
String code = stringRedisTemplate.opsForValue().get(Const.VERIFY_EMAIL_DATA + email);
if (code == null) return "请先获取验证码";
if (!code.equals(vo.getCode())) return "验证码错误,请重新输入";
return null;
}
public Account findAccountByNameOrEmail(String text){
return this.query()
.eq("username",text).or()