【问题标题】:Customize Spring Oauth Error自定义 Spring Oauth 错误
【发布时间】:2016-05-17 20:14:06
【问题描述】:

我必须自定义我的 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());
    }

}

但反应还是一样

【问题讨论】:

  • 你有没有尝试过创建自定义异常和创建自定义错误处理程序?
  • @İlkerKorkut 当然,但我认为我需要将我的 spring servlet xml 编辑为自定义异常翻译器,不仅仅是自定义异常.. 我只需要不同的响应,我试图改变序列化器也是,但不是最好的方法......
  • 请从您的标题和问题的答案中删除“已解决”。您可以添加自己的解决方案作为正确答案。
  • 如果有人找到了确切的解决方案,请分享。我也面临同样的问题。

标签: java spring spring-security oauth-2.0


【解决方案1】:

我已经解决了扩展 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;
    }





}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-06-03
  • 2015-07-03
  • 2011-06-15
  • 1970-01-01
  • 2013-08-29
  • 2020-10-25
  • 2016-09-14
  • 2017-12-13
相关资源
最近更新 更多