在swagger-ui中,我想向请求标头添加授权令牌。
我几乎在这里和其他网站上浏览了所有帖子。仍然无法使其按预期工作。我尝试了不同的解决方案,但没有任何改变。
我当前根据this offical spring issue的实现:
XController.java:

@ApiOperation(
            value = "Yeni Üye Tanımlama Servisi",
            notes = "Platformlar tarafından iletilen üye bilgilerinin kaydedilmesini sağlayan servistir.",
            response = KfsResponse.class
            , authorizations = { @Authorization(value="Authorization") })

SwaggerConfig.java:
 @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(Predicates.or(RequestHandlerSelectors.basePackage("tr.com.mkk.kfs.kfs.web"), RequestHandlerSelectors.basePackage("tr.com.mkk.kfs.kfs.model.infos")))
                .paths(PathSelectors.any()).build()
                .apiInfo(apiEndPointsInfo())
                .securitySchemes(Arrays.asList(apiKey()))
                .securityContexts(Arrays.asList(securityContext));

    }

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

    SecurityReference securityReference = SecurityReference.builder()
            .reference("Authorization")
            .scopes(new AuthorizationScope[0])
            .build();

    SecurityContext securityContext = SecurityContext.builder()
            .securityReferences(Arrays.asList(securityReference))
            .build();

pom.xml:
<springfox.version>2.9.2</springfox.version>

<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${springfox.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${springfox.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-bean-validators</artifactId>
            <version>${springfox.version}</version>
        </dependency>

结果总是相同的:
java - 无法使用Springfox 2.9.2从Swagger-UI发送授权 token-LMLPHP
java - 无法使用Springfox 2.9.2从Swagger-UI发送授权 token-LMLPHP
一些解决方案,已指出工作,我尝试过:
https://github.com/springfox/springfox/issues/2661
https://github.com/springfox/springfox/issues/2194
https://stackoverflow.com/a/58720077/7831244
https://stackoverflow.com/a/52868853/7831244

最佳答案

您可以使用Authorizationdocket标头设置为ParameterBuilder,如下所示

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

    @Value("${title:title}")
    private String title;
    @Value("${description:description")
    private String description;
    @Value("${version:0.0.1}")
    private String version;

    ApiInfo apiInfo() {
        return new ApiInfoBuilder().title(title).description(description).version(version).build();
    }

    @Bean
public Docket api() {

        Docket docket;

        docket = new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.basePackage("com.controller")).paths(PathSelectors.any()).build()
                .apiInfo(apiInfo());


        docket.globalOperationParameters(
                Arrays.asList(
                new ParameterBuilder().name("Authorization")
                        .description("Authorization details for security (JWT token or BasicAuth)")
                        .modelRef(new ModelRef("String")).parameterType("header").required(false).build()));

        return docket;
    }
}

10-07 17:12