在 spring 启动应用程序中使用 cloudfoundry api 时如何进行身份验证

How to authenticate when using cloudfoundry apis in a spring boot app

我想在一个简单的 spring 启动应用程序中使用 cloudfoundry api (https://apidocs.cloudfoundry.org/272/)。

我正在关注为其实施 java 客户端的文档。 (https://docs.cloudfoundry.org/buildpacks/java/java-client.html)

我的 pom.xml ->

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.cloudfoundry/cloudfoundry-client-lib -->
<dependency>
    <groupId>org.cloudfoundry</groupId>
    <artifactId>cloudfoundry-client-lib</artifactId>
    <version>1.1.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.cloudfoundry</groupId>
    <artifactId>cloudfoundry-client-reactor</artifactId>
    <version>2.20.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.cloudfoundry</groupId>
    <artifactId>cloudfoundry-operations</artifactId>
    <version>2.20.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>io.projectreactor</groupId>
    <artifactId>reactor-core</artifactId>
    <version>3.1.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>io.projectreactor.ipc</groupId>
    <artifactId>reactor-netty</artifactId>
    <version>0.7.0.RELEASE</version>
</dependency>

控制器和应用程序文件与示例简单 spring 引导启动工具包中提到的完全相同。 (https://spring.io/guides/gs/spring-boot/)。

使用独立 class(如 cloudfoundry 文档中所述)时它有效,但在 spring 启动应用程序中使用相同的代码时, 它给了我 -

{
    "timestamp": 1510245582167,
    "status": 401,
    "error": "Unauthorized",
    "message": "Full authentication is required to access this resource",
    "path": "/hello"
}

我在日志中注意到,当我添加 cloudfoundry-client-lib 依赖项时,会形成一个过滤器链..

2017-11-09 11:34:38.086  INFO 8648 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: OrRequestMatcher [requestMatchers=[Ant [pattern='/**']]], 
[org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@288ca5f0, 
org.springframework.security.web.context.SecurityContextPersistenceFilter@2ba5aa7a, org.springframework.security.web.header.HeaderWriterFilter@4207609e, 
org.springframework.security.web.authentication.logout.LogoutFilter@2c7a8af2,
org.springframework.security.web.authentication.www.BasicAuthenticationFilter@21c815e4, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@22e5f96e,
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@5b22d8a1, 
org.springframework.security.web.authentication.AnonymousAuthenticationFilter@4068102e, 
org.springframework.security.web.session.SessionManagementFilter@67b100fe, 
org.springframework.security.web.access.ExceptionTranslationFilter@704641e3,
org.springframework.security.web.access.intercept.FilterSecurityInterceptor@7efd28bd]

使用cloudfoundry lib时如何处理鉴权或如何处理绕过过滤链安全?


解决方案:CF Java 客户端 v2 与 v1 有不同的 API,因此您不会在 v2 中找到 CloudCredentials。有关构建新客户端和提供凭据的示例,请参阅 v2 READMEhttps://github.com/cloudfoundry/cf-java-client#cloudfoundryclient-dopplerclient-uaaclient-builders 同样,仅使用 v2 库,不要在同一应用程序中使用任何 v1 库。


我不确定该错误的确切来源,但您在同一个应用程序中混用了 CF Java 客户端的不同主要版本。这不太可能奏效。请参阅 CF Java Client 的项目页面,并仅使用所有 org.cloudfoundry 依赖项的 2.x.x 版本。所有 org.cloudfoundry 依赖项也应该在相同的版本上。

所以替换:

<dependency>
        <groupId>org.cloudfoundry</groupId>
        <artifactId>cloudfoundry-client-lib</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.cloudfoundry</groupId>
        <artifactId>cloudfoundry-client-reactor</artifactId>
        <version>2.0.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.cloudfoundry</groupId>
        <artifactId>cloudfoundry-operations</artifactId>
        <version>2.20.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.cloudfoundry</groupId>
        <artifactId>cloudfoundry-client-reactor</artifactId>
        <version>2.20.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.cloudfoundry</groupId>
        <artifactId>cloudfoundry-operations</artifactId>
        <version>2.20.0.RELEASE</version>
    </dependency>