springdoc / springdoc-openapi

Library for OpenAPI 3 with spring-boot
https://springdoc.org
Apache License 2.0
3.3k stars 501 forks source link

How to set global header in swagger-ui.html , never invisible or not submit when try #2779

Open farsunset-com opened 2 weeks ago

farsunset-com commented 2 weeks ago

srpingboot 3.2.5 springdoc 2.6.0

like swagger2

private List<SecurityScheme> securitySchemes() {
    List<SecurityScheme> schemeList = new ArrayList<>();
    schemeList.add(new ApiKey(Common.MVC_HEADER_TOKEN, Common.MVC_HEADER_TOKEN, "header"));
    return schemeList;
}
farsunset-com commented 2 weeks ago

The document is not very clear, this is work fine

@Bean
    public OpenAPI springDocOpenAPI() {
        OpenAPI api = new OpenAPI();

        api.components(new Components().addSecuritySchemes("x-token", new SecurityScheme()
                .type(SecurityScheme.Type.APIKEY)
                .in(SecurityScheme.In.HEADER)
                .name("x-token")
                .description("用户Token")));

        api.info(new Info()
                .title("Service APIs.")
                .description("系统业务接口服务")
                .version("3.0.0")
        );
        api.addSecurityItem(new SecurityRequirement().addList("x-token"));

        return api;

    }