Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于 securityDefinitions 不起作用的解决方案 #93

Open
mrsquhuainan opened this issue Oct 7, 2020 · 1 comment
Open

关于 securityDefinitions 不起作用的解决方案 #93

mrsquhuainan opened this issue Oct 7, 2020 · 1 comment

Comments

@mrsquhuainan
Copy link

mrsquhuainan commented Oct 7, 2020

securityDefinitions 配置不仅需要在config加添加,也需要在control层添加对应的securityType
另外注意 apiKey的大小写。
`
// config.default.ts
securityDefinitions: {
apiKey: {
type: 'apiKey',
name: 'authorization',
in: 'header',
},

},

// controller.ts

/**
 * @summary 查询商品优惠信息
 * @description 查询商品优惠信息
 * @router post /v1/goods/searchGoodsCoupon
 * @request body searchGoodsCouponRequest *body 
 * @apiKey // 这个注释必须要添加否则securityDefinitions 不起作用
 * @response 200 
 */
async searchGoodsCoupon() {}

`

以上解决方案可以看源码
egg-swagger-doc/document/index.js/generateSecurity方法
只有满足 if (block.indexOf(@${security}) > -1) 条件时接口才会添加header

`
/**

  • 解析安全验证
  • @param {String} block 注释块
  • @param {Array} securitys 设定的安全验证名称
  • @param {Object} swagger swagger配置
    */
    function generateSecurity(block, securitys, swagger) {
    let securityDoc = [];
    for (let security of securitys) {
    if (block.indexOf(@${security}) > -1) {
    let securityItem = {};
    if (swagger.securityDefinitions[security].type === 'apiKey') {
    securityItem[security] = [];
    securityItem[security].push(swagger.securityDefinitions[security]);
    }
    if (swagger.securityDefinitions[security].type === 'oauth2') {
    securityItem[security] = [];
    Object.keys(swagger.securityDefinitions[security].scopes).forEach(i => {
    securityItem[security].push(i);
    });
    }
    securityDoc.push(securityItem);
    }
    }
    return securityDoc;
    }

`

@sitonlotus
Copy link

可以参考这个issue
#72 (comment)
我的配置是这样的,如下图
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants