[尝试添加摇摇欲坠的配置以发送带有请求的授权标头后,应用程序无法启动

问题描述 投票:0回答:2

在尝试添加一个swagger配置类后,尝试在本地运行我的API时出现错误,该类允许我发送带有swagger请求的Bearer Token。我已经尝试了所有可能在Google上找到的相关内容,但没有运气。我是个新手,所以非常感谢任何指针。这是我的配置类的样子:

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.ServletContext;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Configuration
@EnableSwagger2
@Import(springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration.class)
public class SwaggerConfig {

    @Bean
    public Docket api(ServletContext servletContext) {
        return new Docket(DocumentationType.SWAGGER_2)
               .securitySchemes(Arrays.asList(apiKey()))
                .securityContexts(Collections.singletonList(securityContext()));
    }

    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("Bearer", authorizationScopes));
    }

    private ApiKey apiKey() {
        return new ApiKey("Bearer", "Authorization", "header");
    }
}

我得到的错误如下:

2020-05-19 17:37:38,437 | WARN  | org.springframework.context.support.AbstractApplicationContext:558 - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'apiModelPropertyPropertyBuilder' defined in URL [jar:file:/C:/Users/jbrooks58/.m2/repository/io/springfox/springfox-swagger-common/2.9.2/springfox-swagger-common-2.9.2.jar!/springfox/documentation/swagger/schema/ApiModelPropertyPropertyBuilder.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'springfox.documentation.spring.web.DescriptionResolver' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in springfox.documentation.swagger.schema.ApiModelPropertyPropertyBuilder required a bean of type 'springfox.documentation.spring.web.DescriptionResolver' that could not be found.


Action:

Consider defining a bean of type 'springfox.documentation.spring.web.DescriptionResolver' in your configuration.

感谢您提供的任何帮助!

java spring-boot swagger swagger-ui springfox
2个回答
0
投票

我假设您在这里使用OAuth2。

您能不能尝试

private ApiKey apiKey() {
        return new ApiKey("JWT", "Authorization", "header");
    }

并删除

.securityContexts(Collections.singletonList(securityContext()))

您会大摇大摆地得到类似的东西

The Authorization Option in Swagger

The Authorization option in Swagger


0
投票

这对我有用,但是您必须将令牌带有Bearer <token>前缀,没有前缀就无法使它起作用。

@Configuration
@EnableSwagger2
public class ApiDocsConfig {

    @Value("${spring.application.name:N/A}")
    private String applicationName;

    @Value("${wind.version:N/A}")
    private String version;

    @Value("${wind.description:N/A}")
    private String description;

    @Value("${wind.developer:N/A}")
    private String developer;

    @Bean
    public Docket apiDocket() {
        HashSet<String> contentType = new HashSet<>();
        contentType.add("application/json");
        return new Docket(DocumentationType.SWAGGER_2)
                .produces(contentType) //Or whatever default value(s)
                .consumes(contentType)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(metadata())
                .securitySchemes(Collections.singletonList(new ApiKey("JWT", "Authorization", "header")))
                .securityContexts(
                        Collections.singletonList(
                                SecurityContext.builder().securityReferences(
                                        Collections.singletonList(SecurityReference.builder().reference("JWT").scopes(new AuthorizationScope[0]).build())
                                ).build()
                        )
                );
    }

    private ApiInfo metadata() {
        return new ApiInfoBuilder()
                .title(this.applicationName)
                .description(this.description)
                .version(this.version)
                .contact(new Contact(null, null, this.developer))
                .build();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.