Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
Springdoc especifica la seguridad como abierta

Estoy usando Springdoc ( 1.4.8 ) para documentar nuestra API Rest. El 99% de las llamadas están protegidas con un token de portador OAuth2, por lo que me gustaría agregarlo de manera predeterminada de esta manera:

 new OpenAPI() .components(new Components() .addSecuritySchemes("bearerTokenScheme", new SecurityScheme() .type(SecurityScheme.Type.HTTP) .name("bearerTokenScheme") .scheme("Bearer") .bearerFormat("JWT") ) ) .addSecurityItem(new SecurityRequirement() .addList("bearerTokenScheme") )

Esto funciona bien para tenerlo especificado de forma predeterminada, pero mi pregunta ahora es, ¿puedo anularlo de alguna manera especificando que una sola llamada no sea segura?

Me doy cuenta de que omitir .addSecurityItem() mostrará las llamadas como no seguras de forma predeterminada, pero me gustaría tenerlas protegidas de forma predeterminada y solo tener que anularlas cuando no lo estén.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Tuve un problema similar y lo resolví usando un complemento. El complemento anula el valor de security de los controladores o métodos etiquetados y lo establece en una lista vacía. Por lo tanto, los puntos finales etiquetados permanecen sin protección.

 @Component public class NoSecurityPlugin implements OpenApiCustomiser { private static final List<Function<PathItem, Operation>> OPERATION_GETTERS = Arrays.asList( PathItem::getGet, PathItem::getPost, PathItem::getPut, PathItem::getDelete, PathItem::getHead, PathItem::getOptions, PathItem::getPatch); private static final List<String> tagsForNoSecurity = List.of( "public", "unsecured" ); @Override public void customise(OpenAPI openApi) { openApi.getPaths().forEach((path, item) -> getOperations(item).forEach(api -> { List<String> tags = api.getTags(); if(tags != null && tags.stream().anyMatch(tagsForNoSecurity::contains)) { api.setSecurity(Collections.emptyList()); } })); } private static Stream<Operation> getOperations(PathItem pathItem) { return OPERATION_GETTERS.stream() .map(g -> g.apply(pathItem)) .filter(Objects::nonNull); } }

Etiquete sus métodos API (o Controller ):

 @Tag(name = "public", description = "Public APIs")
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!