简体   繁体   English

无法使用 Springfox 2.9.2 从 Swagger-UI 发送授权令牌

[英]Cannot send Authorization token from Swagger-UI using Springfox 2.9.2

In swagger-ui, I want to add authorization token to request header.在 swagger-ui 中,我想添加授权令牌以请求 header。

I almost looked at every post in here and other sites.我几乎看了这里和其他网站上的每一篇文章。 Still cannot make it work as expected.仍然无法使其按预期工作。 I tried different solutions but nothing changed.我尝试了不同的解决方案,但没有任何改变。

My current implementation according to this offical spring issue :根据这个官方 spring 问题,我当前的实现是:

XController.java: 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:招摇配置.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: 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>

Result is always the same:结果总是一样的:

从 swagger-ui 授权 卷曲帖子

some solutions, has been pointed out working, I tried:一些解决方案,已经指出有效,我试过了:

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 . https://github.com/springfox/springfox/issues/2661 , https://github.com/springfox/springfox/issues/2194 , https://stackoverflow.com/a/58720077/7831244 , 882070588335 stackoverflow.com/a/52868853/7831244

You can set Authorization header to docket using ParameterBuilder as shown below您可以使用ParameterBuilderAuthorization header 设置为docket ,如下所示

@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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM