🐛 Fixing a bug. #2533 登录过程中自定义异常,中文提示 500 异常

This commit is contained in:
lengleng committed 2021-04-01 21:41:03 +08:00
1 parent 381838a431
commit cbdd175831
1 file changed
+10 -17
@@ -17,26 +17,28 @@
package com.pig4cloud.pigx.common.security.component;
import com.pig4cloud.pigx.common.security.exception.*;
import java.util.Locale;
import com.pig4cloud.pigx.common.security.exception.ForbiddenException;
import com.pig4cloud.pigx.common.security.exception.InvalidException;
import com.pig4cloud.pigx.common.security.exception.MethodNotAllowedException;
import com.pig4cloud.pigx.common.security.exception.PigxAuth2Exception;
import com.pig4cloud.pigx.common.security.exception.ServerErrorException;
import com.pig4cloud.pigx.common.security.exception.UnauthorizedException;
import com.pig4cloud.pigx.common.security.util.PigxSecurityMessageSourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.oauth2.common.DefaultThrowableAnalyzer;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.exceptions.ClientAuthenticationException;
import org.springframework.security.oauth2.common.exceptions.InsufficientScopeException;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
import org.springframework.security.web.util.ThrowableAnalyzer;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import java.util.Locale;
/**
* @author lengleng
* @date 2020-06-20 OAutH Server 异常处理,重写oauth 默认实现
@@ -89,21 +91,12 @@ public class PigxWebResponseExceptionTranslator implements WebResponseExceptionT
}
private ResponseEntity<OAuth2Exception> handleOAuth2Exception(OAuth2Exception e) {
int status = e.getHttpErrorCode();
HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.CACHE_CONTROL, "no-store");
headers.set(HttpHeaders.PRAGMA, "no-cache");
if (status == HttpStatus.UNAUTHORIZED.value() || (e instanceof InsufficientScopeException)) {
headers.set(HttpHeaders.WWW_AUTHENTICATE,
String.format("%s %s", OAuth2AccessToken.BEARER_TYPE, e.getSummary()));
}
// 客户端异常直接返回客户端,不然无法解析
if (e instanceof ClientAuthenticationException) {
return new ResponseEntity<>(e, headers, HttpStatus.valueOf(status));
return new ResponseEntity<>(e, HttpStatus.valueOf(status));
}
return new ResponseEntity<>(new PigxAuth2Exception(e.getMessage(), e.getOAuth2ErrorCode()), headers,
return new ResponseEntity<>(new PigxAuth2Exception(e.getMessage(), e.getOAuth2ErrorCode()),
HttpStatus.valueOf(status));
}