auth.json中多个http-basic域名需作为独立键共存于"http-basic"对象内,如{"http-basic":{"gitlab.a.com":{"username":"u1","password":"p1"},"gitlab.b.internal":{"username":"u2","password":"p2"}}},域名必须精确匹配请求host字段,不支持通配符或路径继承。

auth.json 里怎么填多个 http-basic 域名
Composer 的 auth.json 文件只认域名一级(不含协议、路径、端口),同一域名重复写会覆盖,不同域名则共存。比如你有两个 GitLab 实例:gitlab.a.com 和 gitlab.b.internal,必须分别作为键写入 http-basic 对象里。
- 错误写法:
composer config --global http-basic.gitlab.a.com user pass执行两次,后一次会删掉前一次 - 正确做法:用
--auth写入项目级auth.json,或直接编辑文件,确保结构是:
{
"http-basic": {
"gitlab.a.com": { "username": "user1", "password": "token1" },
"gitlab.b.internal": { "username": "svc-composer", "password": "xxx" }
}
}
注意:gitlab.a.com 和 https://gitlab.a.com/group/repo.git 中的域名部分必须完全一致——Composer 不解析 URL,只比对 Host 头字段。
GitHub、GitLab、Bitbucket 的 token 字段名不能混用
不同平台认证字段名不同,写错就等于没配。GitHub 必须用 github-oauth,GitLab/Bitbucket 必须用 http-basic,且大小写敏感。
- GitHub:
"github-oauth": { "github.com": "ghp_xxx..." },token 需含repo权限 - GitLab:
"http-basic": { "gitlab.example.com": { "username": "glpat-xxx", "password": "" } },username填 token,password可为空(部分旧版要求填任意非空字符串) - Bitbucket:
"http-basic": { "bitbucket.org": { "username": "your-username", "password": "app-password" } },密码必须是 App Password,不是登录密码
字段名写成 gitlab-oauth 或 http-basic.github.com 都不会生效,Composer 也不会报错,只会静默跳过认证。
生成周报:读取本机 Git 仓库最近一周的提交记录,按项目分类提炼总结,生成标准周报格式,通过微信推送并返回给用户。触发词:周报、weekly report、git总结、提交记录总结、工作周报。
凭证放哪?权限和路径必须严格匹配
Composer 只读取两个位置的 auth.json:项目根目录(优先)或 COMPOSER_HOME(通常是 ~/.composer/auth.json)。放错位置或权限太宽,它直接忽略。
- Linux/macOS 下执行:
chmod 600 ~/.composer/auth.json,否则 Composer 拒绝加载 - Windows 上检查文件是否在
%APPDATA%\Composer\auth.json,别放在子文件夹里 - CI/CD 环境推荐用项目级
auth.json,配合.gitignore排除,再通过环境变量注入敏感值(如用sed或模板渲染生成)
如果 composer diagnose 输出里没出现 “Checking http-basic authentication” 后列出你的域名,基本就是路径或权限问题。
为什么加了凭证还是 401?先看请求有没有带 Authorization 头
401 不代表凭证错,很可能根本没发出去。Composer 只对 repositories 中声明的 URL 所属域名才自动加 Authorization: Basic 头。如果仓库 URL 是 https://gitlab.example.com/api/v4/projects/123/repository/archive.zip,凭证仍只认 gitlab.example.com;但如果 URL 是 https://custom-proxy.internal/proxy-to-gitlab,你就得配 custom-proxy.internal,而不是后端真实域名。
- 用
composer install -v看日志里实际请求的 Host 是什么 - 用
curl -I -u user:pass https://gitlab.example.com/xxx.git手动验证凭证是否有效 - 确认
composer.json里的repositories是vcs类型且url域名与auth.json完全一致
最容易被忽略的是:凭证配对了,但 repositories 没声明这个源,或者声明了却用错协议(比如配了 HTTPS 凭证,url 却写成了 git@gitlab...),这时根本不会触发 HTTP 认证流程。










