To enable the Authorize button for authorization in a Swagger API with springdoc-openapi-starter-webmvc-ui, follow these steps:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>{version}</version>
</dependency>
@EnableWebSecurity
:@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/**")
.authenticated()
.and()
.httpBasic();
}
}
@SecurityScheme
:@SecurityScheme(
name = "basicAuth",
type = SecuritySchemeType.HTTP,
scheme = "basic")
public class SpringdocOpenApiConfig {
// ...
}
@OpenAPIDefinition
annotation:@OpenAPIDefinition(
security = @SecurityRequirement(
name = "basicAuth",
scopes = {})
)
public class SpringdocOpenApiConfig {
// ...
}
Run your application and navigate to the Swagger UI page. The Authorize button should now be enabled.
Click the Authorize button and enter your username and password in the dialog box that appears.
After clicking the Authorize button, you should be authenticated and authorized to access the protected API resources.
Asked: 2021-09-25 11:00:00 +0000
Seen: 16 times
Last updated: Aug 07 '21