S
S
Sland Show2019-02-26 16:58:17
Java
Sland Show, 2019-02-26 16:58:17

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: 5c75440729217108531294.png

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:
5c7544fde5712505556275.png

Here is the CURL that came out:

curl -X GET "http://localhost:8080/VaultDairy/entry/criteria" -H "accept: */*" -H "Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsidGVzdGp3dHJlc291cmNlaWQiXSwidXNlcl9uYW1lIjoic3VwZXItdXNlckBhZG1pbiIsInNjb3BlIjpbInJlYWQiLCJ3cml0ZSJdLCJleHAiOjE1NTEyMjk3NTcsImF1dGhvcml0aWVzIjpbIkFETUlOIl0sImp0aSI6Ijk4NzAxODYxLWZlNDctNGMzOC1hNjAyLTE0YzJhNmU3ZTBjYSIsImNsaWVudF9pZCI6InRlc3Rqd3RjbGllbnRpZCJ9.mk21_auFUHlWj79fktwRi5CETxhkPPYThKjIeP1pqe8"


It is incorrectly composed, you can even check it via Postman: It
5c7545698889f539713563.png
should be:
5c7545ac5bf1c671542713.png

Here is how my SwaggerConfig is configured:
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));
    }


What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question