【问题标题】:Changing Json return format of Spring OAuth2更改 Spring OAuth2 的 Json 返回格式
【发布时间】:2017-01-28 07:18:07
【问题描述】:

在整合我们的 json 响应时,我尝试将 spring oauth2 json 响应更改为我们的格式。

来自

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

{
  "status" : 401,
  "error_code": "invalid_token",
  "description": "Invalid access token: undefined"
}

我已经调试并发现了几个可能相关的点,但我无法将所有内容放在一起。

这些是我的方法

  • 回复写在这里OAuth2ExceptionJackson2Serializer,但我不知道如何在spring中交换那个序列化器
  • 我找到了WebResponseExceptionTranslator。但据我了解,它不允许在那里设置 json 正文
  • json 正文是由 DefaultOAuth2ExceptionRenderer 编写的,但我无法设置它。我只找到了如何设置ExceptionTranslator,通过在AuthorizationServerEndpointsConfigurer 中设置它。但它不允许设置渲染器
  • 文档谈到设置HttpMessageConverter,我不知道该怎么做。

长话短说,我是 Spring 新手,非常感谢有关如何修改响应的一些指导。

谢谢, 奥托

【问题讨论】:

    标签: java spring spring-boot spring-oauth2


    【解决方案1】:

    找到解决办法,注册WebResponseExceptionTranslator:

        @Bean
    public WebResponseExceptionTranslator webResponseExceptionTranslator() {
        return new DefaultWebResponseExceptionTranslator() {
            @Override
            public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception {
                ResponseEntity<OAuth2Exception> responseEntity = super.translate(e);
                OAuth2Exception body = responseEntity.getBody();
                HttpHeaders headers = new HttpHeaders();
                headers.setAll(responseEntity.getHeaders().toSingleValueMap());
    
               // translate the exception
    
                return new ResponseEntity<>(body, headers, responseEntity.getStatusCode());
            }
        };
    }
    

    【讨论】:

    • oauth服务器返回invalid_token状态时没有触发
    猜你喜欢
    • 2016-04-04
    • 2015-05-04
    • 2012-07-09
    • 1970-01-01
    • 2017-10-13
    • 2023-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多