From 505c07bde68a81b74ba81b05fc27ecb2cec034ad Mon Sep 17 00:00:00 2001 From: Amadeus Date: Sat, 25 May 2024 21:58:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=86=E7=A0=81=E9=87=8D=E7=BD=AE=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E8=AE=BE=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AuthorizeController.java | 18 ++++++++++++- .../entity/vo/request/ConfirmResetVO.java | 15 +++++++++++ .../entity/vo/request/EmailResetVO.java | 15 +++++++++++ .../com/example/service/AccountService.java | 4 +++ .../service/impl/AccountServiceImpl.java | 25 +++++++++++++++++++ 5 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/example/entity/vo/request/ConfirmResetVO.java create mode 100644 src/main/java/com/example/entity/vo/request/EmailResetVO.java diff --git a/src/main/java/com/example/controller/AuthorizeController.java b/src/main/java/com/example/controller/AuthorizeController.java index afff70d..356da24 100644 --- a/src/main/java/com/example/controller/AuthorizeController.java +++ b/src/main/java/com/example/controller/AuthorizeController.java @@ -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 register(@RequestBody @Valid EmailRegisterVO vo){ - return this.messageHandle(()-> service.registerEmailAccount(vo)); + return this.messageHandle(vo , service::registerEmailAccount); + } + + @PostMapping("reset-confirm") + public RestBean resetConfirm(@RequestBody @Valid ConfirmResetVO vo){ + return this.messageHandle(vo, service::resetConfirm); + } + @PostMapping("reset-password") + public RestBean resetConfirm(@RequestBody @Valid EmailResetVO vo){ + return this.messageHandle(vo, service::resetEmailAccountPassword); + } + + private RestBean messageHandle(T vo , Function function){ + return messageHandle(()-> function.apply(vo)); } private RestBean messageHandle(Supplier action){ String message = action.get(); diff --git a/src/main/java/com/example/entity/vo/request/ConfirmResetVO.java b/src/main/java/com/example/entity/vo/request/ConfirmResetVO.java new file mode 100644 index 0000000..57b698c --- /dev/null +++ b/src/main/java/com/example/entity/vo/request/ConfirmResetVO.java @@ -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; +} diff --git a/src/main/java/com/example/entity/vo/request/EmailResetVO.java b/src/main/java/com/example/entity/vo/request/EmailResetVO.java new file mode 100644 index 0000000..fbd015b --- /dev/null +++ b/src/main/java/com/example/entity/vo/request/EmailResetVO.java @@ -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; +} diff --git a/src/main/java/com/example/service/AccountService.java b/src/main/java/com/example/service/AccountService.java index 4aa6e7e..dfd7f19 100644 --- a/src/main/java/com/example/service/AccountService.java +++ b/src/main/java/com/example/service/AccountService.java @@ -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 , 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); } diff --git a/src/main/java/com/example/service/impl/AccountServiceImpl.java b/src/main/java/com/example/service/impl/AccountServiceImpl.java index a4194d1..76f0c6f 100644 --- a/src/main/java/com/example/service/impl/AccountServiceImpl.java +++ b/src/main/java/com/example/service/impl/AccountServiceImpl.java @@ -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 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()