java – 自定义Spring Oauth错误

我要自定义我的
Spring OAuth 2响应.

现在就像:

{
  "error": "invalid_token",
  "error_description": "Invalid access token: INVALID"
}

但我需要额外的数据,如:

{
  "status": -5,
  "error": "invalid_token",
  "error_description": "Invalid access token: INVALID",
  "errors":[
    {"message":"clear message for customer"}
  ]
}

如何自定义它以获取所请求的消息?

我试过这个:

我的春天servlet xml:

<bean id="oauthAccessDeniedHandler"
    class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler" >
    <property name="exceptionTranslator" ref="OauthErrorHandler" />
</bean>

<bean id="OauthErrorHandler" class="com.mypath.handler.OauthErrorHandler"/>

我的翻译:

import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.error.DefaultWebResponseExceptionTranslator;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;

public class OauthErrorHandler extends DefaultWebResponseExceptionTranslator implements WebResponseExceptionTranslator {

    @Override
    public ResponseEntity<OAuth2Exception> translate(Exception e){
     ResponseEntity<OAuth2Exception> responseEntity = null;
    try {
        responseEntity = super.translate(e);
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    OAuth2Exception body = responseEntity.getBody();
    body.addAdditionalInformation("status", "-5");
    body.addAdditionalInformation("errors", "[{\"message\":\""+body.getLocalizedMessage()+"\"}]");
    HttpHeaders headers = new HttpHeaders();
    headers.setAll(responseEntity.getHeaders().toSingleValueMap());
    return new ResponseEntity<>(body, headers, responseEntity.getStatusCode());
    }

}

但反应仍然相同

最佳答案 我已经解决了扩展OAuth2Exception和使用自定义JSON序列化器和反序列化器的问题

@org.codehaus.jackson.map.annotate.JsonSerialize(using = OAuth2ExceptionJackson1Serializer.class)
@org.codehaus.jackson.map.annotate.JsonDeserialize(using = OAuth2ExceptionJackson1Deserializer.class)
@com.fasterxml.jackson.databind.annotation.JsonSerialize(using = OAuth2ExceptionJackson2Serializer.class)
@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = OAuth2ExceptionJackson2Deserializer.class)
public class CustomOauthException extends OAuth2Exception {

    private static final long serialVersionUID = 124661L;

    public CustomOauthException(String msg) {
        super(msg);
    }
    private String oaut_error_code;
    private int http_error_code;


    public CustomOauthException(String msg, Throwable t) {
        super(msg, t);
    }

    @Override
    public int getHttpErrorCode() {
        return this.http_error_code;
    }


    public int getHttp_error_code() {
        return http_error_code;
    }

    public void setHttp_error_code(int http_error_code) {
        this.http_error_code = http_error_code;
    }

    @Override
    public String getOAuth2ErrorCode() {
        return oaut_error_code;
    }



    public void setOaut_error_code(String oaut_error_code) {
        this.oaut_error_code = oaut_error_code;
    }





}
点赞