Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To enable the Authorize button for authorization in a Swagger API with springdoc-openapi-starter-webmvc-ui, follow these steps:

  1. Add the springfox-swagger-ui dependency to your pom.xml file:
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>{version}</version>
</dependency>
  1. Enable the security configuration using @EnableWebSecurity:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .antMatchers("/api/**")
            .authenticated()
            .and()
            .httpBasic();
    }
}
  1. Add the security scheme to your Swagger document using @SecurityScheme:
@SecurityScheme(
    name = "basicAuth",
    type = SecuritySchemeType.HTTP,
    scheme = "basic")
public class SpringdocOpenApiConfig {
    // ...
}
  1. Add the security reference to the @OpenAPIDefinition annotation:
@OpenAPIDefinition(
    security = @SecurityRequirement(
        name = "basicAuth",
        scopes = {})
)
public class SpringdocOpenApiConfig {
    // ...
}
  1. Run your application and navigate to the Swagger UI page. The Authorize button should now be enabled.

  2. Click the Authorize button and enter your username and password in the dialog box that appears.

  3. After clicking the Authorize button, you should be authenticated and authorized to access the protected API resources.