【问题标题】:how to add Authorization header by using open api swagger doc如何使用 open api swagger doc 添加授权标头
【发布时间】:2021-01-21 06:23:23
【问题描述】:

我正在尝试在 api 调用中添加授权标头,请您告诉如何使用 open api swagger doc 进行配置。

代码:

@Bean
public OpenAPI customOpenAPI(@Value("${application-description}") String appDesciption, @Value("${application-version}") String appVersion) {
    return new OpenAPI()
            .info(new Info()
                    .title("Access Management APIs")
                    .version("1.0")
                    .description("Access Management APIs for tp db")
                    .termsOfService("http://swagger.io/terms/")
                    .license(new License().name("Apache 2.0").url("http://springdoc.org")));
}

控制器类:

@PutMapping(value = "/{aNumber}",
            produces = APPLICATION_JSON_VALUE)
public ResponseEntity<Object> updateSubsidyAward(@Valid @RequestBody UpdateAwardDetailsRequest awardUpdateRequest,
                                                 @PathVariable("aNumber") Long aNumber) 
{
    
}

【问题讨论】:

    标签: swagger swagger-ui


    【解决方案1】:

    OpenApiConfig

    package com.user.demoapp.config;
    
    import io.swagger.v3.oas.models.Components;
    import io.swagger.v3.oas.models.OpenAPI;
    import io.swagger.v3.oas.models.info.Contact;
    import io.swagger.v3.oas.models.info.Info;
    import io.swagger.v3.oas.models.info.License;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class OpenApiConfig {
    
        @Bean
        public OpenAPI customOpenAPI() {
            return new OpenAPI()
                    .components(new Components())
                    .info(new Info()
                            .title("App_name")
                            .description("App_description")
                            .version("0.0.1")
                            .termsOfService("http://swagger.io/terms/")
                            .contact(new Contact().name("contact_name").email("contact_email_address"))
                            .license(new License().name("Apache 2.0").url("http://springdoc.org"))
    
    
                    );
        }
    }
    
    
    

    控制器类

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @Tag(name = "api_title", description = "api_description")
    @RestController
    public class TestController {
        @Autowired
        private TestService testService;
    
        @Operation(summary = "api_summary ", description = "api_description", tags = { "api_Tag" })
        @PostMapping(path = "/test-api")
        public ResponseEntity<ApiResponseDTO> addTest(@RequestBody List<testDTO> testModel) {
            return testService.addtest(testModel);
        }
    }
    

    我希望你找到了你要找的东西:)

    【讨论】:

    • 感谢您的快速回复。我会尽力让你知道是否有任何问题
    • 我在控制器中使用了 @CrossOrigin(allowCredentials = "true", allowedHeaders = "Authhorization,UserPrinciple") 并重新启动了我的 Spring Boot 应用程序,但我仍然无法在招摇中看到任何选项输入授权标头?
    【解决方案2】:

    添加组件(新组件() .addSecuritySchemes())

    @Bean
    public OpenAPI customOpenAPI(@Value("${application-description}") String appDesciption, @Value("${application-version}") String appVersion) {
    
        final String securitySchemeName = "basicAuth";
    
            return new OpenAPI()
                    .components(new Components()
                            .addSecuritySchemes(securitySchemeName, new SecurityScheme()
                                    .type(SecurityScheme.Type.HTTP)
                                    .scheme("basic"))
                    )
                    .info(new Info()
                        .title("Access Management APIs")
                        .version("1.0")
                        .description("Access Management APIs for tp db")
                        .termsOfService("http://swagger.io/terms/")
                        .license(new License().name("Apache 2.0").url("http://springdoc.org")));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-15
      • 2017-04-24
      相关资源
      最近更新 更多