简体   繁体   中英

How to Activate authorization-Button in swagger-ui with openapi-ui and bearer-token (jwt)?

my task is to enable the authorization-button in the swagger-ui site. I make the documentation of the REST-Controller with Annotations like @Operation or @ApiResponse from the springdoc-openapi-ui - dependency. Now i should enable the button for authorization in the swagger surface.

I have follow introduction: https://www.baeldung.com/spring-boot-swagger-jwt but here i have to include springfox to my maven-dependencies but when i do this i get follow exception:

17.11.2020 19:15:46,664 ERROR [o.s.boot.SpringApplication] (main) Application run 
failed
java.lang.NoClassDefFoundError: org/springframework/data/rest/webmvc/mapping/Associations
at java.base/java.lang.Class.getDeclaredConstructors0(Native Method)
at java.base/java.lang.Class.privateGetDeclaredConstructors(Class.java:3137)
at java.base/java.lang.Class.getDeclaredConstructors(Class.java:2357)
(....)

Additionally the Introduction use apikeys, but we don't use apikeys in the application and I don't know how to use them.

Then I found following question which seems to be my problem: Enable Authorize button in springdoc-openapi-ui for Bearer Token Authentication (JWT)

so i implmented the code from the top-answer:

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

@Configuration
public class OpenApiConfiguration {

  private final String moduleName;
  private final String apiVersion;

  public OpenApiConfiguration(
      @Value("${module-name}") String moduleName,
      @Value("${api-version}") String apiVersion) {
    this.moduleName = moduleName;
    this.apiVersion = apiVersion;
  }

  @Bean
  public OpenAPI customOpenAPI() {
    final String securitySchemeName = "bearerAuth";
    final String apiTitle = String.format("%s API", StringUtils.capitalize(moduleName));

    return new OpenAPI().addSecurityItem(new SecurityRequirement().addList(securitySchemeName))
        .components(new Components().addSecuritySchemes(securitySchemeName,
            new SecurityScheme().name(securitySchemeName)
                .type(SecurityScheme.Type.HTTP)
                .scheme("bearer")
                .bearerFormat("JWT")))
        .info(new Info().title(apiTitle)
            .version(apiVersion));
  }

}

but then i get follow exception:

17.11.2020 19:21:00,215 ERROR [o.s.boot.SpringApplication] (main) Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'openApiConfiguration' defined in file [<PATH>/configuration/OpenApiConfiguration.class]: Unexpected exception during bean creation; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'module-name' in value "${module-name}"
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:530)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
(....)

Can someone explain how this placeholder and @Value works? Maybe anybody has a good/better way for my problem? Im very new in this topic

best regards and sorry for my bad english

ilikefatcats

about the problem: Could not resolve placeholder 'module-name' in value "${module-name}"

Maybe I misunderstood the question but isn't this just missing values for that config? It works if you just use String constants?

Try to add a properties file /yaml on the classpath with these two properties and it should work: module-name=xxx api-version=xxx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM