Spring Boot & Swagger UI. Set JWT token

前端 未结 4 1200
隐瞒了意图╮
隐瞒了意图╮ 2020-12-23 14:57

I have a Swagger config like this

@EnableSwagger2
@Configuration
public class SwaggerConfig {
    @Bean
    public Docket api() {
        List

        
4条回答
  •  攒了一身酷
    2020-12-23 15:10

    For a quick solution, I configured my docket with a global parameter authorization header in my swaggerConfig class.

        @Configuration
        @EnableSwagger2
        public class SwaggerConfig {
          private static final Set DEFAULT_PRODUCES_CONSUMES = new HashSet(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 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);
          }
        }
    

    Wrote a small post authorization-field-in-swagger-ui about this.

提交回复
热议问题