egg-jwt与egg-swagger-doc冲突,egg-jwt无效
发布于 2 年前 作者 JingMo 3234 次浏览 来自 问答

pulgin.js中 jwt: { enable: true, package: ‘egg-jwt’ }, swaggerdoc: { enable: true, package: ‘egg-swagger-doc’ },

config.default.js中 config.swaggerdoc = { dirScanner: ‘./app/controller’, basePath: ‘/v1’, apiInfo: { title: ‘api’, description: ‘server api’, version: ‘1.0.0’, }, schemes: [‘http’, ‘https’], consumes: [‘application/json’], produces: [‘application/json’], securityDefinitions: { apikey: { type: ‘apiKey’, name: ‘X-ACCESS-KEY’, in: ‘header’, }, oauth2: { type: ‘oauth2’, tokenUrl: ‘http://localhost:7001/oauth’, flow: ‘password’, scopes: { ‘write:access_token’: ‘write access_token’, ‘read:access_token’: ‘read access_token’, }, }, }, enableSecurity: true, enableValidate: true, routerMap: true, enable: true, };

** Router中** module.exports = app => { const { router, controller } = app; router.post(’/’, controller.admin.login); router.get(’/’, app.jwt, controller.admin.getUserInfo); *** //app.jwt无效,不走app.jwt*** };

** Controller中** ‘use strict’;

const Controller = require(‘egg’).Controller;

/**

  • @controller admin 接口 / class AdminController extends Controller { /*
    • @description 控制台用户登录
    • @router post /admins/login
    • @request body adminLoginReq *body
    • @response 200 baseResponse successed / async login() { const { ctx, app } = this; const { phone } = ctx.request.body; const token = app.jwt.sign({ phone }, app.config.jwt.secret, { expiresIn: ‘1h’}); ctx.body = token; } /*
    • @description 获取控制台用户信息
    • @router get /admins/{id}
    • @request path string *id
    • @response 200 baseResponse successed */ async getUserInfo() { const { ctx , app } = this; console.log(ctx.state.user); ctx.body = 'success’ } } module.exports = AdminController;

测试: curl http://localhost:7001/admins/login -X POST -d ‘{“phone”: “123”}’ --header “Content-Type: application/json”

curl http://localhost:7001/admins/123 –header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwaG9uZSI6IjE1NjA1MTY5NTcxIiwiaWF0IjoxNTg2NjkxOTY2LCJleHAiOjE1ODY2OTIwMjZ9.dRNhTXVpNXvVB8Njq9kE9zv92orEZpaZt8glysjmH9E’ 此次请求不验证jwt

这是哪个配置冲突吗?谢谢

1 回复

学习下基础的 Markdown 排版吧。。。

回到顶部