Browse Source
feat: show error message when oauth callback error (#34)
* feat: show error message when oauth callback error
* fix: checkstyle
master
vran
3 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with
26 additions and
11 deletions
-
api/src/main/java/com/databasir/api/config/security/DatabasirAuthenticationFailureHandler.java
-
core/src/main/java/com/databasir/core/domain/DomainErrors.java
-
core/src/main/java/com/databasir/core/domain/app/exception/DatabasirAuthenticationException.java
-
core/src/main/java/com/databasir/core/domain/app/handler/OpenAuthHandlers.java
-
dao/src/main/java/com/databasir/dao/impl/OauthAppDao.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<Void> data = JsonData.error("-1", bizException.getMessage()); |
|
|
|
JsonData<Void> 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)); |
|
|
|
|
|
@ -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); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
@ -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(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
@ -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<String, String[]> 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() |
|
|
|
|
|
@ -29,9 +29,7 @@ public class OauthAppDao extends BaseDao<OauthAppPojo> { |
|
|
|
} |
|
|
|
|
|
|
|
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)); |
|
|
|
} |
|
|
|
} |