From f2a7019ec40553a549516171a4d8b1b55e1ab064 Mon Sep 17 00:00:00 2001 From: vran Date: Sat, 5 Mar 2022 13:09:03 +0800 Subject: [PATCH] feat: show error message when oauth callback error (#34) * feat: show error message when oauth callback error * fix: checkstyle --- .../DatabasirAuthenticationFailureHandler.java | 4 ++-- .../com/databasir/core/domain/DomainErrors.java | 7 ++++++- .../exception/DatabasirAuthenticationException.java | 13 +++++++++---- .../core/domain/app/handler/OpenAuthHandlers.java | 9 ++++++++- .../java/com/databasir/dao/impl/OauthAppDao.java | 4 +--- 5 files changed, 26 insertions(+), 11 deletions(-) diff --git a/api/src/main/java/com/databasir/api/config/security/DatabasirAuthenticationFailureHandler.java b/api/src/main/java/com/databasir/api/config/security/DatabasirAuthenticationFailureHandler.java index 5c291eb..e341be1 100644 --- a/api/src/main/java/com/databasir/api/config/security/DatabasirAuthenticationFailureHandler.java +++ b/api/src/main/java/com/databasir/api/config/security/DatabasirAuthenticationFailureHandler.java @@ -1,7 +1,7 @@ package com.databasir.api.config.security; -import com.databasir.core.domain.app.exception.DatabasirAuthenticationException; import com.databasir.common.JsonData; +import com.databasir.core.domain.app.exception.DatabasirAuthenticationException; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; @@ -42,7 +42,7 @@ public class DatabasirAuthenticationFailureHandler implements AuthenticationFail response.getOutputStream().write(jsonString.getBytes(StandardCharsets.UTF_8)); } else if (exception instanceof DatabasirAuthenticationException) { DatabasirAuthenticationException bizException = (DatabasirAuthenticationException) exception; - JsonData data = JsonData.error("-1", bizException.getMessage()); + JsonData data = JsonData.error(bizException.getErrCode(), bizException.getErrMessage()); String jsonString = objectMapper.writeValueAsString(data); response.setStatus(HttpStatus.OK.value()); response.getOutputStream().write(jsonString.getBytes(StandardCharsets.UTF_8)); diff --git a/core/src/main/java/com/databasir/core/domain/DomainErrors.java b/core/src/main/java/com/databasir/core/domain/DomainErrors.java index 8be823e..4141a3a 100644 --- a/core/src/main/java/com/databasir/core/domain/DomainErrors.java +++ b/core/src/main/java/com/databasir/core/domain/DomainErrors.java @@ -26,7 +26,8 @@ public enum DomainErrors implements DatabasirErrors { ORIGIN_PASSWORD_NOT_CORRECT("A_10011", "原密码不正确"), INVALID_CRON_EXPRESSION("A_10012", "不合法的 cron 表达式"), REGISTRATION_ID_DUPLICATE("A_10013", "应用注册 ID 不能重复"), - MISS_REQUIRED_PARAMETERS("A_10014", "缺少必填参数"); + REGISTRATION_ID_NOT_FOUND("A_10014", "应用 ID 不存在"), + MISS_REQUIRED_PARAMETERS("A_10015", "缺少必填参数"); private final String errCode; @@ -43,4 +44,8 @@ public enum DomainErrors implements DatabasirErrors { public DatabasirException exception(String message, Throwable origin) { return new DatabasirException(this, message, origin); } + + public DatabasirException exception(String s) { + return exception(s, (Throwable) null); + } } diff --git a/core/src/main/java/com/databasir/core/domain/app/exception/DatabasirAuthenticationException.java b/core/src/main/java/com/databasir/core/domain/app/exception/DatabasirAuthenticationException.java index 1d9986f..69b444c 100644 --- a/core/src/main/java/com/databasir/core/domain/app/exception/DatabasirAuthenticationException.java +++ b/core/src/main/java/com/databasir/core/domain/app/exception/DatabasirAuthenticationException.java @@ -1,19 +1,24 @@ package com.databasir.core.domain.app.exception; import com.databasir.common.DatabasirException; +import lombok.Getter; import org.springframework.security.core.AuthenticationException; +@Getter public class DatabasirAuthenticationException extends AuthenticationException { + private final DatabasirException databasirException; + public DatabasirAuthenticationException(DatabasirException databasirException) { super(databasirException.getErrMessage(), databasirException); + this.databasirException = databasirException; } - public DatabasirAuthenticationException(String msg) { - super(msg); + public String getErrMessage() { + return databasirException.getErrMessage(); } - public DatabasirAuthenticationException(String msg, Throwable cause) { - super(msg, cause); + public String getErrCode() { + return databasirException.getErrCode(); } } diff --git a/core/src/main/java/com/databasir/core/domain/app/handler/OpenAuthHandlers.java b/core/src/main/java/com/databasir/core/domain/app/handler/OpenAuthHandlers.java index d9df275..7d59b68 100644 --- a/core/src/main/java/com/databasir/core/domain/app/handler/OpenAuthHandlers.java +++ b/core/src/main/java/com/databasir/core/domain/app/handler/OpenAuthHandlers.java @@ -1,5 +1,6 @@ package com.databasir.core.domain.app.handler; +import com.databasir.core.domain.app.exception.DatabasirAuthenticationException; import com.databasir.dao.impl.OauthAppDao; import com.databasir.dao.tables.pojos.OauthAppPojo; import lombok.RequiredArgsConstructor; @@ -8,6 +9,8 @@ import org.springframework.stereotype.Component; import java.util.List; import java.util.Map; +import static com.databasir.core.domain.DomainErrors.REGISTRATION_ID_NOT_FOUND; + @Component @RequiredArgsConstructor public class OpenAuthHandlers { @@ -26,7 +29,11 @@ public class OpenAuthHandlers { } public OAuthProcessResult process(String registrationId, Map parameters) { - OauthAppPojo app = oauthAppDao.selectByRegistrationId(registrationId); + OauthAppPojo app = oauthAppDao.selectOptionByRegistrationId(registrationId) + .orElseThrow(() -> { + var bizErr = REGISTRATION_ID_NOT_FOUND.exception("应用 ID [" + registrationId + "] 不存在"); + return new DatabasirAuthenticationException(bizErr); + }); return handlers.stream() .filter(handler -> handler.support(app.getAppType())) .findFirst() diff --git a/dao/src/main/java/com/databasir/dao/impl/OauthAppDao.java b/dao/src/main/java/com/databasir/dao/impl/OauthAppDao.java index bd7450b..e59d206 100644 --- a/dao/src/main/java/com/databasir/dao/impl/OauthAppDao.java +++ b/dao/src/main/java/com/databasir/dao/impl/OauthAppDao.java @@ -29,9 +29,7 @@ public class OauthAppDao extends BaseDao { } public OauthAppPojo selectByRegistrationId(String registrationId) { - return this.getDslContext() - .select(OAUTH_APP.fields()).from(OAUTH_APP).where(OAUTH_APP.REGISTRATION_ID.eq(registrationId)) - .fetchOptionalInto(OauthAppPojo.class) + return this.selectOptionByRegistrationId(registrationId) .orElseThrow(() -> new DataNotExistsException("can not found oauth app by " + registrationId)); } } \ No newline at end of file