如果 gitlab 是采用 https 协议连接,会报 401授权失败的错误 1. 修改 controller/oauth2Controller.js 文件 ``` js const https = require('https'); // 添加 https 依赖 const fs = require('fs'); // 用于证书文件读取 // 修改 requestInfo 方法,如果配置的 host 地址是 https 的则通过 https 的方式发送请求 requestInfo(ops, path, method) { return new Promise((resolve, reject) => { let req = ''; let client = http; // 默认为 http 请求 let options = { method: method }; if (ops.host.indexOf('https') === 0) { // 如果 host 以 https 开头 client = https; // 将连接设置为 https // gitlab 证书是自己生成的不获取证书文件会报 UNABLE_TO_VERIFY_LEAF_SIGNATURE 错误 // 这个错误可以通过两种方式解决(任选一种即可): // 1)options.rejectUnauthorized 设置成 false // 2)将 ssl 的根证书文件赋值给 options.ca options.ca = fs.readFileSync('[path to our CA cert file]'); } let http_client = clent.request(ops.host + path, options, function(res){ ... }); ... }); } ``` 2. 修改 server.js 文件 ``` js const fs = require('fs'); // 用于证书文件读取 module.exports = function (options) { const {emailPostfix, emailKey, userKey, host, loginPath, redirectUri} = options; this.bindHook('third_login', (ctx) => { let token = ctx.request.body.token || ctx.request.query.token; // 如果是 https,设置 options.ca 参数,或者将 options.rejectUnauthorized 设置成 false let options = host.indexOf("https") === 0 ? {ca: fs.readFileSync('[path to our CA cert file]')} : {}; return new Promise((resolve, reject) => { // 发送请求时,传递 options 参数 request(host + loginPath + "?access_token=" + token, options, function (error, response, body) { ... }); }); } ```
Activity
[-]让插件支持 https,鼓捣了很久希望能帮到有同样问题的人[/-][+]401 “授权失败”,让插件支持 https,鼓捣了很久希望能帮到有同样问题的人[/+]junzizhiyao commentedon May 20, 2020
照着如上更改了vendors/node_modules/yapi-plugin-gitlab下相关文件,然而没有起作用,如何破
zhamdoctor commentedon Aug 28, 2020
解决了么
zhamdoctor commentedon Aug 28, 2020
还是不行,有具体点的么,急需
xuyihuang commentedon Jun 23, 2022
我试了不行
baojd42 commentedon Nov 17, 2022
试了,方法可行。中间出了一些错,后来发现是
let http_client = http.request中的http.request忘了改成client.request。楼主好人,一生平安。voldikss commentedon Jul 2, 2023
用上了,感谢。另外有三点需要注意:
kexuanlin commentedon Sep 11, 2023
我用的gitlab是http协议,但也会提示授权失败,按楼主的方法改了也没成功,有和我一样的吗