简体   繁体   中英

jHipster swagger api-doc not sending JWT authorization header resulting in 401

Generating a project with jhipster@6.2.0 with API-First development and JWT does not send the authorization header.

api.yml (default generated with addition of /api prefix and pet path/schema)

# API-first development with OpenAPI
# This file will be used at compile time to generate Spring-MVC endpoint stubs using openapi-generator
openapi: '3.0.1'
info:
  title: 'temp2'
  version: 0.0.1
servers:
  - url: http://localhost:8080/api
    description: Development server
  - url: https://localhost:8080/api
    description: Development server with TLS Profile
paths:
  /pet/findByStatus:
    get:
      tags:
        - pet
      summary: Finds Pets by status
      description: Multiple status values can be provided with comma separated strings
      operationId: findPetsByStatus
      responses:
        200:
          description: successful operation
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Pet'
        400:
          description: Invalid status value
          content: {}

components:
  schemas:
    Pet:
      required:
        - name
        - photoUrls
      type: object
      properties:
        id:
          type: integer
          format: int64
  securitySchemes:
    jwt:
      type: http
      description: JWT Authentication
      scheme: bearer
      bearerFormat: JWT
security:
  - jwt: []

The authorization header is sent for the account-resources GET /api/account 在此处输入图片说明

However it is not sent for the pet request GET /api/pet/findByStatus resulting in a 401 Unauthorized.

在此处输入图片说明

In src/main/webapp/swagger-ui/index.html

                function addApiKeyAuthorization() {
                    var authToken = JSON.parse(localStorage.getItem("jhi-authenticationtoken") || sessionStorage.getItem("jhi-authenticationtoken"));
                    var apiKeyAuth = new SwaggerClient.ApiKeyAuthorization("Authorization", "Bearer " + authToken, "header");
                    window.swaggerUi.api.clientAuthorizations.add("bearer", apiKeyAuth);
                }

The clientAuthorization is added with the key "bearer" instead of the autogenerated "jwt".

Changing jwt to bearer resolves it

diff --git a/src/main/resources/swagger/api.yml b/src/main/resources/swagger/api.yml
index b259b3e..1f77650 100644
--- a/src/main/resources/swagger/api.yml
+++ b/src/main/resources/swagger/api.yml
@@ -42,10 +42,10 @@ components:
           type: integer
           format: int64
   securitySchemes:
-    jwt:
+    bearer:
       type: http
       description: JWT Authentication
       scheme: bearer
       bearerFormat: JWT
 security:
-  - jwt: []
+  - bearer: []

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM