我正在使用openApi 3.0规范,koa2-swagger-ui和swagger-jsdoc。我试图在swagger ui上获得“授权”按钮,以允许我输入JWT令牌,以便我的请求将得到授权。
我遵循了OpenApi 3.0文档,在securitySchemes中设置了bearerAuth,并使用安全性使其成为全局性。所有这些都在我的swagger-config.yaml中实现。
我想要的是能够在swagger ui上单击授权,并可以选择输入JWT。当前,当我单击“授权”时,该框为空。 empty authorization request response 401 swagger ui
Swagger.json
{
"openapi": "3.0.0",
"info": {
"title": "LMS API Specification",
"version": "1.0.0",
"description": "Open documentation for LMS API"
},
"host": "localhost:8091",
"basePath": "/",
"components": {
"securitySchemes": {
"bearerAuth": {
"type": "http",
"scheme": "bearer",
"bearerFormat": "JWT"
}
}
},
"security": [
{
"bearerAuth": []
}
],
"paths": {
"/instructor/me": {
"get": {
"tags": [
"Instructor"
],
"description": "Finds all classes and their status for the current user",
"responses": {
"200": {
"description": "You have successfully found all classes and their status for the current user"
}
}
}
}
},
"tags": []
}
swagger-config.yaml
openapi: 3.0.0
info:
title: LMS API Specification
version: 1.0.0
description: Open documentation for LMS API
host: localhost:8091
basePath: /
apis: ['api/v1/instructor/index.js']
components:
securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT
security:
- bearerAuth: []
app.js
import Koa from 'koa'
import cors from 'koa-cors'
import serveStatic from 'koa-static'
// import websockify from 'koa-websocket'
import Logger from './lib/Logger'
import authInit from './auth'
import index from './routes/index'
import auth from './routes/auth'
import launch from './routes/launch'
import lesson from './routes/lesson'
import v1 from './api/v1'
import Router from 'koa-router'
export default async port => {
const koaSwagger = require('koa2-swagger-ui');
// const app = websockify(new Koa
const app = new Koa()
const swaggerJSDoc = require('swagger-jsdoc');
var router = new Router()
await authInit(app)
// Definitions for the swagger docs
const swaggerDefinition = {
info: {
// API informations (required)
title: 'LMS API Specification', // Title (required)
version: '1.0.0', // Version (required)
description: 'OpenAPI documentation for LMS', // Description (optional)
},
host: `localhost:8091/api/v1`, // Host (optional)
basePath: '/', // Base path (optional)
};
const options = {
// Import swaggerDefinitions
swaggerDefinition,
// Path to the API docs
// Note that this path is relative to the current directory from which the Node.js is ran, not the application itself.
apis: ['api/v1/instructor/index.js'],
};
// Initialize swagger-jsdoc -> returns validated swagger spec in json format
const swaggerSpec = swaggerJSDoc(options);
router.get('/swagger.json', async (ctx, next) => {
ctx.set('Content-Type', 'application/json')
ctx.body = (swaggerSpec);
return
});
app.use(
koaSwagger({
swaggerOptions: {
url: 'http://localhost:8091/swagger.json', // example path to json
},
hideTopbar: true,
routePrefix: '/docs', // route where the view is returned
}),
);
Logger.info(`Running in ${process.env.NODE_ENV} environment`)
app
.use(cors())
.use(serveStatic(__dirname + '/assets'))
.use(index.routes())
.use(auth.routes())
.use(launch.routes())
.use(lesson.routes())
.use(v1.routes())
.use(router.routes())
return app.listen(port, () => {
Logger.info(`> Ready on port ${port}`)
})
}
答案 0 :(得分:1)
我最终使它工作的方法是更新我的app.js文件和swagger-config.yaml文件,使其看起来像这样...
app.js
import Koa from 'koa'
import cors from 'koa-cors'
import serveStatic from 'koa-static'
// import websockify from 'koa-websocket'
import Logger from './lib/Logger'
import authInit from './auth'
import index from './routes/index'
import auth from './routes/auth'
import launch from './routes/launch'
import lesson from './routes/lesson'
import v1 from './api/v1'
import Router from 'koa-router'
export default async port => {
const koaSwagger = require('koa2-swagger-ui');
// const app = websockify(new Koa
const app = new Koa()
const swaggerJSDoc = require('swagger-jsdoc');
var router = new Router()
await authInit(app)
// Definitions for the swagger docs
const swaggerDefinition = {
openapi: '3.0.1',
info: {
// API informations (required)
title: 'LMS API Specification', // Title (required)
version: '1.0.0', // Version (required)
description: 'OpenAPI documentation for LMS', // Description (optional)
},
servers: [{url: 'http://localhost:8091/'}],
components: {
securitySchemes: {
bearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
}
}
},
security: [{
bearerAuth: []
}]
};
const options = {
// Import swaggerDefinitions
swaggerDefinition,
// Path to the API docs
// Note that this path is relative to the current directory from which the Node.js is ran, not the application itself.
apis: ['api/v1/instructor/index.js'],
};
// Initialize swagger-jsdoc -> returns validated swagger spec in json format
const swaggerSpec = swaggerJSDoc(options);
router.get('/swagger.json', async (ctx, next) => {
ctx.set('Content-Type', 'application/json')
ctx.body = (swaggerSpec);
return
});
app.use(
koaSwagger({
swaggerOptions: {
url: 'http://localhost:8091/swagger.json', // example path to json
},
hideTopbar: true,
routePrefix: '/docs', // route where the view is returned
}),
);
Logger.info(`Running in ${process.env.NODE_ENV} environment`)
app
.use(cors())
.use(serveStatic(__dirname + '/assets'))
.use(index.routes())
.use(auth.routes())
.use(launch.routes())
.use(lesson.routes())
.use(v1.routes())
.use(router.routes())
return app.listen(port, () => {
Logger.info(`> Ready on port ${port}`)
})
}
swagger-config.yaml
openapi: 3.0.1
info:
title: LMS API Specification
version: 1.0.0
description: Open documentation for LMS API
servers:
- url: http://localhost:8091/
apis: ['api/v1/instructor/index.js']
components:
securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT
security:
- bearerAuth: []
基本上,我向swaggerDefinition添加了openapi:3.0.1。