Springfox: Spring Boot 2.X with Swagger ApiKey Security

Created on 19 Jun 2018  路  3Comments  路  Source: springfox/springfox

Please take the time to search the repository, if your question has already been asked or answered.
Currently am working with Spring boot 2.0.1 with Swagger 2.8.0 version, I've followed below sample documentation completely . I could see the ApiKey for adding the token but when am giving request from the Swagger UI. it is not sending the Apikey through Header. How to resolve this issue.

find the attached screen shots.
1
2

question

Most helpful comment

Hi @dilipkrish I've Restructure the modified Code in below attached manner, now it's started working as expected.

@Configuration
public class SwaggerConfiguration extends WebMvcConfigurationSupport {

    @Bean
    public Docket appApi() {
        AuthorizationScope[] authScopes = new AuthorizationScope[1];
        authScopes[0] = new AuthorizationScopeBuilder().scope("global").description("full access").build();
        SecurityReference securityReference = SecurityReference.builder().reference("Authorization-Key")
                .scopes(authScopes).build();

        ArrayList<SecurityContext> securityContexts = newArrayList(
                SecurityContext.builder().securityReferences(newArrayList(securityReference)).build());
        return new Docket(DocumentationType.SWAGGER_2)
                .securitySchemes(newArrayList(new ApiKey("Authorization-Key", "Authorization", "header")))
                .securityContexts(securityContexts).apiInfo(apiInfo()).select().build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("Swagger API Security REST API")
                .description("\"Swagger API Security REST API\"").version("1.0.0").build();
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

All 3 comments

I think you need to upgrade to 2.9.0

Hi @dilipkrish I've Restructure the modified Code in below attached manner, now it's started working as expected.

@Configuration
public class SwaggerConfiguration extends WebMvcConfigurationSupport {

    @Bean
    public Docket appApi() {
        AuthorizationScope[] authScopes = new AuthorizationScope[1];
        authScopes[0] = new AuthorizationScopeBuilder().scope("global").description("full access").build();
        SecurityReference securityReference = SecurityReference.builder().reference("Authorization-Key")
                .scopes(authScopes).build();

        ArrayList<SecurityContext> securityContexts = newArrayList(
                SecurityContext.builder().securityReferences(newArrayList(securityReference)).build());
        return new Docket(DocumentationType.SWAGGER_2)
                .securitySchemes(newArrayList(new ApiKey("Authorization-Key", "Authorization", "header")))
                .securityContexts(securityContexts).apiInfo(apiInfo()).select().build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("Swagger API Security REST API")
                .description("\"Swagger API Security REST API\"").version("1.0.0").build();
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

Hi, how to add a description on the popup swagger UI like used bearer token format etc..

Was this page helpful?
0 / 5 - 0 ratings