【问题标题】:Could not fetch user details: class org.springframework.web.client.RestClientException, Could not extract response: no suitable无法获取用户详细信息:类 org.springframework.web.client.RestClientException,无法提取响应:不合适
【发布时间】:2017-01-04 20:08:44
【问题描述】:

我在构建简单应用程序时收到此错误消息

从:http://localhost:9999/uaa/user 获取用户信息 无法获取用户详细信息:org.springframework.web.client.RestClientException 类,无法提取响应:没有为响应类型 [interface java.util.Map] 和内容类型 [text/html;charset=UTF-8 找到合适的 HttpMessageConverter ]

但是我确定内容类型是 json

这是github仓库https://github.com/ashraf-revo/oauth

这是服务器

@SpringBootApplication
public class AuthServerApplication {

public static void main(String[] args) {
    SpringApplication.run(AuthServerApplication.class, args);
}
}

@Order(1)
@Configuration
@EnableWebSecurity
class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws     Exception {
      auth.inMemoryAuthentication().withUser("ashraf").password("ashraf").roles("user");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().antMatchers("/user").authenticated().and().formLogin();
}

@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
}
}

@Configuration
class OAuth2ServerConfig {
private static final String RESOURCE_IDS = "revo";

@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.resourceId(RESOURCE_IDS);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests().antMatchers("/user").authenticated();
    }
}

@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
    @Autowired
    TokenStore tokenStore;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory().withClient("revo")
                .resourceIds(RESOURCE_IDS)
                .authorizedGrantTypes("authorization_code", "implicit")
                .authorities("ROLE_CLIENT")
                .scopes("read", "write")
                .secret("revo");
    }

    @Bean
    public TokenStore tokenStore() {
        return new InMemoryTokenStore();
    }

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

}

这是客户

@SpringBootApplication
public class ClientApplication {

public static void main(String[] args) {
    SpringApplication.run(ClientApplication.class, args);
}
}

@Configuration
@EnableOAuth2Sso
class SecurityConfigurer extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().mvcMatchers("/home").authenticated();
}
}

 @Configuration
 class MvcConfigurer extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("index");
    registry.addViewController("/home").setViewName("home");
}
}

【问题讨论】:

  • 您请求“text/html”,但希望收到“application/json”。那是行不通的。
  • 我无法控制发送此请求。 spring EnableOAuth2Sso 发送请求以获取用户信息
  • 你能发布你完整的异常堆栈跟踪吗?

标签: java spring spring-boot spring-oauth2


【解决方案1】:

你可以考虑 spring-boot 版本和 spring-cloud 版本,选择不是最新的版本,比如 spring-boot-1.4.4.RELEASE 和 spring-cloud-Camden.SR5;因为我有同样的问题并通过这个解决方式

【讨论】:

  • 您的回答是否与所提问题相关?
  • 我在使用 spring-boot-1.5.2.RELEASE 和 spring-cloud-Dalston 时遇到了同样的问题,所以我只是给出我的建议
【解决方案2】:

如果您可以控制服务器响应,请修改它以将 Content-type 设置为 application/json。

请尝试,

@RequestMapping(value = "/user", method = RequestMethod.GET, 
produces = "application/json; charset=utf-8")

【讨论】:

    猜你喜欢
    • 2015-05-03
    • 2017-09-06
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 2019-11-21
    • 2015-07-03
    • 2017-09-24
    • 2016-07-11
    相关资源
    最近更新 更多