Java 8 当使用OAuth 2.0 URL生成令牌时,如何配置SwaggerUI以正确设置标头上的承载令牌?

Java 8 当使用OAuth 2.0 URL生成令牌时,如何配置SwaggerUI以正确设置标头上的承载令牌?,java-8,oauth-2.0,swagger-2.0,apigee,bearer-token,Java 8,Oauth 2.0,Swagger 2.0,Apigee,Bearer Token,我已设置了Swagger 2配置以连接到我们的OAuth2 accesstoken URL。 当使用Apigee URL连接时,它能够连接到URL而不会出现问题,但当我执行POST请求时,会出现无效访问令牌错误。(参见下面的堆栈跟踪) 我已经使用curl验证了URL、客户机id和secret,这是有效的。我已经验证了,当我在curl POST命令中使用提供的令牌-H“Authorization:Bearer**********************”时,它工作正常 但是,我似乎没有正确配置Sw

我已设置了Swagger 2配置以连接到我们的OAuth2 accesstoken URL。
当使用Apigee URL连接时,它能够连接到URL而不会出现问题,但当我执行POST请求时,会出现
无效访问令牌
错误。(参见下面的堆栈跟踪)

我已经使用curl验证了URL、客户机id和secret,这是有效的。我已经验证了,当我在curl POST命令中使用提供的令牌
-H“Authorization:Bearer**********************”
时,它工作正常

但是,我似乎没有正确配置Swagger来创建“Bearer”头,因为它没有显示在Swagger中显示的curl命令中

大摇大摆地授权后:

生成令牌的手动Curl命令: accesstoken响应主体: 成功使用手动Curl命令访问终结点: swagger生成的Curl命令失败: 招摇过市错误消息: POM.xml:

伊奥·斯普林福克斯
springfox-Swagger 2
2.9.2
伊奥·斯普林福克斯
springfox招摇过市用户界面
2.9.2
斯威格配置:
@配置
@使能招摇过市2
公共类招摇过市配置{
@值(${swagger.host:}”)
私有字符串摇摇摆摆的主机;
@值(${swagger.basePath:}”)
私有字符串swaggerBasePath;
@值(${swagger.oauth2.security.schema:oauth2}”)
私有字符串SecuritySchemaAuth2;
@值(${swagger.oauth2.token.request.url:}”)
私有字符串oauthTokenRequestURL;
@自动连线
ServletContext ServletContext;
@豆子
公开摘要api(){
返回新摘要(DocumentationType.SWAGGER_2)
.主持人(招摇过市主持人)
.pathProvider(新的RelativePathProvider(servletContext)){
@凌驾
公共字符串getApplicationBasePath(){
返回路径;
}
})
.选择()
.Api(RequestHandlerSelectors.withClassAnnotation(Api.class))
.path(路径选择器.any())
.build()
.apinfo(apinfo())
.securitySchemes(Collections.singletonList(oauth()))
.securityContext(Collections.singletonList(securityContext()))
.useDefaultResponseMessages(假);
}
私有OAuth OAuth(){
List authorizationScopeList=新建ArrayList();
List grantTypes=new ArrayList();
GrantType creGrant=新客户端CredentialsGrant(oauthTokenRequestURL);
grantTypes.add(creGrant);
返回新的OAuth(“oauth2schema”,authorizationScopeList,grantTypes);
}
私有apinfo apinfo(){
返回新的ApiInfoBuilder()
.标题(“废话”)
.说明(“废话”)
.版本(“2019.0.1”)
.联系人(新联系人(“废话”、“废话”、“废话”)
.build();
}
私有SecurityContext SecurityContext(){
返回SecurityContext
.builder()
.securityReferences(defaultAuth())
.forpath(路径选择器.regex(“/.*))
.build();
}
私有列表defaultAuth(){
最终授权范围授权范围=新授权范围(“全局”、“访问一切”);
最终授权范围[]授权范围=新授权范围[]{AuthorizationScope};
返回Collections.singletonList(新的SecurityReference(SecuritySchemaAuth2,authorizationScopes));
}
}

您是否也可以发布您的Swagger YAML/JSON文件?你看,这个选项不存在。所有这些都是我无法发布的
/api文档。但是,配置都是注释驱动的,并在上面的代码中提供。
curl -k -v -X POST -u *****************:*************** -d "grant_type=client_credentials" https://***********************/oauth/accesstoken
{
  "refresh_token_expires_in" : "0",
  "api_product_list" : "[********, ********]",
  "api_product_list_json" : [ "********", "********" ],
  "organization_name" : "********",
  "developer.email" : "********",
  "token_type" : "BearerToken",
  "issued_at" : "********",
  "client_id" : "************************",
  "access_token" : "************************",
  "application_name" : "********-****-****-****-********",
  "scope" : "",
  "expires_in" : "1799",
  "refresh_count" : "0",
  "status" : "approved"
}
curl -k -v -X POST "https://*********************/start" -H "accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer *****************" -d "{}"
curl -X POST "https://*********************/start" -H "accept: application/json" -H "Content-Type: application/json" -d "{}"
{
  "fault": {
    "faultstring": "Invalid access token",
    "detail": {
      "errorcode": "oauth.v2.InvalidAccessToken"
    }
  }
}
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value("${swagger.host:}")
    private String swaggerHost;

    @Value("${swagger.basePath:}")
    private String swaggerBasePath;

    @Value("${swagger.oauth2.security.schema:oauth2}")
    private String securitySchemaOAuth2;

    @Value("${swagger.oauth2.token.request.url:}")
    private String oauthTokenRequestURL;

    @Autowired
    ServletContext servletContext;

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .host(swaggerHost)
                .pathProvider(new RelativePathProvider(servletContext) {

                    @Override
                    public String getApplicationBasePath() {
                        return swaggerBasePath;
                    }
                })
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo())
                .securitySchemes(Collections.singletonList(oauth()))
                .securityContexts(Collections.singletonList(securityContext()))
                .useDefaultResponseMessages(false);
    }

    private OAuth oauth() {
        List<AuthorizationScope> authorizationScopeList = new ArrayList<>();

        List<GrantType> grantTypes = new ArrayList<>();
        GrantType creGrant = new ClientCredentialsGrant(oauthTokenRequestURL);

        grantTypes.add(creGrant);

        return new OAuth("oauth2schema", authorizationScopeList, grantTypes);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Blah")
                .description("Blah")
                .version("2019.0.1")
                .contact(new Contact("Blah", "", ""))
                .build();
    }

    private SecurityContext securityContext() {
        return SecurityContext
                .builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex("/.*"))
                .build();
    }

    private List<SecurityReference> defaultAuth() {

        final AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        final AuthorizationScope[] authorizationScopes = new AuthorizationScope[] { authorizationScope };

        return Collections.singletonList(new SecurityReference(securitySchemaOAuth2, authorizationScopes));
    }
}