Skip to content

authorization header didn't exist in requests #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
okosiuta opened this issue Aug 20, 2019 · 10 comments
Closed

authorization header didn't exist in requests #49

okosiuta opened this issue Aug 20, 2019 · 10 comments
Labels
question Further information is requested

Comments

@okosiuta
Copy link

Hi,
I tried to add the bearer token to all my swagger's UI requests in the next way:

@Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI().components(new Components()
                .addSecuritySchemes("BEARER KEY", new SecurityScheme().type(SecurityScheme.Type.HTTP)
                        .scheme("bearer").in(SecurityScheme.In.HEADER)));
    }

Then after my application started I pass the bearer token into an authentication form with swagger UI:

Bearer

Actual result:

curl -X GET "http://localhost:8080/api/v1/contacts/employees?query=name" -H "accept: */*"

Bearer token didn't exist in the request.

Expected result:

curl -X GET "http://localhost:8080/api/v1/contacts/employees?query=name" -H "accept: */*" -H "Authorization: Bearer ***********"

Bearer token exists in the request.

Could you please clarify, what I'm doing wrong?
Thank you!

@springdoc
Copy link
Collaborator

Hi,

You should add the @securityrequirement tags to your protected APIs.
For example:

@Operation(security = { @SecurityRequirement(name = "bearer-key") })

And the security definition sample:

@Bean
 public OpenAPI customOpenAPI() {
   return new OpenAPI().components(new Components().addSecuritySchemes("bearer-key",
     new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT")));
}

@MugdhaB
Copy link

MugdhaB commented Jan 2, 2020

Extending the question. What if the authentication method applies to all the APIs? Is there a way to say that the scheme applies to all the APIs?

@ricoatapex
Copy link

I'm running into this exact issue, and even adding the "SecurityRequirement" and "SecurityScheme" above, it's not sending the "authorization" field in the header. Does anyone have the full code definition for this? This is what I'm using, very straightforward, on my operation (after declaring the Security Schemes in "customOpenAPI"

@operation(
security={ @securityrequirement(name="bearerToken") },
parameters={
@parameter(
name="authorization",
in=ParameterIn.HEADER,
required=true
)
)
@GetMapping(value="/someFunctionCall")
public ResponseEntity<?> someFunctionCall(String authorization) {
return adminService.someFunctionCall(null);
}

@vladimirevterev
Copy link

Hi, @MugdhaB

use addSecurityItem method of global OpenAPI config bean to set SecurityRequirement globally

@q23qweliuhan
Copy link

I'm running into this exact issue, and even adding the "SecurityRequirement" and "SecurityScheme" above, it's not sending the "authorization" field in the header. Does anyone have the full code definition for this? This is what I'm using, very straightforward, on my operation (after declaring the Security Schemes in "customOpenAPI"

@operation(
security={ @securityrequirement(name="bearerToken") },
parameters={
@parameter(
name="authorization",
in=ParameterIn.HEADER,
required=true
)
)
@GetMapping(value="/someFunctionCall")
public ResponseEntity<?> someFunctionCall(String authorization) {
return adminService.someFunctionCall(null);
}

I've run into the same issue, do you maybe found the solution already?

@bnasslahsen
Copy link
Collaborator

@q23qweliuhan,

The behaviour you are describing is not related to springdoc-openapi. But to swagger-ui:
swagger-api/swagger-ui#5715

The OpenAPI 3 specification does not allow explicitly adding Authorization header. For more information, please read:

Note: Header parameters named Accept, Content-Type and Authorization are not allowed. To describe these headers

https://swagger.io/docs/specification/describing-parameters/#header-parameters.

@q23qweliuhan
Copy link

q23qweliuhan commented Mar 22, 2020

Hi @bnasslahsen,

Thanks for your reply!
I've changed it as below now:
@Operation(summary = "User logout - remove JWT from whitelist", security = {@SecurityRequirement(name = "bearer-key")})
public ResponseEntity logout(@RequestHeader(name = "Authorization") String authorization)
With these, I can get the token from the security now.

However, in the created api-docs, it still contains the Authorization as header parameters, also shown on the swagger ui.
image

Any idea what is causing this?

@bnasslahsen
Copy link
Collaborator

@okosiuta,

You can hide it using @Parameter(hidden = true)

@Operation(summary = "User logout - remove JWT from whitelist",security = { @SecurityRequirement(name = "bearer-key") })
public String logout(@Parameter(hidden = true) @RequestHeader(name = "Authorization") String authorization) {...}

If it makes sense, we might ignore Authorization as header parameters from the generated api-docs by default, on springdoc-openapi.
If you think its releavant, you can create a feature request for that.

@q23qweliuhan
Copy link

@Parameter(hidden = true)

I see, thanks!

@farsunset-com
Copy link

你好,@MugdhaB

使用全局 OpenAPI 配置 bean 的 addSecurityItem 方法全局设置 SecurityRequirement

shit. Copy the token every time for try?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

7 participants