2

我稍微更新了一个 Spring Cloud Services 示例来说明我遇到的问题: https ://github.com/spring-cloud-services-samples/greeting/compare/master...timtebeek:master

经过上述更改后,我正在使用:

  • spring-cloud-services-starter-config-client:1.5.0.RELEASE
  • spring-cloud-services-starter-service-registry:1.5.0.RELEASE
  • spring-cloud-starter-oauth2:2.0.14.RELEASE

我还添加了一个最小的ResourceServerConfigurerAdapter

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
  @Override
  public void configure(final HttpSecurity http) throws Exception {
    http.authorizeRequests().anyRequest().authenticated();
  }

  @Override
  public void configure(final ResourceServerSecurityConfigurer resources) {
    resources.resourceId("greeter");
  }
}

以及最低限度的配置设置:

security: oauth2: resource: jwt: key-uri: https://example.com/oauth/token_key

通过这些更改,我的应用程序无法在 PCF-DEV 中部署;我没有尝试过正确的 PCF,但希望结果是相似的。这是我收到的错误消息:

Method userInfoRestTemplateFactory in org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerTokenServicesConfiguration required a single bean, but 2 were found: - eurekaOAuth2ResourceDetails: defined by method 'eurekaOAuth2ResourceDetails' in class path resource [io/pivotal/spring/cloud/service/eureka/EurekaOAuth2AutoConfiguration.class] - configClientOAuth2ResourceDetails: defined by method 'configClientOAuth2ResourceDetails' in io.pivotal.spring.cloud.service.config.ConfigClientOAuth2BootstrapConfiguration Action: Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

所以它试图使用OAuth2ProtectedResourceDetails与 spring-cloud-services-starters 完全分开的东西来设置我的应用程序安全性;我希望它只使用外部 JWT 密钥。

谁能帮助我如何让我的 PCF 部署的应用程序同时使用配置和发现服务也使用外部 JWT 令牌进行身份验证设置?

4

2 回答 2

2

我遇到了同样的事情。几个月前我看到了这个帖子。我还升级到 spring boot 1.5.4 和 cloud Dalston.SR4,这让我克服了困难。谢谢。

看到http://start.spring.io使用的是 spring boot 1.5.9。它在 PivotalWS 上运行,所以我知道有一个解决方案。

试试这个改变:

security:
  oauth2:
    client:
      client-id: someclient
      client-secret: someclientpass
    resource:
      jwt:
        key-uri: https://example.com/oauth/token_key

在我的例子中,client-id 和 client-secret 是虚拟值。我假设由于您也在使用 JWT 令牌,因此您的资源不需要使用您的 JWT 令牌提供程序验证令牌,只需签名 (key-uri)。

因此,通过添加 client-id 和 client-secret,我猜测(完全猜测)它会创建所需的 OAuth2ProtectedResourceDetails 并具有更好(更近)的范围。

当我们不需要查找用户信息时,它正在寻找“userInfoRestTemplateFactory”这一事实将我指向了这个方向。

我的服务已通过此更改成功部署在 PivotalWS (run.pivotal.io) 上,使用 spring boot 1.5.9 和 Dalston.SR4 和 io.pivotal.spring.cloud:spring-cloud-services-dependencies:1.5.0.RELEASE

于 2018-01-19T02:01:55.283 回答
1

将 spring-boot-starter-parent 更改为 1.5.2.RELEASE ,spring-cloud-dependencies 为 Dalston.RC1 , spring-cloud-services-dependencies 1.5.0.RELEASE

于 2017-10-06T11:56:13.090 回答