🐛 Fixing a bug. oauth print http_error_code always the default

This commit is contained in:
冷冷 committed 2018-12-12 10:11:38 +08:00
1 parent 189fbdebeb
commit c6dda5c8e9
3 files changed
+17 -10

No files matched your search

@@ -40,7 +40,7 @@ public class PigxAuth2ExceptionSerializer extends StdSerializer<PigxAuth2Excepti
gen.writeStartObject();
gen.writeObjectField("code", 1);
gen.writeStringField("message", value.getMessage());
gen.writeStringField("data", value.getOAuth2ErrorCode());
gen.writeStringField("data", value.getErrorCode());
gen.writeEndObject();
}
}
@@ -51,31 +51,31 @@ public class PigxWebResponseExceptionTranslator implements WebResponseExceptionT
Throwable[] causeChain = throwableAnalyzer.determineCauseChain(e);
Exception ase = (AuthenticationException) throwableAnalyzer.getFirstThrowableOfType(AuthenticationException.class,
causeChain);
causeChain);
if (ase != null) {
return handleOAuth2Exception(new UnauthorizedException(e.getMessage(), e));
}
ase = (AccessDeniedException) throwableAnalyzer
.getFirstThrowableOfType(AccessDeniedException.class, causeChain);
.getFirstThrowableOfType(AccessDeniedException.class, causeChain);
if (ase != null) {
return handleOAuth2Exception(new ForbiddenException(ase.getMessage(), ase));
}
ase = (InvalidGrantException) throwableAnalyzer
.getFirstThrowableOfType(InvalidGrantException.class, causeChain);
.getFirstThrowableOfType(InvalidGrantException.class, causeChain);
if (ase != null) {
return handleOAuth2Exception(new InvalidException(ase.getMessage(), ase));
}
ase = (HttpRequestMethodNotSupportedException) throwableAnalyzer
.getFirstThrowableOfType(HttpRequestMethodNotSupportedException.class, causeChain);
.getFirstThrowableOfType(HttpRequestMethodNotSupportedException.class, causeChain);
if (ase != null) {
return handleOAuth2Exception(new MethodNotAllowedException(ase.getMessage(), ase));
}
ase = (OAuth2Exception) throwableAnalyzer.getFirstThrowableOfType(
OAuth2Exception.class, causeChain);
OAuth2Exception.class, causeChain);
if (ase != null) {
return handleOAuth2Exception((OAuth2Exception) ase);
@@ -98,11 +98,10 @@ public class PigxWebResponseExceptionTranslator implements WebResponseExceptionT
// 客户端异常直接返回客户端,不然无法解析
if (e instanceof ClientAuthenticationException) {
return new ResponseEntity<>(e, headers,
HttpStatus.valueOf(status));
HttpStatus.valueOf(status));
}
return new ResponseEntity<>(new PigxAuth2Exception(e.getMessage()), headers,
HttpStatus.valueOf(status));
return new ResponseEntity<>(new PigxAuth2Exception(e.getMessage(), e.getOAuth2ErrorCode()), headers,
HttpStatus.valueOf(status));
}
}
@@ -19,6 +19,7 @@ package com.pig4cloud.pigx.common.security.exception;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.pig4cloud.pigx.common.security.component.PigxAuth2ExceptionSerializer;
import lombok.Getter;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
/**
@@ -28,8 +29,15 @@ import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
*/
@JsonSerialize(using = PigxAuth2ExceptionSerializer.class)
public class PigxAuth2Exception extends OAuth2Exception {
@Getter
private String errorCode;
public PigxAuth2Exception(String msg) {
super(msg);
}
public PigxAuth2Exception(String msg, String errorCode) {
super(msg);
this.errorCode = errorCode;
}
}