提交 91d14784 编写于 作者: zlt2000's avatar zlt2000

优化oauth2授权错误处理

上级 0f3145f3
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 { ...@@ -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 { ...@@ -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();
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册