重构。
parent
6f4503684f
commit
71438901a0
|
@ -34,6 +34,12 @@ public class AccountContextController {
|
||||||
return RestfulResult.success("查询成功", result);
|
return RestfulResult.success("查询成功", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("logout")
|
||||||
|
public RestfulResult logout() {
|
||||||
|
service.logout();
|
||||||
|
return RestfulResult.success("注销成功");
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("menus")
|
@GetMapping("menus")
|
||||||
public RestfulResult getMenuTree() {
|
public RestfulResult getMenuTree() {
|
||||||
adminAuthLogic.checkLogin();
|
adminAuthLogic.checkLogin();
|
||||||
|
|
|
@ -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 <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
|
||||||
*/
|
|
||||||
@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("注销成功");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -10,10 +10,13 @@ import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import cn.dev33.satoken.stp.StpLogic;
|
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.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.AccountQueries;
|
||||||
import xyz.zhouxy.plusone.system.application.query.result.AccountDetails;
|
import xyz.zhouxy.plusone.system.application.query.result.AccountDetails;
|
||||||
import xyz.zhouxy.plusone.system.application.query.result.MenuViewObject;
|
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.ChangePasswordCommand;
|
||||||
import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordWithoutLoginCommand;
|
import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordWithoutLoginCommand;
|
||||||
import xyz.zhouxy.plusone.system.domain.model.account.Account;
|
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.Email;
|
||||||
import xyz.zhouxy.plusone.system.domain.model.account.MobilePhone;
|
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.Principal;
|
||||||
|
import xyz.zhouxy.plusone.validator.InvalidInputException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 账号查询本身相关信息
|
* 账号对当前帐号进行操作
|
||||||
*
|
*
|
||||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||||
*/
|
*/
|
||||||
|
@ -47,6 +51,11 @@ public class AccountContextService {
|
||||||
return accountQueries.queryAccountDetails(accountId);
|
return accountQueries.queryAccountDetails(accountId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void logout() {
|
||||||
|
adminAuthLogic.checkLogin();
|
||||||
|
adminAuthLogic.logout();
|
||||||
|
}
|
||||||
|
|
||||||
public List<MenuViewObject> getMenuTree() {
|
public List<MenuViewObject> getMenuTree() {
|
||||||
adminAuthLogic.checkLogin();
|
adminAuthLogic.checkLogin();
|
||||||
long accountId = adminAuthLogic.getLoginIdAsLong();
|
long accountId = adminAuthLogic.getLoginIdAsLong();
|
||||||
|
@ -76,4 +85,18 @@ public class AccountContextService {
|
||||||
accountRepository.save(account);
|
accountRepository.save(account);
|
||||||
adminAuthLogic.logout();
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import static xyz.zhouxy.plusone.system.constant.AuthLogic.adminAuthLogic;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
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.PrincipalType;
|
||||||
import xyz.zhouxy.plusone.system.application.common.util.PrincipalUtil;
|
import xyz.zhouxy.plusone.system.application.common.util.PrincipalUtil;
|
||||||
import xyz.zhouxy.plusone.system.application.exception.AccountLoginException;
|
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.AccountRepository;
|
||||||
import xyz.zhouxy.plusone.system.domain.model.account.Email;
|
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.MobilePhone;
|
||||||
import xyz.zhouxy.plusone.system.domain.model.account.Principal;
|
|
||||||
import xyz.zhouxy.plusone.system.domain.model.account.Username;
|
import xyz.zhouxy.plusone.system.domain.model.account.Username;
|
||||||
|
import xyz.zhouxy.plusone.validator.InvalidInputException;
|
||||||
import xyz.zhouxy.plusone.validator.ValidateDto;
|
import xyz.zhouxy.plusone.validator.ValidateDto;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -43,62 +43,45 @@ public class AdminLoginService {
|
||||||
|
|
||||||
@ValidateDto
|
@ValidateDto
|
||||||
public LoginInfoViewObject loginByPassword(LoginByPasswordCommand command) {
|
public LoginInfoViewObject loginByPassword(LoginByPasswordCommand command) {
|
||||||
Principal principal = PrincipalUtil.getPrincipal(command.getPrincipal());
|
var principal = command.getPrincipal();
|
||||||
Account account;
|
Account account = switch (command.getPrincipalType()) {
|
||||||
if (principal instanceof Email) {
|
case USERNAME -> accountRepository.findByUsername(Username.of(principal));
|
||||||
account = accountRepository.findByEmail((Email) principal);
|
case EMAIL -> accountRepository.findByEmail(Email.of(principal));
|
||||||
} else if (principal instanceof MobilePhone) {
|
case MOBILE_PHONE -> accountRepository.findByMobilePhone(MobilePhone.of(principal));
|
||||||
account = accountRepository.findByMobilePhone((MobilePhone) principal);
|
};
|
||||||
} else {
|
Assert.notNull(account, () -> AccountLoginException.accountNotExistException());
|
||||||
account = accountRepository.findByUsername((Username) principal);
|
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());
|
adminAuthLogic.login(account.getId().orElseThrow(), command.isRememberMe());
|
||||||
|
|
||||||
var accountDetails = accountQueries.queryAccountDetails(account.getId().orElseThrow());
|
var accountDetails = accountQueries.queryAccountDetails(account.getId().orElseThrow());
|
||||||
return LoginInfoViewObject.of(adminAuthLogic.getTokenValue(), accountDetails);
|
return LoginInfoViewObject.of(adminAuthLogic.getTokenValue(), accountDetails);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ValidateDto
|
@ValidateDto
|
||||||
public LoginInfoViewObject loginByOtp(LoginByOtpCommand command) {
|
public LoginInfoViewObject loginByOtp(LoginByOtpCommand command) {
|
||||||
String principal = command.getPrincipal();
|
var principal = command.getPrincipal();
|
||||||
PrincipalType principalType = PrincipalUtil.getPrincipalType(principal);
|
Account account = switch (command.getPrincipalType()) {
|
||||||
String otp = command.getOtp();
|
case EMAIL -> accountRepository.findByEmail(Email.of(principal));
|
||||||
boolean rememberMe = command.isRememberMe();
|
case MOBILE_PHONE -> accountRepository.findByMobilePhone(MobilePhone.of(principal));
|
||||||
|
default -> throw InvalidInputException.unsupportedPrincipalTypeException("输入邮箱地址或手机号");
|
||||||
|
};
|
||||||
|
Assert.notNull(account, () -> AccountLoginException.accountNotExistException());
|
||||||
|
|
||||||
Account account;
|
mailAndSmsVerifyService.checkOtp(principal, command.getOtp());
|
||||||
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("输入邮箱地址或手机号");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (account == null) {
|
adminAuthLogic.login(account.getId().orElseThrow(), command.isRememberMe());
|
||||||
throw AccountLoginException.accountNotExistException();
|
|
||||||
}
|
|
||||||
mailAndSmsVerifyService.checkOtp(principal, otp);
|
|
||||||
adminAuthLogic.login(account.getId().orElseThrow(), rememberMe);
|
|
||||||
|
|
||||||
var accountDetails = accountQueries.queryAccountDetails(account.getId().orElseThrow());
|
var accountDetails = accountQueries.queryAccountDetails(account.getId().orElseThrow());
|
||||||
return LoginInfoViewObject.of(adminAuthLogic.getTokenValue(), accountDetails);
|
return LoginInfoViewObject.of(adminAuthLogic.getTokenValue(), accountDetails);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendOtp(String principal) {
|
public void sendOtp(String principal) {
|
||||||
Principal emailOrMobilePhone = PrincipalUtil.getEmailOrMobilePhone(principal);
|
PrincipalType principalType = PrincipalUtil.getPrincipalType(principal);
|
||||||
if (emailOrMobilePhone instanceof Email) {
|
if (principalType == PrincipalType.EMAIL) {
|
||||||
mailAndSmsVerifyService.sendOtpToEmail((Email) emailOrMobilePhone);
|
mailAndSmsVerifyService.sendOtpToEmail(Email.of(principal));
|
||||||
} else {
|
} else {
|
||||||
mailAndSmsVerifyService.sendOtpToMobilePhone((MobilePhone) emailOrMobilePhone);
|
mailAndSmsVerifyService.sendOtpToMobilePhone(MobilePhone.of(principal));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
|
||||||
*/
|
|
||||||
@Service
|
|
||||||
public class AdminLogoutService {
|
|
||||||
|
|
||||||
public void execute() {
|
|
||||||
adminAuthLogic.checkLogin();
|
|
||||||
adminAuthLogic.logout();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -5,7 +5,6 @@ import java.util.Set;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
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.PrincipalType;
|
||||||
import xyz.zhouxy.plusone.system.application.common.util.PrincipalUtil;
|
import xyz.zhouxy.plusone.system.application.common.util.PrincipalUtil;
|
||||||
import xyz.zhouxy.plusone.system.application.exception.AccountRegisterException;
|
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.MobilePhone;
|
||||||
import xyz.zhouxy.plusone.system.domain.model.account.Password;
|
import xyz.zhouxy.plusone.system.domain.model.account.Password;
|
||||||
import xyz.zhouxy.plusone.system.domain.model.account.Username;
|
import xyz.zhouxy.plusone.system.domain.model.account.Username;
|
||||||
|
import xyz.zhouxy.plusone.validator.InvalidInputException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注册账号服务
|
* 注册账号服务
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ package xyz.zhouxy.plusone.system.application.service.command;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import xyz.zhouxy.plusone.domain.ICommand;
|
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 principal; // 邮箱地址 / 手机号
|
||||||
String otp; // 密码
|
String otp; // 密码
|
||||||
boolean rememberMe; // 记住我
|
boolean rememberMe; // 记住我
|
||||||
|
PrincipalType principalType;
|
||||||
|
|
||||||
// 进入登陆界面时或刷新验证码时,前端发送图形验证码的请求,后端生成 captcha 并暂存到 redis 中,key 为 UUID,将图形和 uuid 响应给前端。
|
// 进入登陆界面时或刷新验证码时,前端发送图形验证码的请求,后端生成 captcha 并暂存到 redis 中,key 为 UUID,将图形和 uuid 响应给前端。
|
||||||
// String uuid; // 校验码的 key
|
// String uuid; // 校验码的 key
|
||||||
|
|
|
@ -2,6 +2,7 @@ package xyz.zhouxy.plusone.system.application.service.command;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import xyz.zhouxy.plusone.domain.ICommand;
|
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 principal; // 用户名 / 邮箱地址 / 手机号
|
||||||
String password; // 密码
|
String password; // 密码
|
||||||
boolean rememberMe; // 记住我
|
boolean rememberMe; // 记住我
|
||||||
|
PrincipalType principalType;
|
||||||
|
|
||||||
// 进入登陆界面时或刷新验证码时,前端发送图形验证码的请求,后端生成 captcha 并暂存到 redis 中,key 为 UUID,将图形和 uuid 响应给前端。
|
// 进入登陆界面时或刷新验证码时,前端发送图形验证码的请求,后端生成 captcha 并暂存到 redis 中,key 为 UUID,将图形和 uuid 响应给前端。
|
||||||
// String uuid; // 校验码的 key
|
// String uuid; // 校验码的 key
|
||||||
|
|
Loading…
Reference in New Issue