How to get api-key security schema working #2809
-
|
May a stupid question but I can't find a docu or howto or something. I have already this: I can use the schema in swagger ui: But then when I send requests ... the header is not in requests. I checked with chrome network console. Where is the missing magic? Kind regards |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
We used something like this and it works: `@Configuration }` |
Beta Was this translation helpful? Give feedback.
-
|
I will re-test this asap. |
Beta Was this translation helpful? Give feedback.
-
|
Configure api-key security in import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class OpenApiConfig {
@Bean
public OpenAPI customOpenAPI() {
return new OpenAPI()
.components(new Components()
.addSecuritySchemes("ApiKey", new SecurityScheme()
.type(SecurityScheme.Type.APIKEY)
.in(SecurityScheme.In.HEADER)
.name("X-API-Key")
.description("API key for authentication")))
.addSecurityItem(new SecurityRequirement().addList("ApiKey"));
}
}To apply it to specific endpoints only (not globally): @Operation(security = { @SecurityRequirement(name = "ApiKey") })
@GetMapping("/secure")
public String secureEndpoint() {
return "authenticated";
}To exclude certain endpoints from requiring the API key: springdoc:
paths-to-exclude: /api/public/**
# Or use @SecurityRequirement with empty list on public endpoints:
# @Operation(security = {})For header-based auth with a custom filter, ensure your security filter extracts the |
Beta Was this translation helpful? Give feedback.

Configure api-key security in
springdoc-openapiwith aSecuritySchemebean: