Spring-boot Swagger2 Sets the global token to resolve the problem that the interface requires token authentication

The problem background

  • Swagger cannot be debugged because the token passed in the header needs to be verified in the project

The specific implementation

@Configuration

@EnableSwagger2

public class SwaggerConfig implements WebMvcConfigurer {



    @Bean

    public Docket createRestApi(a) {

        return new Docket(DocumentationType.SWAGGER_2)

            .apiInfo(apiInfo())

            .select()

            // Class annotated with ApiOperation generates interface documentation

            .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))

            // The interface document is generated for the class under the package

            .paths(PathSelectors.any())

            .build()

            .securitySchemes(security());

    }



    private ApiInfo apiInfo(a) {

        return new ApiInfoBuilder()

            .title("XXXX background Management")

            .description("Interface Document")

            .build();

    }



    private List<ApiKey> security(a) {

        return newArrayList(

            new ApiKey("token"."token"."header")

        );

    }



}

Copy the code

After the configuration is complete, the page is displayed

title

Click the Authorize

The certification page

Fill in your generated token and complete the global token verification problem according to the authorize, and you can happily adjust.