feat: add custom authentication exception

This commit is contained in:
vran
2022-03-02 14:28:34 +08:00
parent e09dcfe37e
commit 7303d4d840
7 changed files with 45 additions and 5 deletions

View File

@@ -10,6 +10,7 @@ import lombok.RequiredArgsConstructor;
public enum DomainErrors implements DatabasirErrors {
REFRESH_TOKEN_EXPIRED("X_0001", "refresh token expired"),
INVALID_REFRESH_TOKEN_OPERATION("X_0002", "invalid refresh token operation"),
NETWORK_ERROR("X_0003", "网络似乎不稳定,请稍后再试"),
NOT_SUPPORT_DATABASE_TYPE("A_10000", "不支持的数据库类型, 请检查项目配置"),
PROJECT_NOT_FOUND("A_10001", "项目不存在"),

View File

@@ -1,6 +1,8 @@
package com.databasir.core.infrastructure.oauth2;
import com.databasir.core.domain.DomainErrors;
import com.databasir.core.infrastructure.oauth2.exception.DatabasirAuthenticationException;
import com.databasir.core.infrastructure.remote.github.GithubRemoteService;
import com.databasir.dao.enums.OAuthAppType;
import com.databasir.dao.impl.OAuthAppDao;
@@ -51,9 +53,12 @@ public class GithubOauthHandler implements OAuthHandler {
Map<String, String[]> params = context.getCallbackParameters();
String code = params.get("code")[0];
String accessToken = githubRemoteService.getToken(baseUrl, clientId, clientSecret, code)
.get("access_token")
.asText();
JsonNode tokenNode = githubRemoteService.getToken(baseUrl, clientId, clientSecret, code)
.get("access_token");
if (tokenNode == null) {
throw new DatabasirAuthenticationException(DomainErrors.NETWORK_ERROR.exception());
}
String accessToken = tokenNode.asText();
if (StringUtils.isBlank(accessToken)) {
throw new CredentialsExpiredException("授权失效,请重新登陆");
}

View File

@@ -52,6 +52,7 @@ public class OAuthAppService {
user.setNickname(result.getNickname());
user.setEmail(result.getEmail());
user.setAvatar(result.getAvatar());
user.setEnabled(true);
user.setPassword(UUID.randomUUID().toString().substring(0, 6));
Integer id = userService.create(user);
return userService.get(id);

View File

@@ -0,0 +1,19 @@
package com.databasir.core.infrastructure.oauth2.exception;
import com.databasir.common.DatabasirException;
import org.springframework.security.core.AuthenticationException;
public class DatabasirAuthenticationException extends AuthenticationException {
public DatabasirAuthenticationException(DatabasirException databasirException) {
super(databasirException.getErrMessage(), databasirException);
}
public DatabasirAuthenticationException(String msg) {
super(msg);
}
public DatabasirAuthenticationException(String msg, Throwable cause) {
super(msg, cause);
}
}

View File

@@ -61,7 +61,7 @@ public class GithubRemoteService {
try {
Response<T> response = call.execute();
if (!response.isSuccessful()) {
log.error("request error: " + call.request());
log.error("request error: " + call.request() + ", response = " + response);
throw new SystemException("Call Remote Error");
} else {
T body = response.body();