feat: show error message when oauth callback error (#34)
* feat: show error message when oauth callback error * fix: checkstyle
This commit is contained in:
parent
842643ea4f
commit
f2a7019ec4
|
@ -1,7 +1,7 @@
|
||||||
package com.databasir.api.config.security;
|
package com.databasir.api.config.security;
|
||||||
|
|
||||||
import com.databasir.core.domain.app.exception.DatabasirAuthenticationException;
|
|
||||||
import com.databasir.common.JsonData;
|
import com.databasir.common.JsonData;
|
||||||
|
import com.databasir.core.domain.app.exception.DatabasirAuthenticationException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
|
@ -42,7 +42,7 @@ public class DatabasirAuthenticationFailureHandler implements AuthenticationFail
|
||||||
response.getOutputStream().write(jsonString.getBytes(StandardCharsets.UTF_8));
|
response.getOutputStream().write(jsonString.getBytes(StandardCharsets.UTF_8));
|
||||||
} else if (exception instanceof DatabasirAuthenticationException) {
|
} else if (exception instanceof DatabasirAuthenticationException) {
|
||||||
DatabasirAuthenticationException bizException = (DatabasirAuthenticationException) exception;
|
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);
|
String jsonString = objectMapper.writeValueAsString(data);
|
||||||
response.setStatus(HttpStatus.OK.value());
|
response.setStatus(HttpStatus.OK.value());
|
||||||
response.getOutputStream().write(jsonString.getBytes(StandardCharsets.UTF_8));
|
response.getOutputStream().write(jsonString.getBytes(StandardCharsets.UTF_8));
|
||||||
|
|
|
@ -26,7 +26,8 @@ public enum DomainErrors implements DatabasirErrors {
|
||||||
ORIGIN_PASSWORD_NOT_CORRECT("A_10011", "原密码不正确"),
|
ORIGIN_PASSWORD_NOT_CORRECT("A_10011", "原密码不正确"),
|
||||||
INVALID_CRON_EXPRESSION("A_10012", "不合法的 cron 表达式"),
|
INVALID_CRON_EXPRESSION("A_10012", "不合法的 cron 表达式"),
|
||||||
REGISTRATION_ID_DUPLICATE("A_10013", "应用注册 ID 不能重复"),
|
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;
|
private final String errCode;
|
||||||
|
|
||||||
|
@ -43,4 +44,8 @@ public enum DomainErrors implements DatabasirErrors {
|
||||||
public DatabasirException exception(String message, Throwable origin) {
|
public DatabasirException exception(String message, Throwable origin) {
|
||||||
return new DatabasirException(this, message, 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;
|
package com.databasir.core.domain.app.exception;
|
||||||
|
|
||||||
import com.databasir.common.DatabasirException;
|
import com.databasir.common.DatabasirException;
|
||||||
|
import lombok.Getter;
|
||||||
import org.springframework.security.core.AuthenticationException;
|
import org.springframework.security.core.AuthenticationException;
|
||||||
|
|
||||||
|
@Getter
|
||||||
public class DatabasirAuthenticationException extends AuthenticationException {
|
public class DatabasirAuthenticationException extends AuthenticationException {
|
||||||
|
|
||||||
|
private final DatabasirException databasirException;
|
||||||
|
|
||||||
public DatabasirAuthenticationException(DatabasirException databasirException) {
|
public DatabasirAuthenticationException(DatabasirException databasirException) {
|
||||||
super(databasirException.getErrMessage(), databasirException);
|
super(databasirException.getErrMessage(), databasirException);
|
||||||
|
this.databasirException = databasirException;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DatabasirAuthenticationException(String msg) {
|
public String getErrMessage() {
|
||||||
super(msg);
|
return databasirException.getErrMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
public DatabasirAuthenticationException(String msg, Throwable cause) {
|
public String getErrCode() {
|
||||||
super(msg, cause);
|
return databasirException.getErrCode();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package com.databasir.core.domain.app.handler;
|
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.impl.OauthAppDao;
|
||||||
import com.databasir.dao.tables.pojos.OauthAppPojo;
|
import com.databasir.dao.tables.pojos.OauthAppPojo;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
@ -8,6 +9,8 @@ import org.springframework.stereotype.Component;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static com.databasir.core.domain.DomainErrors.REGISTRATION_ID_NOT_FOUND;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class OpenAuthHandlers {
|
public class OpenAuthHandlers {
|
||||||
|
@ -26,7 +29,11 @@ public class OpenAuthHandlers {
|
||||||
}
|
}
|
||||||
|
|
||||||
public OAuthProcessResult process(String registrationId, Map<String, String[]> parameters) {
|
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()
|
return handlers.stream()
|
||||||
.filter(handler -> handler.support(app.getAppType()))
|
.filter(handler -> handler.support(app.getAppType()))
|
||||||
.findFirst()
|
.findFirst()
|
||||||
|
|
|
@ -29,9 +29,7 @@ public class OauthAppDao extends BaseDao<OauthAppPojo> {
|
||||||
}
|
}
|
||||||
|
|
||||||
public OauthAppPojo selectByRegistrationId(String registrationId) {
|
public OauthAppPojo selectByRegistrationId(String registrationId) {
|
||||||
return this.getDslContext()
|
return this.selectOptionByRegistrationId(registrationId)
|
||||||
.select(OAUTH_APP.fields()).from(OAUTH_APP).where(OAUTH_APP.REGISTRATION_ID.eq(registrationId))
|
|
||||||
.fetchOptionalInto(OauthAppPojo.class)
|
|
||||||
.orElseThrow(() -> new DataNotExistsException("can not found oauth app by " + registrationId));
|
.orElseThrow(() -> new DataNotExistsException("can not found oauth app by " + registrationId));
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue