I am facing an issue that authorization header is not added while accessing api via openapi swagger.
I have 3 microservice and an API gateway. Each microservice use token based authentication and use jwt authentication. Each microservice is accessing api with jwt token using swagger easily and Its working fine. Here is the code for token authentication in each microservice
Following are the added in main class of application and by this swagger UI show Authorize button. Using Authorize button, I am entering JWT token easily and it api is working fine also.
@OpenAPIDefinition(info = @Info(title = Backend Core API", version = "1.0", description = "Payment Application"))
@SecurityScheme(name = "PAYMENTSERVICE", scheme = "Bearer", type = SecuritySchemeType.HTTP, in = SecuritySchemeIn.HEADER)
And following is added in each controller of service
@SecurityRequirement(name = "PAYMENTSERVICE")
And also, added following in the YML file
springdoc:
api-docs:
path: /v3/api-docs/swagger-config
swagger-ui:
path: /swagger-ui.html
Now I am added following in api gateway to access all microservice api's here.
I am adding two dependency.
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>${springdoc-openapi.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
and following config class
@Configuration
@Primary // register custom configuration first to avoid registration conflict with inMemorySwaggerResourcesProvider
public class SwaggerConfig implements SwaggerResourcesProvider {
public static final String API_URI = "/v3/api-docs/swagger-config"; // OpenApi description default URI
private final RouteDefinitionLocator routeLocator; // Gateway locator
public SwaggerConfig(RouteDefinitionLocator routeLocator) {
this.routeLocator = routeLocator;
}
/**
* create Swagger resource registration details.
* @return List
*/
@Override
public List<SwaggerResource> get() {
List<SwaggerResource> resources = new ArrayList<>(); // declare & initialize swagger resource list
routeLocator.getRouteDefinitions().subscribe( // subscribe to Gateway locator definitions to monitor locator definition changes
routeDefinition -> { // for each route definition
String resourceName = routeDefinition.getId(); // assign the route ID to resourceName
String location = routeDefinition.getPredicates().get(0).getArgs().get("_genkey_0") // get the route address (e.g., http://address:port/
.replace("/**", API_URI); // replace predicate extensions with /v3/api-docs. The resulted location will become: http://address:port/v3/api-docs
resources.add(swaggerResource(resourceName, location)); // add the route definition as a Swagger resource
}
);
return resources;
}
/**
* Swagger resource registration.
* @param name
* @param location
* @return SwaggerResource
*/
private SwaggerResource swaggerResource(String name, String location) {
SwaggerResource swaggerResource = new SwaggerResource(); // initialize a swaggerResource pointer
swaggerResource.setName(name); // set swaggerResource name to the given name
swaggerResource.setLocation(location); // set swaggerResource location to the given location (e.g., http://address:port/v3/api-docs)
swaggerResource.setSwaggerVersion("2.0"); // set swaggerResource version.
return swaggerResource;
}
}
And also added following in the main class,
@OpenAPIDefinition(info = @Info(title = "APIGateway Backend Core API", version = "1.0", description = "API Gateway Application"))
API gateway swagger UI is working fine. It shows all microservice in the drop down. I select one microservice and add JWT token in Authorization header(By clicking Authorize button which shows a dialog box in which I enter JWT token as a value and hit authorize button).
Now, When I execute a get api, then it gives an error unauthorize or sometime it gives Failed to fetch. following is the code snippet for error.
Here We can easily see, authorization header is missing in the curl. Kindly help me to resolve this issue.
Thanks in advance.