0

I want to achieve the authorization button in Swagger. For the older version, there were some configurations for Swagger, but I guess those configurations are not needed in the newer version. Added a picture that I want to achieve. I know what Basic auth but my requirement is a header i.e authorization of the bearer and token. Thanks in advance for the help.

enter image description here

dur
  • 15,689
  • 25
  • 79
  • 125
  • Check this link https://stackoverflow.com/questions/59357205/springdoc-openapi-apply-default-global-securityscheme-possible – Ret Ret Aug 25 '23 at 10:44
  • Check this link [https://stackoverflow.com/questions/59357205/springdoc-openapi-apply-default-global-securityscheme-possible](https://stackoverflow.com/questions/59357205/springdoc-openapi-apply-default-global-securityscheme-possible). – Ret Ret Aug 26 '23 at 01:27

2 Answers2

2

Added

@SecurityScheme(
    name = "bearerAuth",
    scheme = "bearer",
    bearerFormat = "JWT", 
    type = SecuritySchemeType.HTTP, 
    in = SecuritySchemeIn.HEADER
)

This configuration is in the main class.

Add @SecurityRequirement(name = "bearerAuth")

It is in the Controller class, or you can add it at your endpoints, which works for me.

Leonardo
  • 1,263
  • 7
  • 20
  • 51
1

You probably have a @Configuration class with OpenApi information. There, you should configure your OpenAPI() instance with the following:

new OpenAPI().
//... whatever configuration you have
    .components(
        new Components().addSecuritySchemes(
            "api",
            new SecurityScheme()
                .scheme("bearer")
                .type(SecurityScheme.Type.HTTP)
                .bearerFormat("jwt") //if it is your case
                .name("api") 
        )
    )

Sorry I'm translating it from Kotlin, but you got the Java idea :)

Leonardo
  • 1,263
  • 7
  • 20
  • 51