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