For work, I'm working on a Java project where we need to pass an authorization token to each request header.
In my SwaggerConfig.java
file, I have the following:
@Override
public Docket createDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(getApiInfo())
...
.build()
.securitySchemes(Arrays.asList(apiKey()));
}
private ApiKey apiKey() {
return new ApiKey("Authorization", "api_key", "header");
}
This enables an Authorize
button which I can click:
And I can paste the token in here:
...which inserts the token into the header for each request.
However, there doesn't exist any documentation in the Swagger Docs or examples elsewhere on how I could automatically insert an auth token. The goal is to retrieve an auth token by calling an internal API (which uses active directory to authenticate) and automatically apply that token to the request headers.
I could probably extract the Swagger HTML/JS and modify the swagger-ui.html
page or the JavaScript to automatically call the API and fill the dialog box with the token, but the downside with that approach is Swagger can't easily be updated if I directly modify those files.
Is there a Java way to insert a custom API key into the request headers?