Message "Support for password authentication was removed."

Message “Support for password authentication was removed.”

技术背景

从2021年8月13日起,GitHub不再接受使用账户密码进行Git操作的身份验证,而是需要使用个人访问令牌(PAT)来替代。这是因为密码容易被意外泄露,而个人访问令牌可以进行权限限制,降低风险。

实现步骤

创建个人访问令牌

  1. 登录GitHub账户,进入Settings -> Developer Settings -> Personal Access Token -> Tokens (classic) -> Generate New Token
  2. 输入密码,填写表单,点击Generate token
  3. 复制生成的令牌,格式类似ghp_sFhFsSHhTzMDreGRLjmks4Tzuzgthdvfsrta

不同操作系统配置PAT

Windows

  1. Control Panel进入Credential Manager -> Windows Credentials
  2. 若找到git:https://github.com,点击Edit,将密码替换为GitHub个人访问令牌。
  3. 若未找到git:https://github.com,点击Add a generic credential,Internet地址填git:https://github.com,用户名填GitHub用户名,密码填个人访问令牌,点击Ok。

macOS

  1. 点击菜单栏右侧的Spotlight图标(放大镜),输入Keychain access并回车打开应用。
  2. 在Keychain Access中搜索github.com
  3. 找到github.cominternet password条目,相应地编辑或删除该条目。

Linux

  1. 配置本地GIT客户端的用户名和邮箱地址:
1
2
3
git config --global user.name "your_github_username"
git config --global user.email "your_github_email"
git config -l
  1. 使用GIT访问GitHub,例如克隆仓库:
1
git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY
  1. 输入用户名和个人访问令牌。
  2. 缓存记录,让计算机记住令牌:
1
git config --global credential.helper cache
  1. 若需要,可随时删除缓存记录:
1
2
git config --global --unset credential.helper
git config --system --unset credential.helper
  1. 验证:
1
git pull -v

JetBrains IDEs配置

以IntelliJ为例:

  1. 按下⌘Cmd + 0/Ctrl + Alt + 0打开设置,选择Version Control | GitHub
  2. 点击Add按钮。
  3. 选择Log In with Token
  4. 在文本框中插入令牌。
  5. 点击Add Account

核心代码

克隆仓库时使用PAT

1
git clone https://<username>:<githubtoken>@github.com/<username>/<repositoryname>.git

修改远程仓库URL使用PAT

1
git remote set-url origin https://<token>@github.com/<username>/<repo>.git

清除凭证管理器缓存

1
echo url=https://[email protected] | git credential reject

设置凭证管理器

  • Windows:
1
git config --global credential.helper manager
  • macOS:
1
git config --global credential.helper osxkeychain
  • Linux(可用libsecret):
1
git config --global credential.helper libsecret
  • Linux(无libsecret):
1
git config --global credential.helper store

最佳实践

  • 为个人访问令牌设置合理的过期时间和权限范围,提高安全性。
  • 使用凭证管理器保存令牌,避免每次操作都手动输入。
  • 定期更换个人访问令牌,降低泄露风险。

常见问题

找不到凭证管理器中的GitHub条目

可以尝试克隆一个项目,Git会提示输入用户名和密码,此时输入用户名和个人访问令牌,条目就会被创建。

Git反复要求输入用户名和令牌

  • 运行nano ~/.git-credentials,删除GitHub相关行并保存。
  • 执行git config --global credential.helper store,将令牌保存到文件中,但存在一定风险。
  • 运行git pull,仅需输入一次用户名和密码。

macOS KeyChain Access中找不到github.com条目

  • 尝试进行一次Git操作,系统会要求输入用户名。
  • 输入GitHub用户名。
  • Personal access tokens生成一个新密钥。
  • 在密码字段中输入新生成的令牌值,此时可以在KeyChain Access -> login中看到新的github.com条目。

Message "Support for password authentication was removed."
https://119291.xyz/posts/message-support-for-password-authentication-was-removed/
作者
ww
发布于
2025年5月26日
许可协议