Answer the question
In order to leave comments, you need to log in
How to properly configure SwaggerUI for JWT token?
I have a REST service, one endpoint of which is protected by a token. There is also a Swagger-UI page.
When I want to use a token, I just enter it:
Accordingly, the whole problem is that the request turns out to be incorrect. The token looks like this: Authorization: Bearer <token>
.
And here is what I get:
Here is the CURL that came out:
curl -X GET "http://localhost:8080/VaultDairy/entry/criteria" -H "accept: */*" -H "Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsidGVzdGp3dHJlc291cmNlaWQiXSwidXNlcl9uYW1lIjoic3VwZXItdXNlckBhZG1pbiIsInNjb3BlIjpbInJlYWQiLCJ3cml0ZSJdLCJleHAiOjE1NTEyMjk3NTcsImF1dGhvcml0aWVzIjpbIkFETUlOIl0sImp0aSI6Ijk4NzAxODYxLWZlNDctNGMzOC1hNjAyLTE0YzJhNmU3ZTBjYSIsImNsaWVudF9pZCI6InRlc3Rqd3RjbGllbnRpZCJ9.mk21_auFUHlWj79fktwRi5CETxhkPPYThKjIeP1pqe8"
public static final String AUTHORIZATION_HEADER = "Authorization";
public static final String DEFAULT_INCLUDE_PATTERN = "/api/.*";
@Bean
public Docket swaggerSpringfoxDocket() {
log.debug("Starting Swagger");
Contact contact = new Contact(
"Matyas Albert-Nagy",
"https://justrocket.de",
"[email protected]");
List<VendorExtension> vext = new ArrayList<>();
ApiInfo apiInfo = new ApiInfo(
"Backend API",
"This is the best stuff since sliced bread - API",
"6.6.6",
"https://justrocket.de",
contact,
"MIT",
"https://justrocket.de",
vext);
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo)
.pathMapping("/")
.apiInfo(ApiInfo.DEFAULT)
.forCodeGeneration(true)
.genericModelSubstitutes(ResponseEntity.class)
.ignoredParameterTypes(Pageable.class)
.ignoredParameterTypes(java.sql.Date.class)
.directModelSubstitute(java.time.LocalDate.class, java.sql.Date.class)
.directModelSubstitute(java.time.ZonedDateTime.class, Date.class)
.directModelSubstitute(java.time.LocalDateTime.class, Date.class)
.securityContexts(Lists.newArrayList(securityContext()))
.securitySchemes(Lists.newArrayList(apiKey()))
.useDefaultResponseMessages(false);
docket = docket.select()
.apis(RequestHandlerSelectors.basePackage("com.slandshow.vtdairy.controller"))
.paths(PathSelectors.any())
.build();
return docket;
}
private ApiKey apiKey() {
// new ApiKey("Bearer %token", AUTHORIZATION_HEADER, "header")
return new ApiKey("JWT", AUTHORIZATION_HEADER, "header");
}
private SecurityContext securityContext() {
return SecurityContext.builder()
.securityReferences(defaultAuth())
.forPaths(PathSelectors.regex("/entry/criteria"))
.build();
}
List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope
= new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return Lists.newArrayList(
new SecurityReference("JWT", authorizationScopes));
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question