简体   繁体   English

如何使用 openapi-ui 和承载令牌 (jwt) 在 swagger-ui 中激活授权按钮?

[英]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.我的任务是在 swagger-ui 站点中启用授权按钮。 I make the documentation of the REST-Controller with Annotations like @Operation or @ApiResponse from the springdoc-openapi-ui - dependency.我使用 springdoc-openapi-ui - 依赖项中的 @Operation 或 @ApiResponse 等注释制作了 REST-Controller 的文档。 Now i should enable the button for authorization in the swagger surface.现在我应该在 swagger 表面启用授权按钮。

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:我有以下介绍: https ://www.baeldung.com/spring-boot-swagger-jwt 但在这里我必须将 springfox 包含到我的 maven 依赖项中,但是当我这样做时,我得到了以下异常:

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.另外介绍使用apikeys,但我们不在应用程序中使用apikeys,我不知道如何使用它们。

Then I found following question which seems to be my problem: Enable Authorize button in springdoc-openapi-ui for Bearer Token Authentication (JWT)然后我发现了以下问题,这似乎是我的问题: 在 springdoc-openapi-ui 中启用授权按钮以进行不记名令牌身份验证 (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?有人可以解释这个占位符和@Value 是如何工作的吗? 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}"关于问题:无法解析值“${module-name}”中的占位符“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尝试使用这两个属性在类路径上添加一个属性文件 /yaml ,它应该可以工作:module-name=xxx api-version=xxx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用最新的 OpenAPI / Swagger UI 的 Bearer JWT 令牌授权 (401) 问题 | 弹簧靴 - Issue with Authorization (401) with Bearer JWT token using latest OpenAPI / Swagger UI | Spring Boot springdoc-openapi-ui生成的swagger-ui中如何更改请求header中授权key的名称 - How to change the name of the authorization key in the request header in the swagger-ui generated by springdoc-openapi-ui Quarkus Swagger-UI 授权 - Quarkus Swagger-UI Authorization 在 springdoc-openapi-ui 中为 Bearer Token Authentication (JWT) 启用授权按钮 - Enable Authorize button in springdoc-openapi-ui for Bearer Token Authentication (JWT) Swagger openapi:openapi.yaml/swagger-ui 中的过滤方法 - Swagger openapi: Filter methods in openapi.yaml/ swagger-ui OpenAPI / Swagger-UI注释和@BeanParam - OpenAPI/Swagger-UI Annotations & @BeanParam 如何在 swagger-ui 上显示“/oauth/token”端点? - How to display “/oauth/token” endpoint on swagger-ui? 如何在 java EE 应用程序中将授权按钮添加到 swagger-ui - How adding Authorize Button to swagger-ui in java EE application 无法使用 Springfox 2.9.2 从 Swagger-UI 发送授权令牌 - Cannot send Authorization token from Swagger-UI using Springfox 2.9.2 是否有任何选项可以为 /swagger-ui 路径添加前缀? 开放API - Is there any option to add a prefix for /swagger-ui path? OpenApi
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM