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

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

在swagger-ui中,我想在请求头中添加授权令牌。

我几乎看遍了这里和其他网站的所有帖子。仍然不能使它像预期的那样工作。我尝试了不同的解决方案,但没有任何改变。

我现在的实现是根据 今春号:

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>

结果总是一样的。

Authorize from swagger-uicurl post

一些解决方案,已经被指出工作,我试过了。

https:/github.comspringfoxspringfoxissues2661。,https:/github.comspringfoxspringfoxissues2194。,https:/stackoverflow.coma587200777831244。,https:/stackoverflow.coma528688537831244。.

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

你可以设置 Authorization 头部至 docket 使用 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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.