微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 open api swagger doc 添加授权标头

如何解决如何使用 open api swagger doc 添加授权标头

我正在尝试在 api 调用添加 Authorization 标头,请问如何使用 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) 
{
    
}

解决方法

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);
    }
}

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

,

添加组件(new Components() .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")));
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。