首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jHipster swagger doc没有发送JWT授权头,导致401

jHipster swagger doc没有发送JWT授权头,导致401
EN

Stack Overflow用户
提问于 2019-09-01 17:55:52
回答 1查看 1.1K关注 0票数 1

使用API-First开发和JWT不发送授权头来生成一个使用jhipster@6.2.0的项目。

api.yml (添加/api prefix和pet路径/模式的默认生成)

代码语言:javascript
复制
# 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: []
  • ./mvnw generate-sources
  • ./mvnw
  • 访问http://localhost:8080/admin/docs

授权头发送给帐户资源GET /api/account

但是,它不会被发送给宠物请求GET /api/pet/findByStatus,从而导致401未经授权。

EN

回答 1

Stack Overflow用户

发布于 2019-09-01 17:55:52

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

代码语言:javascript
复制
                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);
                }

clientAuthorization是用键“承载”来添加的,而不是自动生成的"jwt“。

jwt更改为bearer可解决此问题

代码语言:javascript
复制
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: []

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57748298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档