Spring OAuth2 缺少 DefaultOAuth2Token 转换器

Spring OAuth2 missing converter for DefaultOAuth2Token

我正在使用 Spring OAuth 版本 2.0.7。通过阅读源代码,我认为端点 /oauth/token 应该配置了 DefaultRequestTokenConverter,但我收到以下异常:

org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.postAccessToken(java.security.Principal,java.util.Map<java.lang.String, java.lang.String>) throws org.springframework.web.HttpRequestMethodNotSupportedException]: java.lang.IllegalArgumentException: No converter found for return value of type: class org.springframework.security.oauth2.common.DefaultOAuth2AccessToken

似乎在 org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor 中找不到可生产的媒体类型:

if (returnValue != null && producibleMediaTypes.isEmpty()) {
    throw new IllegalArgumentException("No converter found for return value of type: " + returnValueClass);
}

无论如何,我还尝试为端点明确设置一个转换器:

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    endpoints.accessTokenConverter(new DefaultAccessTokenConverter()).tokenStore(tokenStore());
}

异常依旧

我还尝试在我的 Spring MVC 配置中设置消息转换器:

@Configuration
@EnableWebMvc
public class AppConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new FormOAuth2AccessTokenMessageConverter());
        converters.add(new FormOAuth2ExceptionHttpMessageConverter());
    }
}

但还是出现异常

我不知道在哪里设置消息转换器。

万一有人遇到同样的问题,在项目的 pom.xml 中添加以下内容可解决此缺少转换器的错误:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.6.2</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.6.2</version>
</dependency>