优化oauth2授权错误处理
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.central.oauth.config;
|
||||
|
||||
import com.central.oauth.exception.ValidateCodeException;
|
||||
import com.central.oauth.handler.OauthLogoutHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -34,19 +35,20 @@ public class SecurityHandlerConfig {
|
||||
@Bean
|
||||
public WebResponseExceptionTranslator webResponseExceptionTranslator() {
|
||||
return new DefaultWebResponseExceptionTranslator() {
|
||||
public static final String BAD_MSG = "坏的凭证";
|
||||
private static final String BAD_MSG = "坏的凭证";
|
||||
private static final String BAD_MSG_EN = "Bad credentials";
|
||||
|
||||
@Override
|
||||
public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception {
|
||||
OAuth2Exception oAuth2Exception;
|
||||
if (e.getMessage() != null && e.getMessage().equals(BAD_MSG)) {
|
||||
if (e.getMessage() != null
|
||||
&& (BAD_MSG.equals(e.getMessage()) || BAD_MSG_EN.equals(e.getMessage()))) {
|
||||
oAuth2Exception = new InvalidGrantException("用户名或密码错误", e);
|
||||
} else if (e instanceof InternalAuthenticationServiceException) {
|
||||
oAuth2Exception = new InvalidGrantException(e.getMessage(), e);
|
||||
} else if (e instanceof RedirectMismatchException) {
|
||||
oAuth2Exception = new InvalidGrantException(e.getMessage(), e);
|
||||
} else if (e instanceof InvalidScopeException) {
|
||||
} else if (e instanceof InternalAuthenticationServiceException
|
||||
|| e instanceof ValidateCodeException) {
|
||||
oAuth2Exception = new InvalidGrantException(e.getMessage(), e);
|
||||
} else if (e instanceof OAuth2Exception) {
|
||||
oAuth2Exception = (OAuth2Exception)e;
|
||||
} else {
|
||||
oAuth2Exception = new UnsupportedResponseTypeException("服务内部错误", e);
|
||||
}
|
||||
|
||||
@@ -63,11 +63,6 @@ public class OauthTokenAspect {
|
||||
.status(HttpStatus.OK)
|
||||
.body(Result.succeed(body));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("授权错误", e);
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.BAD_REQUEST)
|
||||
.body(Result.failed(e.getMessage()));
|
||||
} finally {
|
||||
TenantContextHolder.clear();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user