Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 自定义Spring Oauth错误_Java_Spring_Spring Security_Oauth 2.0 - Fatal编程技术网

Java 自定义Spring Oauth错误

Java 自定义Spring Oauth错误,java,spring,spring-security,oauth-2.0,Java,Spring,Spring Security,Oauth 2.0,我必须定制我的Spring OAuth 2响应。 现在是这样的: { "error": "invalid_token", "error_description": "Invalid access token: INVALID" } 但我需要其他数据,如: { "status": -5, "error": "invalid_token", "error_description": "Invalid access token: INVALID", "errors":[

我必须定制我的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"}
  ]
}
如何自定义它以获取请求的消息

我试过这个:

我的spring 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());
    }

}
import org.springframework.http.HttpHeaders;
导入org.springframework.http.ResponseEntity;
导入org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
导入org.springframework.security.oauth2.provider.error.DefaultWebResponseExceptionTranslator;
导入org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
公共类OauthErrorHandler扩展DefaultWebResponseExceptionTranslator实现WebResponseExceptionTranslator{
@凌驾
公共责任翻译(例外e){
ResponseEntity ResponseEntity=null;
试一试{
responseEntity=super.translate(e);
}捕获(异常e1){
e1.printStackTrace();
}
OAuth2Exception body=responseEntity.getBody();
正文。添加附加信息(“状态”、“-5”);
body.addAddAdditionalInformation(“errors”、“[{\“message\”:\”“+body.getLocalizedMessage()+“\”}]”;
HttpHeaders=新的HttpHeaders();
setAll(responseEntity.getHeaders().toSingleValueMap());
返回新的ResponseEntity(正文、标题、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;
    }





}

你曾经尝试过创建自定义异常和创建自定义错误处理程序吗?@lkerkorku当然,但我认为我需要将我的spring servlet xml编辑为自定义异常转换器,而不仅仅是自定义异常。。我只需要一个不同的回答,我试图改变序列化太多,但不是最好的方式…请删除“已解决”从您的标题和您的问题的答案。您可以添加自己的解决方案作为正确答案。如果有人找到了确切的解决方案,请与我们分享。我也面临同样的问题。如何配置Spring以使用CustomOAutheException?您可以找到更多信息: