Home > Spring > Spring Boot > Set globally multiple header parameters in Swagger

Set globally multiple header parameters in Swagger

In this article, we will learn how to set multiple header parameters globally in a Spring Boot application.

Why do we need it?

There are cases where we want the client to send multiple header parameters along with the one auth token, these parameters may be useful to validate the token or may be required along with the token to define the request.

How to do it?

First, we will modify our Docket bean as below

@Bean
public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.jkoder.controller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo())
                .securitySchemes(apiKey())
                .securityContexts(Collections.singletonList(securityContext()));
}

Next, we will create one method apiKey() as below, in the method we are creating two more headers along with the one authorization, first one is “user-type” and the second one is “user-id”. It could be anything as per your project requirement.

private List<SecurityScheme> apiKey() {
        List<SecurityScheme> apiKeys =  new ArrayList<>();
        apiKeys.add(new ApiKey("Bearer", HttpHeaders.AUTHORIZATION, "header"));
        apiKeys.add(new ApiKey("user-type", "user-type", "header"));
        apiKeys.add(new ApiKey("user-id", "user-id", "header"));

        return apiKeys;
}

Next, we will create one method apiInfo() as below

private ApiInfo apiInfo() {
    return new ApiInfo(
        "Spring Boot REST API",
        "List of API's for example project.",
        "API TOS",
        "https://jkoder.com/services/",
        new Contact("Jkoder", "https://jkoder.com/", "info@jkoder.com"),
        "License of API", "https://jkoder.com/privacy-policy/", Collections.emptyList());
}

Next, we will create securityContext() method as below

private SecurityContext securityContext() {
    return SecurityContext.builder()
       .securityReferences(defaultAuth())
       .forPaths(PathSelectors.any()).build();
}

Next, we will create defaultAuth() method, mentioned in the above securityContext() method as below

private List<SecurityReference> defaultAuth() {
    AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
    AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
    authorizationScopes[0] = authorizationScope;

    return Arrays.asList(new SecurityReference("Bearer", authorizationScopes)
            , new SecurityReference("user-type", authorizationScopes)
            , new SecurityReference("user-id", authorizationScopes));
}

Now start the spring -boot application, this is the way your swagger will look like

When the right-hand side Authorize button is clicked, it gives the option where you can fill data on headers, which will access globally

That’s it!!!

More Tutorials on Swagger –

How to document Controller and Model class using Swagger2 in Spring Boot REST project

Hide an End-points from Swagger Documentation in Spring Boot REST API

How to Disabled Swagger-UI in Production in Spring Boot Project

Integrate Swagger2 with Spring Boot REST API