From 71438901a0c71ef36611d5315af3a1b62822dbf2 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 14 Dec 2022 13:41:57 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AccountContextController.java | 6 ++ .../controller/AdminLogoutController.java | 30 --------- .../service/AccountContextService.java | 25 ++++++- .../service/AdminLoginService.java | 65 +++++++------------ .../service/AdminLogoutService.java | 19 ------ .../service/RegisterAccountService.java | 2 +- .../command/ChangePasswordByOtpCommand.java | 11 ++++ .../service/command/LoginByOtpCommand.java | 2 + .../command/LoginByPasswordCommand.java | 2 + 9 files changed, 70 insertions(+), 92 deletions(-) delete mode 100644 plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/controller/AdminLogoutController.java delete mode 100644 plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AdminLogoutService.java create mode 100644 plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/ChangePasswordByOtpCommand.java diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/controller/AccountContextController.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/controller/AccountContextController.java index b221879..4518373 100644 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/controller/AccountContextController.java +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/controller/AccountContextController.java @@ -34,6 +34,12 @@ public class AccountContextController { return RestfulResult.success("查询成功", result); } + @GetMapping("logout") + public RestfulResult logout() { + service.logout(); + return RestfulResult.success("注销成功"); + } + @GetMapping("menus") public RestfulResult getMenuTree() { adminAuthLogic.checkLogin(); diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/controller/AdminLogoutController.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/controller/AdminLogoutController.java deleted file mode 100644 index a7c1a81..0000000 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/controller/AdminLogoutController.java +++ /dev/null @@ -1,30 +0,0 @@ -package xyz.zhouxy.plusone.system.application.controller; - -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import xyz.zhouxy.plusone.system.application.service.AdminLogoutService; -import xyz.zhouxy.plusone.util.RestfulResult; - -/** - * Admin 账号登出 - * - * @author ZhouXY - */ -@RestController -@RequestMapping("logout") -public class AdminLogoutController { - - private final AdminLogoutService service; - - public AdminLogoutController(AdminLogoutService service) { - this.service = service; - } - - @GetMapping - public RestfulResult execute() { - service.execute(); - return RestfulResult.success("注销成功"); - } -} diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AccountContextService.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AccountContextService.java index 801e0b3..b40d60c 100644 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AccountContextService.java +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AccountContextService.java @@ -10,10 +10,13 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import cn.dev33.satoken.stp.StpLogic; +import cn.hutool.core.lang.Assert; import xyz.zhouxy.plusone.system.application.common.util.PrincipalUtil; +import xyz.zhouxy.plusone.system.application.exception.AccountLoginException; import xyz.zhouxy.plusone.system.application.query.AccountQueries; import xyz.zhouxy.plusone.system.application.query.result.AccountDetails; import xyz.zhouxy.plusone.system.application.query.result.MenuViewObject; +import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordByOtpCommand; import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordCommand; import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordWithoutLoginCommand; import xyz.zhouxy.plusone.system.domain.model.account.Account; @@ -21,9 +24,10 @@ import xyz.zhouxy.plusone.system.domain.model.account.AccountRepository; import xyz.zhouxy.plusone.system.domain.model.account.Email; import xyz.zhouxy.plusone.system.domain.model.account.MobilePhone; import xyz.zhouxy.plusone.system.domain.model.account.Principal; +import xyz.zhouxy.plusone.validator.InvalidInputException; /** - * 账号查询本身相关信息 + * 账号对当前帐号进行操作 * * @author ZhouXY */ @@ -47,6 +51,11 @@ public class AccountContextService { return accountQueries.queryAccountDetails(accountId); } + public void logout() { + adminAuthLogic.checkLogin(); + adminAuthLogic.logout(); + } + public List getMenuTree() { adminAuthLogic.checkLogin(); long accountId = adminAuthLogic.getLoginIdAsLong(); @@ -76,4 +85,18 @@ public class AccountContextService { accountRepository.save(account); adminAuthLogic.logout(); } + + @Transactional + public void changePasswordByOtp(ChangePasswordByOtpCommand command) { + + var principal = command.getAccount(); + Account account = switch (command.getPrincipalType()) { + case EMAIL -> accountRepository.findByEmail(Email.of(principal)); + case MOBILE_PHONE -> accountRepository.findByMobilePhone(MobilePhone.of(principal)); + default -> throw InvalidInputException.unsupportedPrincipalTypeException("输入邮箱地址或手机号"); + }; + Assert.notNull(account, () -> AccountLoginException.accountNotExistException()); + + mailAndSmsVerifyService.checkOtp(principal, command.getOtp()); + } } diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AdminLoginService.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AdminLoginService.java index 18cf975..5f12908 100644 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AdminLoginService.java +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AdminLoginService.java @@ -5,7 +5,7 @@ import static xyz.zhouxy.plusone.system.constant.AuthLogic.adminAuthLogic; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import xyz.zhouxy.plusone.exception.InvalidInputException; +import cn.hutool.core.lang.Assert; import xyz.zhouxy.plusone.system.application.common.util.PrincipalType; import xyz.zhouxy.plusone.system.application.common.util.PrincipalUtil; import xyz.zhouxy.plusone.system.application.exception.AccountLoginException; @@ -17,8 +17,8 @@ import xyz.zhouxy.plusone.system.domain.model.account.Account; import xyz.zhouxy.plusone.system.domain.model.account.AccountRepository; import xyz.zhouxy.plusone.system.domain.model.account.Email; import xyz.zhouxy.plusone.system.domain.model.account.MobilePhone; -import xyz.zhouxy.plusone.system.domain.model.account.Principal; import xyz.zhouxy.plusone.system.domain.model.account.Username; +import xyz.zhouxy.plusone.validator.InvalidInputException; import xyz.zhouxy.plusone.validator.ValidateDto; /** @@ -43,62 +43,45 @@ public class AdminLoginService { @ValidateDto public LoginInfoViewObject loginByPassword(LoginByPasswordCommand command) { - Principal principal = PrincipalUtil.getPrincipal(command.getPrincipal()); - Account account; - if (principal instanceof Email) { - account = accountRepository.findByEmail((Email) principal); - } else if (principal instanceof MobilePhone) { - account = accountRepository.findByMobilePhone((MobilePhone) principal); - } else { - account = accountRepository.findByUsername((Username) principal); - } + var principal = command.getPrincipal(); + Account account = switch (command.getPrincipalType()) { + case USERNAME -> accountRepository.findByUsername(Username.of(principal)); + case EMAIL -> accountRepository.findByEmail(Email.of(principal)); + case MOBILE_PHONE -> accountRepository.findByMobilePhone(MobilePhone.of(principal)); + }; + Assert.notNull(account, () -> AccountLoginException.accountNotExistException()); + var isPasswordCorrect = account.checkPassword(command.getPassword()); + Assert.isTrue(isPasswordCorrect, () -> AccountLoginException.passwordErrorException()); - if (account == null) { - throw AccountLoginException.accountNotExistException(); - } - @SuppressWarnings("null") - boolean isPasswordCorrect = account.checkPassword(command.getPassword()); - if (!isPasswordCorrect) { - throw AccountLoginException.passwordErrorException(); - } adminAuthLogic.login(account.getId().orElseThrow(), command.isRememberMe()); - var accountDetails = accountQueries.queryAccountDetails(account.getId().orElseThrow()); return LoginInfoViewObject.of(adminAuthLogic.getTokenValue(), accountDetails); } @ValidateDto public LoginInfoViewObject loginByOtp(LoginByOtpCommand command) { - String principal = command.getPrincipal(); - PrincipalType principalType = PrincipalUtil.getPrincipalType(principal); - String otp = command.getOtp(); - boolean rememberMe = command.isRememberMe(); + var principal = command.getPrincipal(); + Account account = switch (command.getPrincipalType()) { + case EMAIL -> accountRepository.findByEmail(Email.of(principal)); + case MOBILE_PHONE -> accountRepository.findByMobilePhone(MobilePhone.of(principal)); + default -> throw InvalidInputException.unsupportedPrincipalTypeException("输入邮箱地址或手机号"); + }; + Assert.notNull(account, () -> AccountLoginException.accountNotExistException()); - Account account; - if (principalType == PrincipalType.EMAIL) { - account = accountRepository.findByEmail(Email.of(principal)); - } else if (principalType == PrincipalType.MOBILE_PHONE) { - account = accountRepository.findByMobilePhone(MobilePhone.of(principal)); - } else { - throw InvalidInputException.unsupportedPrincipalTypeException("输入邮箱地址或手机号"); - } + mailAndSmsVerifyService.checkOtp(principal, command.getOtp()); - if (account == null) { - throw AccountLoginException.accountNotExistException(); - } - mailAndSmsVerifyService.checkOtp(principal, otp); - adminAuthLogic.login(account.getId().orElseThrow(), rememberMe); + adminAuthLogic.login(account.getId().orElseThrow(), command.isRememberMe()); var accountDetails = accountQueries.queryAccountDetails(account.getId().orElseThrow()); return LoginInfoViewObject.of(adminAuthLogic.getTokenValue(), accountDetails); } public void sendOtp(String principal) { - Principal emailOrMobilePhone = PrincipalUtil.getEmailOrMobilePhone(principal); - if (emailOrMobilePhone instanceof Email) { - mailAndSmsVerifyService.sendOtpToEmail((Email) emailOrMobilePhone); + PrincipalType principalType = PrincipalUtil.getPrincipalType(principal); + if (principalType == PrincipalType.EMAIL) { + mailAndSmsVerifyService.sendOtpToEmail(Email.of(principal)); } else { - mailAndSmsVerifyService.sendOtpToMobilePhone((MobilePhone) emailOrMobilePhone); + mailAndSmsVerifyService.sendOtpToMobilePhone(MobilePhone.of(principal)); } } } diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AdminLogoutService.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AdminLogoutService.java deleted file mode 100644 index dd83505..0000000 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AdminLogoutService.java +++ /dev/null @@ -1,19 +0,0 @@ -package xyz.zhouxy.plusone.system.application.service; - -import static xyz.zhouxy.plusone.system.constant.AuthLogic.adminAuthLogic; - -import org.springframework.stereotype.Service; - -/** - * Admin 账号登出 - * - * @author ZhouXY - */ -@Service -public class AdminLogoutService { - - public void execute() { - adminAuthLogic.checkLogin(); - adminAuthLogic.logout(); - } -} diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/RegisterAccountService.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/RegisterAccountService.java index 370a15f..e05c4b8 100644 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/RegisterAccountService.java +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/RegisterAccountService.java @@ -5,7 +5,6 @@ import java.util.Set; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import xyz.zhouxy.plusone.exception.InvalidInputException; import xyz.zhouxy.plusone.system.application.common.util.PrincipalType; import xyz.zhouxy.plusone.system.application.common.util.PrincipalUtil; import xyz.zhouxy.plusone.system.application.exception.AccountRegisterException; @@ -18,6 +17,7 @@ import xyz.zhouxy.plusone.system.domain.model.account.Email; import xyz.zhouxy.plusone.system.domain.model.account.MobilePhone; import xyz.zhouxy.plusone.system.domain.model.account.Password; import xyz.zhouxy.plusone.system.domain.model.account.Username; +import xyz.zhouxy.plusone.validator.InvalidInputException; /** * 注册账号服务 diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/ChangePasswordByOtpCommand.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/ChangePasswordByOtpCommand.java new file mode 100644 index 0000000..8cf3bff --- /dev/null +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/ChangePasswordByOtpCommand.java @@ -0,0 +1,11 @@ +package xyz.zhouxy.plusone.system.application.service.command; + +import lombok.Data; +import xyz.zhouxy.plusone.system.application.common.util.PrincipalType; + +@Data +public class ChangePasswordByOtpCommand { + String account; + String otp; + PrincipalType principalType; +} diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/LoginByOtpCommand.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/LoginByOtpCommand.java index 2bf1efc..4e43c83 100644 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/LoginByOtpCommand.java +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/LoginByOtpCommand.java @@ -2,6 +2,7 @@ package xyz.zhouxy.plusone.system.application.service.command; import lombok.Data; import xyz.zhouxy.plusone.domain.ICommand; +import xyz.zhouxy.plusone.system.application.common.util.PrincipalType; /** * 登录命令 @@ -14,6 +15,7 @@ public class LoginByOtpCommand implements ICommand { String principal; // 邮箱地址 / 手机号 String otp; // 密码 boolean rememberMe; // 记住我 + PrincipalType principalType; // 进入登陆界面时或刷新验证码时,前端发送图形验证码的请求,后端生成 captcha 并暂存到 redis 中,key 为 UUID,将图形和 uuid 响应给前端。 // String uuid; // 校验码的 key diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/LoginByPasswordCommand.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/LoginByPasswordCommand.java index 518260a..47c6bd5 100644 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/LoginByPasswordCommand.java +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/LoginByPasswordCommand.java @@ -2,6 +2,7 @@ package xyz.zhouxy.plusone.system.application.service.command; import lombok.Data; import xyz.zhouxy.plusone.domain.ICommand; +import xyz.zhouxy.plusone.system.application.common.util.PrincipalType; /** * 登录命令 @@ -14,6 +15,7 @@ public class LoginByPasswordCommand implements ICommand { String principal; // 用户名 / 邮箱地址 / 手机号 String password; // 密码 boolean rememberMe; // 记住我 + PrincipalType principalType; // 进入登陆界面时或刷新验证码时,前端发送图形验证码的请求,后端生成 captcha 并暂存到 redis 中,key 为 UUID,将图形和 uuid 响应给前端。 // String uuid; // 校验码的 key