Ask Your Question
2

How can the Authorize button be enabled for authorization in a Swagger API with springdoc-openapi-starter-webmvc-ui?

asked 2021-09-25 11:00:00 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-08-07 07:00:00 +0000

woof gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-09-25 11:00:00 +0000

Seen: 16 times

Last updated: Aug 07 '21