Skip to content

Disable security for one operation #259

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
dlamoris opened this issue Dec 12, 2019 · 7 comments
Closed

Disable security for one operation #259

dlamoris opened this issue Dec 12, 2019 · 7 comments
Labels
enhancement New feature or request

Comments

@dlamoris
Copy link

Hi,
I have a @OpenAPIDefinition with security defined for the whole app, but I want to override and disable security for one method, how can I do that?
Using @Operation(security = {}) on the method doesn't seem to work.
Expected output in yaml is security: [] for that one operation.

Thanks

@springdoc
Copy link
Collaborator

Hi,

Can you please add a sample code to reproduce your issue ?

@dlamoris
Copy link
Author

application annotations:

@SpringBootApplication(scanBasePackages = "example")
@OpenAPIDefinition(
    info = @Info(
       ...
    ),
    servers = {
    },
    security = {@SecurityRequirement(name = "bearerToken")}
)
@SecurityScheme(
    name = "bearerToken",
    type = SecuritySchemeType.HTTP,
    scheme = "bearer",
    bearerFormat = "JWT"
)
public class ExampleApplication {
   public static void main(String[] args) {
        SpringApplication.run(ExampleApplication.class, args);
    }
}

annotation on controller method:

@RestController
public class AuthenticationController {

    @PostMapping(value = "/login", consumes = MediaType.APPLICATION_JSON_VALUE)
    @Operation(security = {})
    public JwtAuthenticationResponse createAuthenticationToken(
        @RequestBody JwtAuthenticationRequest authenticationRequest) {
   }
}

@dlamoris dlamoris removed their assignment Dec 13, 2019
@springdoc
Copy link
Collaborator

Hi,

First of all, please note that we rely on swagger-core official annotations / jars.
There is already an open issue related to your expected behaviour: You can submit your comments here instead.

You have another option, which is to a add the security annotations for the secured operations only.

@RestController
@RequestMapping(path = "/demo2",
	produces = MediaType.TEXT_PLAIN_VALUE)
@SecurityScheme(
		name = "bearerToken",
		type = SecuritySchemeType.HTTP,
		scheme = "bearer",
		bearerFormat = "JWT"
)
public class DemoController {

	@PostMapping(value = "/login1", consumes = MediaType.APPLICATION_JSON_VALUE)
	@Operation(summary = "Add a new person to the store", description = "", security = {
			@SecurityRequirement(name = "bearerToken")})
	public Object createAuthenticationToken(
			@RequestBody String authenticationRequest) {
		return null;
	}

	@PostMapping(value = "/login3", consumes = MediaType.APPLICATION_JSON_VALUE)
	@Operation(description =  "hello, no security")
	public Object createAuthenticationToken2(
			@RequestBody String authenticationRequest) {
		return null;
	}
}

@mafor
Copy link

mafor commented Dec 30, 2019

Hi,

I came across this issue recently. I found a workaround using OpenApiCustomiser, but I would prefer a proper solution. I think it is doable without changes to the swagger-core annotations (not with the Operation annotation, but with an 'empty' SecurityRequirements). It would look like this:

@RestController
public class AuthenticationController {

    @PostMapping(value = "/login", consumes = MediaType.APPLICATION_JSON_VALUE)
    @SecurityRequirements(value = {}) // <- or without 'value', added for clarity
    public JwtAuthenticationResponse createAuthenticationToken(
        @RequestBody JwtAuthenticationRequest authenticationRequest) {
   }
}

It doesn't work out of the box, there are some changes needed in the SecurityParser
Would you mind if I sent you a PR?

@bnasslahsen
Copy link
Collaborator

@mafor, your PR is welcome.

@mafor
Copy link

mafor commented Jan 30, 2020

Hi @bnasslahsen. Forget it, I see you've done it yourself already.

@bnasslahsen
Copy link
Collaborator

Yeah @mafor, i wasn't sure about your feedback. Anyway, the important is that its now shared with the community.

@bnasslahsen bnasslahsen added the enhancement New feature or request label Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants