背景
公司内部有基于oauth2协议自建的单点登录服务,现将原有的各子系统单独维护的登录统一迁移至单点登录。在迁移gitlab时,遇到的坑比较多,所以在这里记录下。
安装gitlab
实验环境是通过dokcer安装的ce版本的gitlab,
gitlab版本号: 13.12.1
编辑gitlab.rb文件
如果你是基于源码的方式安装,请编辑 gitlab.yml 文件
gitlab官网地址
gitlab_rails['omniauth_enabled'] = true # 开启omniauth
gitlab_rails['omniauth_allow_single_sign_on'] = true # 此处值为true的话,当gitlab不存在该用户时会自动在gitlab中创建用户
gitlab_rails['omniauth_block_auto_created_users'] = false # 是否禁用自动创建的gitlab用户 ,为false则表示自动创建的用户不禁用。为true时则表示禁用,需要gitlab管理员手动解除禁用
gitlab_rails['omniauth_auto_link_user'] = true # 是否自动关联已经存在的gitlab账号
gitlab_rails['omniauth_providers'] = [
{
'name' => 'oauth2_generic',
'app_id' => 'xxxxx', # oauth2的app_id 由sso服务进行分配
'app_secret' => 'xxxxx', # oauth2的app_secret 由sso服务进行分配
'args' => {
client_options: {
'site' => 'http://127.0.0.1:9001', # sso的地址
'authorize_url' => '/sso/login', # 认证URL
'token_url' => '/sso/api/oauth/gitlab', # 获取token的URL
'user_info_url' => '/sso/api/user' # 获取用户信息的URL
},
user_response_structure: {
root_path: [], # i.e. if attributes are returned in JsonAPI format (in a 'user' node nested under a 'data' node)
id_path: ['uid'], # 此处的用户信息如何配置 我会在下面详细说明
attributes: { name: 'username', nickname: 'nickname',email:'email'} # 此处的用户信息如何配置 我会在下面详细说明
# optionally, you can add the following two lines to "white label" the display name
# of this strategy (appears in urls and Gitlab login buttons)
# If you do this, you must also replace oauth2_generic, everywhere it appears above, with the new name.
name: 'SSO', # 此处的属性值会在登陆处,以及设置identitifier时使用到,建议英文(不支持中文)
strategy_class: "OmniAuth::Strategies::OAuth2Generic" # Devise-specific config option Gitlab uses to find renamed strategy
}
}
]
配置说明
user_response_structure
此处的配置是映射你的sso服务 user_info_url接口返回的用户信息。
如果你的用户信息接口返回的结构为
{
"code":200,
"data":{
"uid":111,
"username":"zhangsan",
"nickname":"张三",
"email":"zhangsan@kiki.com"
}
}
那么 root_path 可以不用配置
id_path建议配置成用户的唯一标识
attributes配置我就不过多解释了
更多详细注释请参考gitlab官网:
https://gitlab.com/satorix/omniauth-oauth2-generic#gitlab-config-example
配置完成后重启gitlab
测试

马赛克中的名称 同配置中的name属性
- 点击Sign in with 会跳转至 配置中sso服务的认证URL

2.认证成功后会sso会根据oauth2协议redirect 到gitlab的回调地址(回调地址我们有个后台可以配置app_id ,secret,redirect_url啥的)
3.gitlab获取access_token

回调sso的token_url接口获取access_token
4.gitlab获取用户信息

5.登录测试
以上配置完成后,重启gitlab,点击登录按钮,这时会跳转到你配置的sso登录页面



sso服务验证成功后会跳转至gitlab,gitlab会回调sso获取用户的信息接口取当前用户信息,若发现该用户不在gitlab中,会在gitlab中创建该用户。


至此gitlab接入外部oauth单点登录完成
写作不易,随手点个赞呗!!!!

4744

被折叠的 条评论
为什么被折叠?



