如何在OAuth2RestTemplate中更改MappingJacksonHttpMessageConverter的MediaType [英] How to change MediaType for MappingJacksonHttpMessageConverter in OAuth2RestTemplate

查看:361
本文介绍了如何在OAuth2RestTemplate中更改MappingJacksonHttpMessageConverter的MediaType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序使用Spring Source OAuth2作为客户端从资源服务器检索用户数据并创建本地用户。当OAuth2ClientContextFilter尝试检索令牌时,我不断收到错误:

I have an application that is using Spring Source OAuth2 as s client to retrieve user data from a resource server and create a local user. I keep getting the error when the OAuth2ClientContextFilter tries to retrieve the token:

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [org.springframework.security.oauth2.common.OAuth2AccessToken] and content type [application/x-javascript;charset=utf-8]

我理解默认的MediaType是'application / json'所以我试着像这样自定义MappingJacksonHttpMessageConverter:

I understand the default MediaType is 'application/json' so I tried to customize the MappingJacksonHttpMessageConverter like this:

<bean id="jacksonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <bean class="org.springframework.http.MediaType">
                    <constructor-arg value="application"/>
                    <constructor-arg value="x-javascript"/>
                    <constructor-arg value="UTF-8"/>
                </bean>
            </list>
     </property>
</bean>

我还尝试了'ALL'构造函数arg,它应该支持 * / * 内容类型但没有运气。请参阅 http:// static。 springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/http/MediaType.html

I also tried the 'ALL' constructor arg that is supposed to support */* content types but no luck. See http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/http/MediaType.html

其他重要信息是我我正在使用全XML配置。我刚刚将2.5应用程序升级到3.1.1。我在spring security PRE_AUTH过滤器中使用OAuth2RestTemplate,而不是在控制器中。所以我没有使用注释来映射其余的调用。我已经尝试添加< context:annotation-config /> 但这并没有什么区别。

Other important info is that I am using an all XML configuration right now. I just upgraded our 2.5 app to 3.1.1. I'm using OAuth2RestTemplate in a spring security PRE_AUTH filter, not in a controller. So I'm not using annotations to map the rest calls. I've tried adding <context:annotation-config/> but that didn't make a difference.

我只是从我的自定义AbstractPreAuthenticatedProcessingFilter调用我的OAuth服务bean。当服务bean尝试执行对用户数据的其余调用时,会抛出异常,触发尝试检索令牌的OAuth2ClientContextFilter。这是我的OAuth2服务bean配置:

I'm simply calling my OAuth service bean from my custom AbstractPreAuthenticatedProcessingFilter. When the service bean tries to execute the rest call for user data, an exception is thrown, triggering the OAuth2ClientContextFilter which attempts to retrieve the token. Here's my OAuth2 service bean config:

<bean id="reprintsOauthService" class="com.coral.user.ReprintsOauthService">
    <property name="reprintsUserInfoUrl" value="https://www.demo.com/api/userinfo.ashx" />
    <property name="reprintsRestTemplate">
        <bean class="org.springframework.security.oauth2.client.OAuth2RestTemplate">
            <constructor-arg ref="reprintsResource"/>
            <property name="messageConverters">
                <list>
                   <ref bean="jacksonConverter"/>
                </list>
            </property>
        </bean>
    </property>
</bean> 

我错过了什么吗?为什么Jackson没有映射回复?

Am I missing something? Why doesn't Jackson map the response?

推荐答案

修正了!问题是OAuth2RestTemplate不用于令牌检索。所以我必须自定义org.springframework.security.oauth2.client.token.OAuth2AccessTokenSupport并将MappingJacksonHttpMessageConverter添加到现有方法中,如下所示:

Fixed! The problem was that the OAuth2RestTemplate isn't used for token retrieval. So I had to customize the org.springframework.security.oauth2.client.token.OAuth2AccessTokenSupport and add the MappingJacksonHttpMessageConverter to the existing method like this:

  public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
    this.messageConverters = new ArrayList<HttpMessageConverter<?>>(messageConverters);
    this.messageConverters.add(new FormOAuth2AccessTokenMessageConverter());
    this.messageConverters.add(new FormOAuth2ExceptionHttpMessageConverter());

    MappingJacksonHttpMessageConverter jackson = new MappingJacksonHttpMessageConverter();
    List<MediaType> mediaTypes = new ArrayList<MediaType>();
    mediaTypes.add(new MediaType("application", "x-javascript"));
    jackson.setSupportedMediaTypes(mediaTypes);
    this.messageConverters.add(jackson);

    if(logger.isDebugEnabled())
    {
        logger.debug("*** Added custom media type 'application/x-javascript' to the Jackson converter");
    }
}

这篇关于如何在OAuth2RestTemplate中更改MappingJacksonHttpMessageConverter的MediaType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆