From 7a5279a38c14e4a284f14426f7e9c77638c9bc70 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sun, 11 Dec 2022 00:21:40 +0800 Subject: [PATCH 01/18] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xyz/zhouxy/plusone/validatortest/BaseValidator2Test.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plusone-basic/plusone-basic-infrastructure/src/test/java/xyz/zhouxy/plusone/validatortest/BaseValidator2Test.java b/plusone-basic/plusone-basic-infrastructure/src/test/java/xyz/zhouxy/plusone/validatortest/BaseValidator2Test.java index 7a963d8..e908fe1 100644 --- a/plusone-basic/plusone-basic-infrastructure/src/test/java/xyz/zhouxy/plusone/validatortest/BaseValidator2Test.java +++ b/plusone-basic/plusone-basic-infrastructure/src/test/java/xyz/zhouxy/plusone/validatortest/BaseValidator2Test.java @@ -32,10 +32,10 @@ class LoginCommandValidator extends BaseValidator2 { public static final LoginCommandValidator INSTANCE = new LoginCommandValidator(); private LoginCommandValidator() { - ruleFor(loginCommand -> loginCommand.getAccount()) + ruleFor(LoginCommand::getAccount) .notNull("邮箱地址不能为空") .matchesOr(new String[] { RegexConsts.EMAIL, RegexConsts.MOBILE_PHONE }, value -> new RuntimeException('"' + value + "\" 不是邮箱地址或手机号")); - ruleFor(loginCommand -> loginCommand.getPwd()) + ruleFor(LoginCommand::getPwd) .notNull("密码不能为空") .notEmpty("密码不能为空") .matches(RegexConsts.PASSWORD, "密码格式错误"); From 00269987c7fc3b39033a87107e90769f30c54457 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sun, 11 Dec 2022 00:22:08 +0800 Subject: [PATCH 02/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E6=8B=A6=E6=88=AA=E5=99=A8=E7=89=88=E6=9C=AC=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plusone-basic/plusone-basic-common/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plusone-basic/plusone-basic-common/pom.xml b/plusone-basic/plusone-basic-common/pom.xml index f48323f..85e0354 100644 --- a/plusone-basic/plusone-basic-common/pom.xml +++ b/plusone-basic/plusone-basic-common/pom.xml @@ -39,7 +39,7 @@ xyz.zhouxy.plusone plusone-exception-handler - 0.0.1-SNAPSHOT + 0.0.2-SNAPSHOT From 2a8a18528827001502b4656db1d39b5c62c798a3 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sun, 11 Dec 2022 02:32:25 +0800 Subject: [PATCH 03/18] =?UTF-8?q?=E9=85=8D=E7=BD=AE=20Spring=20devtools?= =?UTF-8?q?=EF=BC=8C=E9=98=B2=E6=AD=A2=E5=87=BA=E7=8E=B0=E5=9B=A0=20Spring?= =?UTF-8?q?=20devtools=20=E8=BF=90=E8=A1=8C=E5=A4=B1=E8=B4=A5=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/META-INF/spring-devtools.properties | 1 + 1 file changed, 1 insertion(+) create mode 100644 plusone-start/src/main/resources/META-INF/spring-devtools.properties diff --git a/plusone-start/src/main/resources/META-INF/spring-devtools.properties b/plusone-start/src/main/resources/META-INF/spring-devtools.properties new file mode 100644 index 0000000..d587606 --- /dev/null +++ b/plusone-start/src/main/resources/META-INF/spring-devtools.properties @@ -0,0 +1 @@ +restart.include.projectcommon=/plusone-[\\w\\.\\-]+\\.jar From eb6d4fbbea75e60cdcf4a0e4fc7e666e0b06eb3f Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sun, 11 Dec 2022 02:34:28 +0800 Subject: [PATCH 04/18] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20DefaultExceptionHand?= =?UTF-8?q?ler=EF=BC=8C=E9=98=B2=E6=AD=A2=E5=85=B6=E6=8B=A6=E6=88=AA?= =?UTF-8?q?=E5=85=B6=E5=AE=83=E5=BC=82=E5=B8=B8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/exception/handler/DefaultExceptionHandler.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/DefaultExceptionHandler.java b/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/DefaultExceptionHandler.java index e991bb8..af53870 100644 --- a/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/DefaultExceptionHandler.java +++ b/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/DefaultExceptionHandler.java @@ -55,7 +55,11 @@ public class DefaultExceptionHandler extends BaseExceptionHandler { HttpStatus.BAD_REQUEST); } - @ExceptionHandler(Exception.class) + @ExceptionHandler({ + IllegalArgumentException.class, + DataAccessException.class, + MethodArgumentNotValidException.class + }) public ResponseEntity handleException(Exception e) { log.error(e.getMessage(), e); return buildExceptionResponse(e); From e084d31f711b842e097e35fc20ff2b5cdae7c57e Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sun, 11 Dec 2022 02:35:08 +0800 Subject: [PATCH 05/18] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E5=A4=9A=E4=BD=99?= =?UTF-8?q?=E4=BE=9D=E8=B5=96=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plusone-basic/plusone-basic-infrastructure/pom.xml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/plusone-basic/plusone-basic-infrastructure/pom.xml b/plusone-basic/plusone-basic-infrastructure/pom.xml index 0a32200..548d37b 100644 --- a/plusone-basic/plusone-basic-infrastructure/pom.xml +++ b/plusone-basic/plusone-basic-infrastructure/pom.xml @@ -49,12 +49,6 @@ spring-boot-starter-validation - - org.springframework.boot - spring-boot-devtools - runtime - - org.postgresql postgresql From 509e195fee58b8b678191c2e5ba329299576a858 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sun, 11 Dec 2022 02:35:37 +0800 Subject: [PATCH 06/18] update file. --- .../src/main/resources/META-INF/spring-devtools.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plusone-start/src/main/resources/META-INF/spring-devtools.properties b/plusone-start/src/main/resources/META-INF/spring-devtools.properties index d587606..56aa70e 100644 --- a/plusone-start/src/main/resources/META-INF/spring-devtools.properties +++ b/plusone-start/src/main/resources/META-INF/spring-devtools.properties @@ -1 +1 @@ -restart.include.projectcommon=/plusone-[\\w\\.\\-]+\\.jar +restart.include.projectcommon=/[\\w\\.\\-]+\\.jar From 0ab296358d81ea89f64dcd44cde4f643c1dee3f2 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sun, 11 Dec 2022 02:37:13 +0800 Subject: [PATCH 07/18] =?UTF-8?q?=E5=B0=86=20AllExceptionHandler=20?= =?UTF-8?q?=E6=8F=90=E5=8F=96=E5=88=B0=20plusone-exception-handler=20?= =?UTF-8?q?=E4=B8=AD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/AllExceptionHandler.java | 29 ------------------- .../handler/AllExceptionHandlerConfig.java | 20 +++++++++++++ plusone-basic/plusone-basic-common/pom.xml | 2 +- 3 files changed, 21 insertions(+), 30 deletions(-) delete mode 100644 plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/AllExceptionHandler.java create mode 100644 plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/AllExceptionHandlerConfig.java diff --git a/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/AllExceptionHandler.java b/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/AllExceptionHandler.java deleted file mode 100644 index 00fb1e4..0000000 --- a/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/AllExceptionHandler.java +++ /dev/null @@ -1,29 +0,0 @@ -package xyz.zhouxy.plusone.exception.handler; - -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.RestControllerAdvice; - -import lombok.extern.slf4j.Slf4j; -import xyz.zhouxy.plusone.util.RestfulResult; - -/** - * 处理所有异常的处理器 - * - * @author ZhouXY - */ -@ConditionalOnProperty(prefix = "plusone.exception", name = "handle-all-exception", havingValue = "true") -@RestControllerAdvice -@Slf4j -public class AllExceptionHandler extends BaseExceptionHandler { - protected AllExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) { - super(exceptionInfoHolder); - } - - @ExceptionHandler(Exception.class) - public ResponseEntity handleException(Throwable e) { - log.error(e.getMessage(), e); - return this.buildExceptionResponse(e); - } -} diff --git a/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/AllExceptionHandlerConfig.java b/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/AllExceptionHandlerConfig.java new file mode 100644 index 0000000..fd05fdb --- /dev/null +++ b/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/AllExceptionHandlerConfig.java @@ -0,0 +1,20 @@ +package xyz.zhouxy.plusone.exception.handler; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler.ExceptionInfoHolder; + +/** + * AllExceptionHandlerConfig + */ +@Configuration +@ConditionalOnProperty(prefix = "plusone.exception", name = "handle-all-exception", havingValue = "true") +public class AllExceptionHandlerConfig { + + @Bean + AllExceptionHandler getAllExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) { + return new AllExceptionHandler(exceptionInfoHolder); + } +} diff --git a/plusone-basic/plusone-basic-common/pom.xml b/plusone-basic/plusone-basic-common/pom.xml index 85e0354..828e5b0 100644 --- a/plusone-basic/plusone-basic-common/pom.xml +++ b/plusone-basic/plusone-basic-common/pom.xml @@ -39,7 +39,7 @@ xyz.zhouxy.plusone plusone-exception-handler - 0.0.2-SNAPSHOT + 0.0.3-SNAPSHOT From 7d37dead60999c354d11c2f16169b809ade2c32f Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sun, 11 Dec 2022 15:16:28 +0800 Subject: [PATCH 08/18] =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=99=A8=E5=8D=87=E7=BA=A7=E5=88=B0=200.0.4-SNAPSHOT=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/handler/AllExceptionHandlerConfig.java | 3 ++- .../exception/handler/DefaultExceptionHandler.java | 3 ++- plusone-basic/plusone-basic-common/pom.xml | 2 +- .../xyz/zhouxy/plusone/constant/ErrorCodeConsts.java | 5 +++++ .../main/java/xyz/zhouxy/plusone/TestController.java | 12 ++++++++++++ .../exception/handler/SaTokenExceptionHandler.java | 3 ++- .../xyz/zhouxy/plusone/system/util/PasswordUtil.java | 3 ++- .../system/domain/model/account/Password.java | 3 ++- 8 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/constant/ErrorCodeConsts.java create mode 100644 plusone-start/src/main/java/xyz/zhouxy/plusone/TestController.java diff --git a/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/AllExceptionHandlerConfig.java b/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/AllExceptionHandlerConfig.java index fd05fdb..4848072 100644 --- a/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/AllExceptionHandlerConfig.java +++ b/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/AllExceptionHandlerConfig.java @@ -4,6 +4,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import xyz.zhouxy.plusone.constant.ErrorCodeConsts; import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler.ExceptionInfoHolder; /** @@ -15,6 +16,6 @@ public class AllExceptionHandlerConfig { @Bean AllExceptionHandler getAllExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) { - return new AllExceptionHandler(exceptionInfoHolder); + return new AllExceptionHandler(ErrorCodeConsts.DEFAULT_ERROR_CODE, exceptionInfoHolder); } } diff --git a/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/DefaultExceptionHandler.java b/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/DefaultExceptionHandler.java index af53870..263d35d 100644 --- a/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/DefaultExceptionHandler.java +++ b/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/DefaultExceptionHandler.java @@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import lombok.extern.slf4j.Slf4j; +import xyz.zhouxy.plusone.constant.ErrorCodeConsts; import xyz.zhouxy.plusone.util.RestfulResult; /** @@ -42,7 +43,7 @@ import xyz.zhouxy.plusone.util.RestfulResult; public class DefaultExceptionHandler extends BaseExceptionHandler { public DefaultExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) { - super(exceptionInfoHolder); + super(ErrorCodeConsts.DEFAULT_ERROR_CODE, exceptionInfoHolder); set(IllegalArgumentException.class, 4010000, "格式错误", HttpStatus.FORBIDDEN); set(DataAccessException.class, 6030000, "数据库错误", HttpStatus.INTERNAL_SERVER_ERROR, true); set(MethodArgumentNotValidException.class, diff --git a/plusone-basic/plusone-basic-common/pom.xml b/plusone-basic/plusone-basic-common/pom.xml index 828e5b0..d3d662d 100644 --- a/plusone-basic/plusone-basic-common/pom.xml +++ b/plusone-basic/plusone-basic-common/pom.xml @@ -39,7 +39,7 @@ xyz.zhouxy.plusone plusone-exception-handler - 0.0.3-SNAPSHOT + 0.0.4-SNAPSHOT diff --git a/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/constant/ErrorCodeConsts.java b/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/constant/ErrorCodeConsts.java new file mode 100644 index 0000000..f1958f9 --- /dev/null +++ b/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/constant/ErrorCodeConsts.java @@ -0,0 +1,5 @@ +package xyz.zhouxy.plusone.constant; + +public class ErrorCodeConsts { + public static final int DEFAULT_ERROR_CODE = 9999999; +} diff --git a/plusone-start/src/main/java/xyz/zhouxy/plusone/TestController.java b/plusone-start/src/main/java/xyz/zhouxy/plusone/TestController.java new file mode 100644 index 0000000..4e1ea42 --- /dev/null +++ b/plusone-start/src/main/java/xyz/zhouxy/plusone/TestController.java @@ -0,0 +1,12 @@ +package xyz.zhouxy.plusone; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class TestController { + @RequestMapping("/test") + public String test() throws Exception { + throw new Exception(); + } +} diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/exception/handler/SaTokenExceptionHandler.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/exception/handler/SaTokenExceptionHandler.java index 79d91bf..b83cafb 100644 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/exception/handler/SaTokenExceptionHandler.java +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/exception/handler/SaTokenExceptionHandler.java @@ -14,6 +14,7 @@ import cn.dev33.satoken.exception.NotRoleException; import cn.dev33.satoken.exception.NotSafeException; import cn.dev33.satoken.exception.SaTokenException; import lombok.extern.slf4j.Slf4j; +import xyz.zhouxy.plusone.constant.ErrorCodeConsts; import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler; import xyz.zhouxy.plusone.util.RestfulResult; @@ -27,7 +28,7 @@ import xyz.zhouxy.plusone.util.RestfulResult; public class SaTokenExceptionHandler extends BaseExceptionHandler { public SaTokenExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) { - super(exceptionInfoHolder); + super(ErrorCodeConsts.DEFAULT_ERROR_CODE, exceptionInfoHolder); set(NotPermissionException.class, 4030103, "会话未能通过权限认证", HttpStatus.FORBIDDEN); set(NotRoleException.class, 4030103, "会话未能通过角色认证", HttpStatus.FORBIDDEN); set(DisableServiceException.class, 4030202, "账号指定服务已被封禁", HttpStatus.FORBIDDEN); diff --git a/plusone-system/plusone-system-common/src/main/java/xyz/zhouxy/plusone/system/util/PasswordUtil.java b/plusone-system/plusone-system-common/src/main/java/xyz/zhouxy/plusone/system/util/PasswordUtil.java index c0ac1bb..38d95b8 100644 --- a/plusone-system/plusone-system-common/src/main/java/xyz/zhouxy/plusone/system/util/PasswordUtil.java +++ b/plusone-system/plusone-system-common/src/main/java/xyz/zhouxy/plusone/system/util/PasswordUtil.java @@ -4,6 +4,7 @@ import javax.annotation.Nonnull; import cn.hutool.core.util.RandomUtil; import cn.hutool.crypto.digest.DigestUtil; +import xyz.zhouxy.plusone.constant.ErrorCodeConsts; import xyz.zhouxy.plusone.exception.PlusoneException; /** @@ -30,7 +31,7 @@ public final class PasswordUtil { + salt.substring(1); String sha512Hex = DigestUtil.sha512Hex(passwordWithSalt); if (sha512Hex == null) { - throw new PlusoneException(9999999, "未知错误:哈希加密失败!"); + throw new PlusoneException(ErrorCodeConsts.DEFAULT_ERROR_CODE, "未知错误:哈希加密失败!"); } return sha512Hex; } diff --git a/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/Password.java b/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/Password.java index 5388195..596e5a7 100644 --- a/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/Password.java +++ b/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/Password.java @@ -7,6 +7,7 @@ import javax.annotation.Nonnull; import org.springframework.util.Assert; +import xyz.zhouxy.plusone.constant.ErrorCodeConsts; import xyz.zhouxy.plusone.constant.RegexConsts; import xyz.zhouxy.plusone.domain.IValueObject; import xyz.zhouxy.plusone.exception.PlusoneException; @@ -36,7 +37,7 @@ public class Password implements IValueObject { } var salt = PasswordUtil.generateRandomSalt(); if (salt == null) { - throw new PlusoneException(9999999, "未知错误:生成随机盐失败"); + throw new PlusoneException(ErrorCodeConsts.DEFAULT_ERROR_CODE, "未知错误:生成随机盐失败"); } this.saltVal = salt; this.passwordVal = PasswordUtil.hashPassword(password, salt); From a11cac238dc2e1be6e6549d6e630f5d55fff41b1 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Mon, 12 Dec 2022 01:44:27 +0800 Subject: [PATCH 09/18] =?UTF-8?q?=E6=8A=9B=E5=87=BA=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/xyz/zhouxy/plusone/sms/TencentSmsServiceImpl.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/sms/TencentSmsServiceImpl.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/sms/TencentSmsServiceImpl.java index 2b79f97..f5e265f 100644 --- a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/sms/TencentSmsServiceImpl.java +++ b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/sms/TencentSmsServiceImpl.java @@ -9,6 +9,8 @@ import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest; import org.springframework.stereotype.Service; import lombok.extern.slf4j.Slf4j; +import xyz.zhouxy.plusone.constant.ErrorCodeConsts; +import xyz.zhouxy.plusone.exception.PlusoneException; /** * 使用腾讯 SMS 服务 @@ -63,6 +65,7 @@ public class TencentSmsServiceImpl implements SmsService { } catch (TencentCloudSDKException e) { log.error(e.getMessage(), e); + throw new PlusoneException(ErrorCodeConsts.DEFAULT_ERROR_CODE, e); } } From c69aaf7ba1fd8c877d6b290f3ba6272d7e4128c8 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Mon, 12 Dec 2022 02:09:46 +0800 Subject: [PATCH 10/18] Revert "update file." This reverts commit 509e195fee58b8b678191c2e5ba329299576a858. --- .../src/main/resources/META-INF/spring-devtools.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plusone-start/src/main/resources/META-INF/spring-devtools.properties b/plusone-start/src/main/resources/META-INF/spring-devtools.properties index 56aa70e..d587606 100644 --- a/plusone-start/src/main/resources/META-INF/spring-devtools.properties +++ b/plusone-start/src/main/resources/META-INF/spring-devtools.properties @@ -1 +1 @@ -restart.include.projectcommon=/[\\w\\.\\-]+\\.jar +restart.include.projectcommon=/plusone-[\\w\\.\\-]+\\.jar From fc4725d9323f56c415d34ca6daf0f43184899799 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Mon, 12 Dec 2022 04:00:05 +0800 Subject: [PATCH 11/18] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=A4=9A=E4=BD=99?= =?UTF-8?q?=E7=9A=84=E6=B3=A8=E8=A7=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhouxy/plusone/system/domain/model/account/Account.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/Account.java b/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/Account.java index e19ab3a..2946951 100644 --- a/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/Account.java +++ b/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/Account.java @@ -5,8 +5,6 @@ import java.util.HashSet; import java.util.Optional; import java.util.Set; -import javax.annotation.Nonnull; - import lombok.Getter; import lombok.Setter; import lombok.ToString; @@ -115,7 +113,7 @@ public class Account extends AggregateRoot implements IWithVersion { addDomainEvent(new AccountRolesBound(this)); } - public boolean checkPassword(@Nonnull String password) { + public boolean checkPassword(String password) { return this.password.check(password); } From 263777aa70ae34b603485cc9358933c70cbc5be9 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Mon, 12 Dec 2022 04:01:00 +0800 Subject: [PATCH 12/18] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E8=85=BE=E8=AE=AF?= =?UTF-8?q?=E4=BA=91=E7=9A=84=E8=A7=84=E5=AE=9A=EF=BC=8C=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E7=A0=81=E5=8F=AA=E8=83=BD=E6=98=AF=E7=BA=AF=E6=95=B0=E5=AD=97?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/xyz/zhouxy/plusone/sms/TencentSmsServiceImpl.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/sms/TencentSmsServiceImpl.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/sms/TencentSmsServiceImpl.java index f5e265f..7b7d319 100644 --- a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/sms/TencentSmsServiceImpl.java +++ b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/sms/TencentSmsServiceImpl.java @@ -6,6 +6,8 @@ import com.tencentcloudapi.common.profile.ClientProfile; import com.tencentcloudapi.common.profile.HttpProfile; import com.tencentcloudapi.sms.v20210111.SmsClient; import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest; +import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse; + import org.springframework.stereotype.Service; import lombok.extern.slf4j.Slf4j; @@ -54,11 +56,10 @@ public class TencentSmsServiceImpl implements SmsService { * 通过 client 对象调用 SendSms 方法发起请求。注意请求方法名与请求对象是对应的 返回的 res 是一个 * SendSmsResponse 类的实例,与请求对象对应 */ - // var res = client.SendSms(req); - client.SendSms(req); + var res = client.SendSms(req); // 输出json格式的字符串回包 - // System.out.println(SendSmsResponse.toJsonString(res)); + System.out.println(SendSmsResponse.toJsonString(res)); // 也可以取出单个值,你可以通过官网接口文档或跳转到response对象的定义处查看返回字段的定义 // System.out.println(res.getRequestId()); From 80530edf6b22345bc71c0ad0f357ba8e6cf57bea Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Mon, 12 Dec 2022 04:01:24 +0800 Subject: [PATCH 13/18] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=AF=86=E7=A0=81=E6=8E=A5=E5=8F=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AccountContextController.java | 15 +++++ .../service/AccountContextService.java | 57 ++++++++++++++++--- .../service/MailAndSmsVerifyService.java | 2 +- .../command/ChangePasswordCommand.java | 11 ++++ .../ChangePasswordWithoutLoginCommand.java | 12 ++++ 5 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/ChangePasswordCommand.java create mode 100644 plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/ChangePasswordWithoutLoginCommand.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 b1aa866..b221879 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 @@ -3,10 +3,13 @@ package xyz.zhouxy.plusone.system.application.controller; import static xyz.zhouxy.plusone.system.constant.AuthLogic.adminAuthLogic; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import xyz.zhouxy.plusone.system.application.service.AccountContextService; +import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordCommand; +import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordWithoutLoginCommand; import xyz.zhouxy.plusone.util.RestfulResult; /** @@ -37,4 +40,16 @@ public class AccountContextController { var result = service.getMenuTree(); return RestfulResult.success("查询成功", result); } + + @PostMapping("changePassword") + public RestfulResult changePassword(ChangePasswordCommand command) { + service.changePassword(command); + return RestfulResult.success("修改成功,请重新登录。"); + } + + @PostMapping("changePasswordWithoutLogin") + public RestfulResult changePasswordWithoutLogin(ChangePasswordWithoutLoginCommand command) { + service.changePasswordWithoutLogin(command); + 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 e6e8996..801e0b3 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 @@ -1,14 +1,26 @@ package xyz.zhouxy.plusone.system.application.service; -import static xyz.zhouxy.plusone.system.constant.AuthLogic.adminAuthLogic; +import xyz.zhouxy.plusone.system.constant.AuthLogic; import java.util.List; -import org.springframework.stereotype.Service; +import javax.annotation.Resource; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import cn.dev33.satoken.stp.StpLogic; +import xyz.zhouxy.plusone.system.application.common.util.PrincipalUtil; 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.ChangePasswordCommand; +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.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; /** * 账号查询本身相关信息 @@ -18,21 +30,50 @@ import xyz.zhouxy.plusone.system.application.query.result.MenuViewObject; @Service public class AccountContextService { - private final AccountQueries accountQueries; - private final MenuManagementService menuManagementService; + private final static StpLogic adminAuthLogic = AuthLogic.adminAuthLogic; - public AccountContextService(AccountQueries accountQueries, MenuManagementService menuManagementService) { - this.accountQueries = accountQueries; - this.menuManagementService = menuManagementService; - } + @Resource + private AccountQueries accountQueries; + @Resource + private MenuManagementService menuManagementService; + @Resource + private AccountRepository accountRepository; + @Resource + private MailAndSmsVerifyService mailAndSmsVerifyService; public AccountDetails getAccountInfo() { + adminAuthLogic.checkLogin(); long accountId = adminAuthLogic.getLoginIdAsLong(); return accountQueries.queryAccountDetails(accountId); } public List getMenuTree() { + adminAuthLogic.checkLogin(); long accountId = adminAuthLogic.getLoginIdAsLong(); return menuManagementService.queryByAccountId(accountId); } + + @Transactional + public void changePassword(ChangePasswordCommand command) { + adminAuthLogic.checkLogin(); + Account account = accountRepository.find(adminAuthLogic.getLoginIdAsLong()); + account.checkPassword(command.getPassword()); + account.changePassword(command.getNewPassword(), command.getPasswordConfirmation()); + accountRepository.save(account); + adminAuthLogic.logout(); + } + + @Transactional + public void changePasswordWithoutLogin(ChangePasswordWithoutLoginCommand command) { + String principal = command.getAccount(); + Principal emailOrMobilePhone = PrincipalUtil.getEmailOrMobilePhone(principal); + + Account account = emailOrMobilePhone instanceof Email + ? accountRepository.findByEmail((Email) emailOrMobilePhone) + : accountRepository.findByMobilePhone((MobilePhone) emailOrMobilePhone); + account.checkPassword(command.getOldPassword()); + account.changePassword(command.getNewPassword(), command.getPasswordConfirmation()); + accountRepository.save(account); + adminAuthLogic.logout(); + } } diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/MailAndSmsVerifyService.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/MailAndSmsVerifyService.java index 0439fcb..03611e5 100644 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/MailAndSmsVerifyService.java +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/MailAndSmsVerifyService.java @@ -107,7 +107,7 @@ public class MailAndSmsVerifyService { } private static String generateCode() { - return RandomUtil.randomString(CODE_LENGTH); + return RandomUtil.randomString(RandomUtil.BASE_NUMBER, CODE_LENGTH); } } diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/ChangePasswordCommand.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/ChangePasswordCommand.java new file mode 100644 index 0000000..5b33d97 --- /dev/null +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/ChangePasswordCommand.java @@ -0,0 +1,11 @@ +package xyz.zhouxy.plusone.system.application.service.command; + +import lombok.Data; +import xyz.zhouxy.plusone.domain.ICommand; + +@Data +public class ChangePasswordCommand implements ICommand { + private String password; + private String newPassword; + private String passwordConfirmation; +} diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/ChangePasswordWithoutLoginCommand.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/ChangePasswordWithoutLoginCommand.java new file mode 100644 index 0000000..d821819 --- /dev/null +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/command/ChangePasswordWithoutLoginCommand.java @@ -0,0 +1,12 @@ +package xyz.zhouxy.plusone.system.application.service.command; + +import lombok.Data; +import xyz.zhouxy.plusone.domain.ICommand; + +@Data +public class ChangePasswordWithoutLoginCommand implements ICommand { + private String account; + private String oldPassword; + private String newPassword; + private String passwordConfirmation; +} From 6f4503684ffc972a0e215ad32077f5445ae5f195 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 14 Dec 2022 13:41:37 +0800 Subject: [PATCH 14/18] =?UTF-8?q?=E6=A0=A1=E9=AA=8C=E5=99=A8=E6=8F=90?= =?UTF-8?q?=E5=8F=96=E4=B8=BA=E5=8D=95=E7=8B=AC=E7=9A=84=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plusone-basic/plusone-basic-common/pom.xml | 5 + .../exception/InvalidInputException.java | 56 --- .../plusone/validator/BaseValidator.java | 88 ---- .../plusone/validator/IValidateRequired.java | 13 - .../plusone/validator/ValidateUtil.java | 29 -- .../zhouxy/plusone/validator/Validator.java | 42 -- .../validator/validator2/BaseValidator2.java | 22 - .../validator/validator2/ValueValidator.java | 379 ------------------ .../common/util/PrincipalUtil.java | 2 +- .../UnsupportedMenuTypeException.java | 2 +- 10 files changed, 7 insertions(+), 631 deletions(-) delete mode 100644 plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/exception/InvalidInputException.java delete mode 100644 plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java delete mode 100644 plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/IValidateRequired.java delete mode 100644 plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/ValidateUtil.java delete mode 100644 plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/Validator.java delete mode 100644 plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/validator2/BaseValidator2.java delete mode 100644 plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/validator2/ValueValidator.java diff --git a/plusone-basic/plusone-basic-common/pom.xml b/plusone-basic/plusone-basic-common/pom.xml index d3d662d..8864cb3 100644 --- a/plusone-basic/plusone-basic-common/pom.xml +++ b/plusone-basic/plusone-basic-common/pom.xml @@ -36,6 +36,11 @@ plusone-commons 0.0.1-SNAPSHOT + + xyz.zhouxy.plusone + plusone-validator + 0.0.1-SNAPSHOT + xyz.zhouxy.plusone plusone-exception-handler diff --git a/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/exception/InvalidInputException.java b/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/exception/InvalidInputException.java deleted file mode 100644 index f2a3be9..0000000 --- a/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/exception/InvalidInputException.java +++ /dev/null @@ -1,56 +0,0 @@ -package xyz.zhouxy.plusone.exception; - -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.ResponseStatus; - -/** - * 4040200 - 无效的用户输入 - * - * @author ZhouXY - */ -@ResponseStatus(HttpStatus.BAD_REQUEST) -public class InvalidInputException extends PlusoneException { - - @java.io.Serial - private static final long serialVersionUID = 7956661913360059670L; - - public static final int ERROR_CODE = 4040200; - - private InvalidInputException(int code, String msg) { - super(code, msg); - } - - private InvalidInputException(int code, Throwable cause) { - super(code, cause); - } - - private InvalidInputException(int code, String msg, Throwable cause) { - super(code, msg, cause); - } - - public InvalidInputException(String msg) { - this(ERROR_CODE, msg); - } - - public InvalidInputException(Throwable cause) { - this(ERROR_CODE, cause); - } - - public InvalidInputException(String msg, Throwable cause) { - this(ERROR_CODE, msg, cause); - } - - /** - * 不支持的 Principal 类型出现时抛出的异常 - */ - public static InvalidInputException unsupportedPrincipalTypeException() { - return unsupportedPrincipalTypeException("不支持的 PrincipalType"); - } - - /** - * 不支持的 Principal 类型出现时抛出的异常 - */ - public static InvalidInputException unsupportedPrincipalTypeException(String message) { - return new InvalidInputException(4040201, message); - } -} diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java deleted file mode 100644 index 7a7d9a8..0000000 --- a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java +++ /dev/null @@ -1,88 +0,0 @@ -package xyz.zhouxy.plusone.validator; - -import java.util.ArrayList; -import java.util.List; -import java.util.function.Function; -import java.util.function.Predicate; -import java.util.function.Supplier; - -import xyz.zhouxy.plusone.exception.InvalidInputException; - -/** - * 校验器 - * - *

- * 可以使用以下方式初始化一个校验器: - *

- * - *
- * BaseValidator<Integer> validator = new BaseValidator<>() {
- *     {
- *         withRule(value -> Objects.nonNull(value), "value 不能为空");
- *         withRule(value -> (value >= 0 && value <= 500), "value 应在 [0, 500] 内");
- *     }
- * };
- * 
- * - *

- * 也可以通过继承本类,定义一个校验器(可使用单例模式)。 - *

- * - *

- * 然后通过校验器的 {@link #validate} 方法,或 - * {@link ValidateUtil#validate(Object, Validator)} 对指定对象进行校验。 - *

- * - *
- * ValidateUtil.validate(255, validator);
- * 
- * - *
- * validator.validate(666);
- * 
- *

- * - * @author ZhouXY - * @see IValidateRequired - * @see ValidateUtil - * @see Validator - */ -public abstract class BaseValidator { - - private final List> rules = new ArrayList<>(); - - protected BaseValidator() { - } - - protected final void withRule(Predicate rule, String errorMessage) { - withRule(rule, () -> new InvalidInputException(errorMessage)); - } - - protected final void withRule(Predicate rule, Supplier exceptionCreator) { - withRule(rule, value -> exceptionCreator.get()); - } - - protected final void withRule(Predicate rule, Function exceptionCreator) { - this.rules.add(new RuleInfo<>(rule, exceptionCreator)); - } - - public void validate(T obj) { - this.rules.forEach(ruleInfo -> ruleInfo.validate(obj)); - } - - protected static class RuleInfo { - private final Predicate rule; - private final Function exceptionCreator; - - private RuleInfo(Predicate rule, Function exceptionCreator) { - this.rule = rule; - this.exceptionCreator = exceptionCreator; - } - - private void validate(T obj) { - if (!rule.test(obj)) { - throw exceptionCreator.apply(obj); - } - } - } -} diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/IValidateRequired.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/IValidateRequired.java deleted file mode 100644 index 82e98ae..0000000 --- a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/IValidateRequired.java +++ /dev/null @@ -1,13 +0,0 @@ -package xyz.zhouxy.plusone.validator; - -/** - * 自带校验方法,校验不通过时直接抛异常。 - * - * @author ZhouXY - * - * @see ValidateUtil - * @see BaseValidator - */ -public interface IValidateRequired { - void validate(); -} diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/ValidateUtil.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/ValidateUtil.java deleted file mode 100644 index 1a8438d..0000000 --- a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/ValidateUtil.java +++ /dev/null @@ -1,29 +0,0 @@ -package xyz.zhouxy.plusone.validator; - -/** - * 校验工具类 - *

- * 对 {@link IValidateRequired} 的实现类对象进行校验 - *

- * - * @author ZhouXY - * - * @see BaseValidator - * @see Validator - * @see IValidateRequired - */ -public class ValidateUtil { - private ValidateUtil() { - throw new IllegalStateException("Utility class"); - } - - public static void validate(Object obj) { - if (obj instanceof IValidateRequired) { - ((IValidateRequired) obj).validate(); - } - } - - public static void validate(T obj, BaseValidator validator) { - validator.validate(obj); - } -} diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/Validator.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/Validator.java deleted file mode 100644 index b56ff50..0000000 --- a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/Validator.java +++ /dev/null @@ -1,42 +0,0 @@ -package xyz.zhouxy.plusone.validator; - -import java.util.function.Predicate; - -/** - * 校验器 - * - *

- * 可以使用以下方式初始化一个校验器: - *

- * - *
- * var validator = new Validator<Integer>()
- *         .addRule(value -> Objects.nonNull(value), "value 不能为空")
- *         .addRule(value -> (value >= 0 && value <= 500), "value 应在 [0, 500] 内");
- * 
- * - *

- * 然后通过校验器的 {@link #validate} 方法,或 - * {@link ValidateUtil#validate(Object, Validator)} 对指定对象进行校验。 - *

- * - *
- * validator.validate(666);
- * 
- * - *
- * ValidateUtil.validate(255, validator);
- * 
- *

- * - * @author ZhouXY - * @see IValidateRequired - * @see ValidateUtil - * @see BaseValidator - */ -public final class Validator extends BaseValidator { - public final Validator addRule(final Predicate rule, final String errorMessage) { - withRule(rule, errorMessage); - return this; - } -} diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/validator2/BaseValidator2.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/validator2/BaseValidator2.java deleted file mode 100644 index 257af16..0000000 --- a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/validator2/BaseValidator2.java +++ /dev/null @@ -1,22 +0,0 @@ -package xyz.zhouxy.plusone.validator.validator2; - -import java.util.ArrayList; -import java.util.List; -import java.util.function.Function; - -public abstract class BaseValidator2 { - - private List> valueValidators = new ArrayList<>(); - - protected final ValueValidator ruleFor(Function getter) { - ValueValidator validValueHolder = new ValueValidator<>(getter); - valueValidators.add(validValueHolder); - return validValueHolder; - } - - public void validate(T obj) { - for (ValueValidator valueValidator : this.valueValidators) { - valueValidator.validateProperty(obj); - } - } -} diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/validator2/ValueValidator.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/validator2/ValueValidator.java deleted file mode 100644 index 933db5c..0000000 --- a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/validator2/ValueValidator.java +++ /dev/null @@ -1,379 +0,0 @@ -package xyz.zhouxy.plusone.validator.validator2; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Objects; -import java.util.function.Consumer; -import java.util.function.Function; -import java.util.function.Predicate; -import java.util.function.Supplier; - -import cn.hutool.core.util.StrUtil; -import xyz.zhouxy.plusone.constant.RegexConsts; -import xyz.zhouxy.plusone.exception.InvalidInputException; -import xyz.zhouxy.plusone.util.RegexUtil; - -public class ValueValidator { - Function getter; - List> rules = new ArrayList<>(); - - public ValueValidator(Function getter) { - this.getter = getter; - } - - private void withRule(Predicate condition, - Function exceptionCreator) { - withRule(value -> { - if (!condition.test(value)) { - throw exceptionCreator.apply(value); - } - }); - } - - private void withRule(Consumer rule) { - this.rules.add(rule); - } - - // ==================== - // ====== Object ====== - // ==================== - - // ====== notNull ===== - - public ValueValidator notNull() { - return notNull("Value could not be null."); - } - - public ValueValidator notNull(String errMsg) { - return notNull(convertExceptionCreator(errMsg)); - } - - public ValueValidator notNull(Supplier exceptionCreator) { - return notNull(convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator notNull(Function exceptionCreator) { - withRule(Objects::nonNull, exceptionCreator); - return this; - } - - // ====== isNull ===== - - public ValueValidator isNull(String errMsg) { - return isNull(convertExceptionCreator(errMsg)); - } - - public ValueValidator isNull(Supplier exceptionCreator) { - return isNull(convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator isNull(Function exceptionCreator) { - withRule(Objects::isNull, exceptionCreator); - return this; - } - - // ===== equals ===== - - public ValueValidator equalsThat(Object that) { - return equalsThat(that, value -> new InvalidInputException(String.format("(%s) 必须与 (%s) 相等", value, that))); - } - - public ValueValidator equalsThat(Object that, String errMsg) { - return equalsThat(that, convertExceptionCreator(errMsg)); - } - - public ValueValidator equalsThat( - Object that, Supplier exceptionCreator) { - return equalsThat(that, convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator equalsThat( - Object that, Function exceptionCreator) { - withRule(value -> Objects.equals(value, that), exceptionCreator); - return this; - } - - // ===== state ===== - - public ValueValidator state(Predicate condition) { - return state(condition, "无效的用户输入"); - } - - public ValueValidator state(Predicate condition, String errMsg) { - return state(condition, convertExceptionCreator(errMsg)); - } - - public ValueValidator state( - Predicate condition, - Supplier exceptionCreator) { - return state(condition, convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator state( - Predicate condition, - Function exceptionCreator) { - withRule(condition, exceptionCreator); - return this; - } - - // ================= - // ====== int ====== - // ================= - - public ValueValidator between(int min, int max) { - return between(min, max, String.format("数值不在 %s 和 %s 之间", String.valueOf(min), String.valueOf(max))); - } - - public ValueValidator between(int min, int max, String errMsg) { - return between(min, max, convertExceptionCreator(errMsg)); - } - - public ValueValidator between(int min, int max, - Supplier exceptionCreator) { - return between(min, max, convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator between(int min, int max, - Function exceptionCreator) { - withRule(value -> ((int) value >= min && (int) value < max), exceptionCreator); - return this; - } - - // ==================== - // ====== double ====== - // ==================== - - public ValueValidator between(double min, double max) { - return between(min, max, String.format("数值不在 %s 和 %s 之间", String.valueOf(min), String.valueOf(max))); - } - - public ValueValidator between(double min, double max, String errMsg) { - return between(min, max, convertExceptionCreator(errMsg)); - } - - public ValueValidator between(double min, double max, - Supplier exceptionCreator) { - return between(min, max, convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator between(double min, double max, - Function exceptionCreator) { - withRule(value -> ((double) value >= min && (double) value < max), exceptionCreator); - return this; - } - - // ================================ - // ====== Collection, String ====== - // ================================ - - // ====== notEmpty ===== - - public ValueValidator notEmpty(String errMsg) { - return notEmpty(convertExceptionCreator(errMsg)); - } - - public ValueValidator notEmpty(Supplier exceptionCreator) { - return notEmpty(convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator notEmpty(Function exceptionCreator) { - withRule(value -> { - if (value == null) { - return false; - } - if (value instanceof Collection) { - return !((Collection) value).isEmpty(); - } - if (value instanceof String) { - return !((String) value).isEmpty(); - } - return false; - }, exceptionCreator); - return this; - } - - // ====== isEmpty ===== - - public ValueValidator isEmpty(String errMsg) { - return isEmpty(convertExceptionCreator(errMsg)); - } - - public ValueValidator isEmpty(Supplier exceptionCreator) { - return isEmpty(convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator isEmpty(Function exceptionCreator) { - withRule(value -> { - if (value == null) { - return false; - } - if (value instanceof Collection) { - return ((Collection) value).isEmpty(); - } - if (value instanceof String) { - return ((String) value).isEmpty(); - } - return false; - }, exceptionCreator); - return this; - } - - // ===================== - // ====== boolean ====== - // ===================== - - // ====== isTrue ====== - - public ValueValidator isTrue() { - return isTrue("The value must be true."); - } - - public ValueValidator isTrue(String errMsg) { - return isTrue(convertExceptionCreator(errMsg)); - } - - public ValueValidator isTrue(Supplier exceptionCreator) { - return isTrue(convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator isTrue(Function exceptionCreator) { - withRule(Boolean.TRUE::equals, exceptionCreator); - return this; - } - - // ====== isFalse ====== - - public ValueValidator isFalse() { - return isFalse("The value must be false."); - } - - public ValueValidator isFalse(String errMsg) { - return isFalse(convertExceptionCreator(errMsg)); - } - - public ValueValidator isFalse(Supplier exceptionCreator) { - return isFalse(convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator isFalse(Function exceptionCreator) { - withRule(Boolean.FALSE::equals, exceptionCreator); - return this; - } - - // ==================== - // ====== String ====== - // ==================== - - // ===== matches ===== - - public ValueValidator matches(String regex, String errMsg) { - return matches(regex, convertExceptionCreator(errMsg)); - } - - public ValueValidator matches( - String regex, - Supplier exceptionCreator) { - return matches(regex, convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator matches( - String regex, - Function exceptionCreator) { - withRule(input -> RegexUtil.matches((String) input, regex), exceptionCreator); - return this; - } - - // ===== matchesOr ===== - - public ValueValidator matchesOr(String[] regexs, String errMsg) { - return matchesOr(regexs, convertExceptionCreator(errMsg)); - } - - public ValueValidator matchesOr( - String[] regexs, - Supplier exceptionCreator) { - return matchesOr(regexs, convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator matchesOr( - String[] regexs, - Function exceptionCreator) { - withRule(input -> RegexUtil.matchesOr((String) input, regexs), exceptionCreator); - return this; - } - - // ===== matchesAnd ===== - - public ValueValidator matchesAnd(String[] regexs, String errMsg) { - return matchesAnd(regexs, convertExceptionCreator(errMsg)); - } - - public ValueValidator matchesAnd( - String[] regexs, - Supplier exceptionCreator) { - return matchesAnd(regexs, convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator matchesAnd( - String[] regexs, - Function exceptionCreator) { - withRule(input -> RegexUtil.matchesAnd((String) input, regexs), exceptionCreator); - return this; - } - - // ===== notBlank ===== - - public ValueValidator notBlank() { - return notBlank("This String argument must have text; it must not be null, empty, or blank"); - } - - public ValueValidator notBlank(String errMsg) { - return notBlank(convertExceptionCreator(errMsg)); - } - - public ValueValidator notBlank(Supplier exceptionCreator) { - return notBlank(convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator notBlank(Function exceptionCreator) { - withRule(input -> StrUtil.isNotBlank((String) input), exceptionCreator); - return this; - } - - // ===== email ===== - - public ValueValidator email() { - return email("The value is not an email address."); - } - - public ValueValidator email(String errMsg) { - return email(convertExceptionCreator(errMsg)); - } - - public ValueValidator email(Supplier exceptionCreator) { - return email(convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator email(Function exceptionCreator) { - return matches(RegexConsts.EMAIL, exceptionCreator); - } - - // ======================================================================== - - void validateProperty(DTO obj) { - PROPERTY value = this.getter.apply(obj); - for (var rule : this.rules) { - rule.accept(value); - } - } - - private static Function convertExceptionCreator(String errMsg) { - return convertExceptionCreator(errMsg); - } - - private static Function convertExceptionCreator( - Supplier exceptionSupplier) { - return value -> exceptionSupplier.get(); - } -} diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/common/util/PrincipalUtil.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/common/util/PrincipalUtil.java index 6e820f7..da05d59 100644 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/common/util/PrincipalUtil.java +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/common/util/PrincipalUtil.java @@ -2,11 +2,11 @@ package xyz.zhouxy.plusone.system.application.common.util; import javax.annotation.Nullable; -import xyz.zhouxy.plusone.exception.InvalidInputException; 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; /** * 根据字面值,判断并生成 {@link Principal} 值对象。 diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/exception/UnsupportedMenuTypeException.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/exception/UnsupportedMenuTypeException.java index 993d936..429246b 100644 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/exception/UnsupportedMenuTypeException.java +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/exception/UnsupportedMenuTypeException.java @@ -1,6 +1,6 @@ package xyz.zhouxy.plusone.system.application.exception; -import xyz.zhouxy.plusone.exception.InvalidInputException; +import xyz.zhouxy.plusone.validator.InvalidInputException; public class UnsupportedMenuTypeException extends InvalidInputException { From 71438901a0c71ef36611d5315af3a1b62822dbf2 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 14 Dec 2022 13:41:57 +0800 Subject: [PATCH 15/18] =?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 From d398800ce4f2f4d89ee58ccf995bae928f1326de Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 14 Dec 2022 14:30:54 +0800 Subject: [PATCH 16/18] =?UTF-8?q?=E9=87=8D=E6=9E=84=EF=BC=8C=E5=8D=87?= =?UTF-8?q?=E7=BA=A7=E5=BC=82=E5=B8=B8=E6=8B=A6=E6=88=AA=E5=99=A8=E4=B8=BA?= =?UTF-8?q?=200.0.5-SNAPSHOT=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/PlusoneExceptionHandlerConfig.java | 3 ++- .../handler/AllExceptionHandlerConfig.java | 3 +-- .../handler/DefaultExceptionHandler.java | 3 +-- plusone-basic/plusone-basic-common/pom.xml | 2 +- .../validator/InvalidInputExceptionHandler.java | 15 +++++++++++++++ .../handler/SaTokenExceptionHandler.java | 5 ++--- 6 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputExceptionHandler.java diff --git a/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/config/PlusoneExceptionHandlerConfig.java b/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/config/PlusoneExceptionHandlerConfig.java index 7a06b84..fd871aa 100644 --- a/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/config/PlusoneExceptionHandlerConfig.java +++ b/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/config/PlusoneExceptionHandlerConfig.java @@ -4,6 +4,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import xyz.zhouxy.plusone.constant.ErrorCodeConsts; import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler.ExceptionInfoHolder; @Configuration @@ -12,6 +13,6 @@ public class PlusoneExceptionHandlerConfig { @Bean @ConditionalOnMissingBean ExceptionInfoHolder exceptionInfoHolder() { - return new ExceptionInfoHolder(); + return new ExceptionInfoHolder(ErrorCodeConsts.DEFAULT_ERROR_CODE); } } diff --git a/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/AllExceptionHandlerConfig.java b/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/AllExceptionHandlerConfig.java index 4848072..fd05fdb 100644 --- a/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/AllExceptionHandlerConfig.java +++ b/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/AllExceptionHandlerConfig.java @@ -4,7 +4,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import xyz.zhouxy.plusone.constant.ErrorCodeConsts; import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler.ExceptionInfoHolder; /** @@ -16,6 +15,6 @@ public class AllExceptionHandlerConfig { @Bean AllExceptionHandler getAllExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) { - return new AllExceptionHandler(ErrorCodeConsts.DEFAULT_ERROR_CODE, exceptionInfoHolder); + return new AllExceptionHandler(exceptionInfoHolder); } } diff --git a/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/DefaultExceptionHandler.java b/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/DefaultExceptionHandler.java index 263d35d..af53870 100644 --- a/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/DefaultExceptionHandler.java +++ b/plusone-basic/plusone-basic-application/src/main/java/xyz/zhouxy/plusone/exception/handler/DefaultExceptionHandler.java @@ -11,7 +11,6 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import lombok.extern.slf4j.Slf4j; -import xyz.zhouxy.plusone.constant.ErrorCodeConsts; import xyz.zhouxy.plusone.util.RestfulResult; /** @@ -43,7 +42,7 @@ import xyz.zhouxy.plusone.util.RestfulResult; public class DefaultExceptionHandler extends BaseExceptionHandler { public DefaultExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) { - super(ErrorCodeConsts.DEFAULT_ERROR_CODE, exceptionInfoHolder); + super(exceptionInfoHolder); set(IllegalArgumentException.class, 4010000, "格式错误", HttpStatus.FORBIDDEN); set(DataAccessException.class, 6030000, "数据库错误", HttpStatus.INTERNAL_SERVER_ERROR, true); set(MethodArgumentNotValidException.class, diff --git a/plusone-basic/plusone-basic-common/pom.xml b/plusone-basic/plusone-basic-common/pom.xml index 8864cb3..14aa305 100644 --- a/plusone-basic/plusone-basic-common/pom.xml +++ b/plusone-basic/plusone-basic-common/pom.xml @@ -44,7 +44,7 @@ xyz.zhouxy.plusone plusone-exception-handler - 0.0.4-SNAPSHOT + 0.0.5-SNAPSHOT diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputExceptionHandler.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputExceptionHandler.java new file mode 100644 index 0000000..f987fec --- /dev/null +++ b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputExceptionHandler.java @@ -0,0 +1,15 @@ +package xyz.zhouxy.plusone.validator; + +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import xyz.zhouxy.plusone.constant.ErrorCodeConsts; +import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler; + +@RestControllerAdvice +public class InvalidInputExceptionHandler extends BaseExceptionHandler { + + protected InvalidInputExceptionHandler() { + super(new ExceptionInfoHolder(ErrorCodeConsts.DEFAULT_ERROR_CODE)); + set(InvalidInputException.class, InvalidInputException.ERROR_CODE, "无效的用户输入"); + } +} diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/exception/handler/SaTokenExceptionHandler.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/exception/handler/SaTokenExceptionHandler.java index b83cafb..e216e16 100644 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/exception/handler/SaTokenExceptionHandler.java +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/exception/handler/SaTokenExceptionHandler.java @@ -6,15 +6,14 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import cn.dev33.satoken.exception.DisableServiceException; -import cn.dev33.satoken.exception.SameTokenInvalidException; import cn.dev33.satoken.exception.NotBasicAuthException; import cn.dev33.satoken.exception.NotLoginException; import cn.dev33.satoken.exception.NotPermissionException; import cn.dev33.satoken.exception.NotRoleException; import cn.dev33.satoken.exception.NotSafeException; import cn.dev33.satoken.exception.SaTokenException; +import cn.dev33.satoken.exception.SameTokenInvalidException; import lombok.extern.slf4j.Slf4j; -import xyz.zhouxy.plusone.constant.ErrorCodeConsts; import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler; import xyz.zhouxy.plusone.util.RestfulResult; @@ -28,7 +27,7 @@ import xyz.zhouxy.plusone.util.RestfulResult; public class SaTokenExceptionHandler extends BaseExceptionHandler { public SaTokenExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) { - super(ErrorCodeConsts.DEFAULT_ERROR_CODE, exceptionInfoHolder); + super(exceptionInfoHolder); set(NotPermissionException.class, 4030103, "会话未能通过权限认证", HttpStatus.FORBIDDEN); set(NotRoleException.class, 4030103, "会话未能通过角色认证", HttpStatus.FORBIDDEN); set(DisableServiceException.class, 4030202, "账号指定服务已被封禁", HttpStatus.FORBIDDEN); From ad8a6da44dd0357de07331563a96af697dc91009 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 14 Dec 2022 14:42:56 +0800 Subject: [PATCH 17/18] 2022/12/14 --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 96041e9..eb3eb56 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,6 @@ build/ ### VS Code ### .vscode/ + +### bak ### +*.bak From 46a72e63c0bc82b9df6d1a3f1870cc0240999ee9 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Thu, 15 Dec 2022 11:38:22 +0800 Subject: [PATCH 18/18] =?UTF-8?q?=E5=88=A0=E9=99=A4=20TestController?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/xyz/zhouxy/plusone/TestController.java | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 plusone-start/src/main/java/xyz/zhouxy/plusone/TestController.java diff --git a/plusone-start/src/main/java/xyz/zhouxy/plusone/TestController.java b/plusone-start/src/main/java/xyz/zhouxy/plusone/TestController.java deleted file mode 100644 index 4e1ea42..0000000 --- a/plusone-start/src/main/java/xyz/zhouxy/plusone/TestController.java +++ /dev/null @@ -1,12 +0,0 @@ -package xyz.zhouxy.plusone; - -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class TestController { - @RequestMapping("/test") - public String test() throws Exception { - throw new Exception(); - } -}