I was setting up JWT authentication for APIs of my spring boot app and wanted to test them from swagger-ui, but could not find a direct answer in my google searches. So adding my tweaks to get this done, as an entry here.

The quickest way to get this done is to configure a global parameter in the Docket bean, which will show up on all APIs. I have added my swagger config here.

@Configuration
@EnableSwagger2
public class SwaggerConfig {
  private static final Set<String> DEFAULT_PRODUCES_CONSUMES = new HashSet<String>(Arrays.asList("application/json"));

  @Bean
  public Docket api() {
    ParameterBuilder parameterBuilder = new ParameterBuilder();
    parameterBuilder.name("Authorization")
		    .modelRef(new ModelRef("string"))
		    .parameterType("header")
		    .description("JWT token")
		    .required(true)
		    .build();
    List<Parameter> parameters = new ArrayList<>();
    parameters.add(parameterBuilder.build());
    return new Docket(DocumentationType.SWAGGER_2).apiInfo(DEFAULT_API_INFO)
	    .produces(DEFAULT_PRODUCES_CONSUMES)
	    .consumes(DEFAULT_PRODUCES_CONSUMES)
	    .select()
	    .build()
	    // Setting globalOperationParameters ensures that authentication header is applied to all APIs
	    .globalOperationParameters(parameters);
  }
}
Code Snippet 1: swagger-configuration-for-auth-header

With the above changes, you should be good to go and your swagger-ui should display the authorization header as shown in Figure 1.

Figure 1: Authorization header in swagger

Figure 1: Authorization header in swagger

If your authentication mechanism expects the string bearer, the simplest solution is to pre-pend the api token with Bearer while entering the authorization header, i.e., Bearer xyzApiTokenabcsd.

Figure 2: Prepending Bearer

Figure 2: Prepending Bearer