Help us learn about your current experience with the documentation. Take the survey.

使用 JWT 作为身份验证提供商

  • 版本:Free, Premium, Ultimate
  • 产品:GitLab Self-Managed

要启用 JWT OmniAuth 提供商,你必须将你的应用程序注册到 JWT。 JWT 会为你提供一个供你使用的密钥。

  1. 在你的 GitLab 服务器上,打开配置文件。

    对于 Linux 包安装:

    sudo editor /etc/gitlab/gitlab.rb

    对于自行编译安装:

    cd /home/git/gitlab
    sudo -u git -H editor config/gitlab.yml
  2. 配置通用设置,将 jwt 添加为单点登录 (SSO) 提供商。这将为没有现有 GitLab 账户的用户启用即时账户配置。

  3. 添加提供商配置。

    对于 Linux 包安装:

    gitlab_rails['omniauth_providers'] = [
      { name: "jwt",
        label: "Provider name", # optional label for login button, defaults to "Jwt"
        args: {
          secret: "YOUR_APP_SECRET",
          algorithm: "HS256", # Supported algorithms: "RS256", "RS384", "RS512", "ES256", "ES384", "ES512", "HS256", "HS384", "HS512"
          uid_claim: "email",
          required_claims: ["name", "email"],
          info_map: { name: "name", email: "email" },
          auth_url: "https://example.com/",
          valid_within: 3600 # 1 hour
        }
      }
    ]

    对于自行编译安装:

    - { name: 'jwt',
        label: 'Provider name', # optional label for login button, defaults to "Jwt"
        args: {
          secret: 'YOUR_APP_SECRET',
          algorithm: 'HS256', # Supported algorithms: 'RS256', 'RS384', 'RS512', 'ES256', 'ES384', 'ES512', 'HS256', 'HS384', 'HS512'
          uid_claim: 'email',
          required_claims: ['name', 'email'],
          info_map: { name: 'name', email: 'email' },
          auth_url: 'https://example.com/',
          valid_within: 3600 # 1 hour
          }
        }

    有关每个配置选项的更多信息,请参阅 OmniAuth JWT 使用文档

    错误地配置这些设置可能会导致实例不安全。

  4. YOUR_APP_SECRET 更改为你的客户端密钥,并将 auth_url 设置为你的重定向 URL。

  5. 保存配置文件。

  6. 要使更改生效,如果你:

现在,在登录页面的常规登录表单下方应该会出现一个 JWT 图标。 选择该图标以开始身份验证流程。JWT 会要求用户 登录并授权 GitLab 应用程序。如果一切顺利,用户 将被重定向到 GitLab 并完成登录。