我有一个应用程序使用 Spring Source OAuth2 作为客户端从资源服务器检索用户数据并创建本地用户。当 OAuth2ClientContextFilter 尝试检索令牌时,我不断收到错误消息:
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:
<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
其他重要信息是我现在使用的是全 XML 配置。我刚刚将我们的 2.5 应用升级到 3.1.1。我在弹簧安全 PRE_AUTH 过滤器中使用 OAuth2RestTemplate,而不是在控制器中。所以我没有使用注释来映射其余的调用。我试过添加<context:annotation-config/>
,但这并没有什么不同。
我只是从我的自定义 AbstractPreAuthenticatedProcessingFilter 中调用我的 OAuth 服务 bean。当服务 bean 尝试对用户数据执行 rest 调用时,会引发异常,触发 OAuth2ClientContextFilter 尝试检索令牌。这是我的 OAuth2 服务 bean 配置:
<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>
我错过了什么吗?为什么杰克逊不映射响应?