【问题标题】:Trouble configuring Springboot with openapi 3.0, aws-cognito oauth2使用 openapi 3.0、aws-cognito oauth2 配置 Springboot 时遇到问题
【发布时间】:2020-09-13 18:00:52
【问题描述】:

我试图将 OpenAPI 3.0 集成到我现有的 springboot restpai 应用程序中。到目前为止,我可以使用此代码 sn-p 配置 openapi-with oAuth2。

    @Bean
    public OpenAPI customOpenAPI() {

        OAuthFlow oAuthFlowObject = new OAuthFlow();
        oAuthFlowObject
                .setAuthorizationUrl("https://<my-domain>.auth.us-east-2.amazoncognito.com/oauth2/authorize");
        oAuthFlowObject.setRefreshUrl("https://<my-domain>.auth.us-east-2.amazoncognito.com/oauth2/refresh");
        oAuthFlowObject.setTokenUrl("https://<my-domain>.auth.us-east-2.amazoncognito.com/oauth2/token");

        OAuthFlows oAuthFlows = new OAuthFlows();
        oAuthFlows.authorizationCode(oAuthFlowObject);

        return new OpenAPI()
                .components(new Components()
                                    .addSecuritySchemes("oauth2", new SecurityScheme().in(SecurityScheme.In.HEADER)
                                                                                      .type(SecurityScheme.Type.OAUTH2)
                                                                                      .flows(oAuthFlows)
                                                        .bearerFormat("JWT")
                                                        .scheme("bearer")
                                    ))
                .info(new Info().title("Contact Application API").description(
                        "This is a sample Spring Boot RESTful service using springdoc-openapi and OpenAPI 3."))
                ;
    }

看来我可以成功从cognito获取token了。

但问题是当我从 swagger-ui 试用任何 api 时,它不包括不记名令牌。

我有什么遗漏的吗? 我如何设置路径前缀,以便在调用这些路径时附加令牌。此外,我只想从 swagger 发送 Authorization-bearer 标头中的“id_token”。

【问题讨论】:

    标签: java spring-boot oauth-2.0 swagger openapi


    【解决方案1】:

    要为 swagger 配置 aws-cognito 并使其发送 id-token 而不是 access-token,我们需要配置两种安全机制。这是示例代码 sn-ps。这是一个解决方法。由于默认发送access-token,而我们需要发送id-token,因此我们配置了两个选项。

    
    @Bean
        public OpenAPI customOpenAPI() {
            return new OpenAPI()
                    //bearer auth with oAuth2 
                    .addSecurityItem(new SecurityRequirement().addList("bearerAuth"))
                    .components(new Components().addSecuritySchemes("oAuth2",                   new SecurityScheme()                                                                .type(SecurityScheme.Type.OAUTH2)                                                               .flows(getOAuthFlows())
    )
    
    /// Bearer AUTH security config settings. ## for id-token.
    .addSecuritySchemes("bearerAuth",                                                       new SecurityScheme()
    .type(SecurityScheme.Type.HTTP)
    .scheme("bearer")
    .bearerFormat("JWT")
    )
    ).info(getInfo());
    }
    
    
        private Info getInfo() {
            return new Info()
                    .title("Title")
                    .version("1.0")
                    .description("Project description..");
        }
    

    它的外观如下:

    之后,您需要从浏览器控制台配置 id-token。

    【讨论】:

      猜你喜欢
      • 2019-04-11
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-09
      • 2014-04-21
      • 2023-03-19
      相关资源
      最近更新 更多