点击关注公众号,实用技术文章及时了解

需求提出:

公司内部已有一套oneid用户中心,需要支持登录gitlab。

实现

GitLab支持配置第三方登录, 修改配置文件gitlab.rb

vi /etc/gitlab/gitlab.rb#OAuth2.0
gitlab_rails['omniauth_enabled'] = true
gitlab_rails['omniauth_allow_single_sign_on'] = ['OneID']
gitlab_rails['omniauth_block_auto_created_users'] = false
gitlab_rails['omniauth_providers'] = [{'name' => 'OneID','app_id' => '123','app_secret' => '1111','args' => {client_options: {'site' => 'http://10.30.75.85:31900','authorize_url' => '/auth','user_info_url' => '/userInfo'},user_response_structure: {root_path: [],id_path: 'userAccountID',attributes: {name: 'realName',nickname: 'nickname',email: 'email',username:'username'}},name: 'OneID',strategy_class: "OmniAuth::Strategies::OAuth2Generic"}}
]

http://10.30.75.85:31900 :本人服务的地址

以上数据仅供参考,请根据实际情况修改,不清楚配置请百度,有详细案例

我服务实现方式为java web项目(Spring boot),配置:

<dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.11.3</version>
</dependency><dependency><groupId>com.konghq</groupId><artifactId>unirest-java</artifactId><version>3.5.00</version>
</dependency><!-- 需要作为独立jar文件引用时(包含隐式依赖) -->
<dependency><groupId>com.konghq</groupId><artifactId>unirest-java</artifactId><version>3.5.00</version><classifier>standalone</classifier>
</dependency>

定义OAuthController.java

@Controller
@RefreshScope
public class OAuthController extends BaseController {@Value("${dossen.gitlab.url}")private String gitLabUrl;/*** 获得通过oneid登录得重定向地址* @return*/@RequestMapping(value = "/login", method = RequestMethod.GET)public String getGitLabStateVal(HttpServletRequest request, HttpServletResponse response){//所有cookie-我就看看,没什么用Cookie[] cookies = request.getCookies();//获得通过oneid登录得重定向地址String location = ImitativeLoginGitLabUtil.getLocation(gitLabUrl);String[] urlAndCookie = location.split("&&");//设置cookieCookie cookie = new Cookie("_gitlab_session",urlAndCookie[1].replaceAll("_gitlab_session=",""));cookie.setPath("/");response.addCookie(cookie);return "redirect:"+urlAndCookie[0];}@RequestMapping(value = "/auth", method = RequestMethod.GET)public String auth(OAuthRequest request) {//需要自己写实现逻辑鉴权返回给gitlabreturn "redirect:"";}/*** 获取用户信息** @return*/@ResponseBody@RequestMapping(value = "/userInfo")public Object userInfo(HttpServletRequest request) {//gitlab请求参数查询用户信息,返回给gitlabUserGetResponse userGetResponse = null;Map<String, Object> resultMap = new HashMap<String, Object>();resultMap.put("userAccountID", userGetResponse.getUserAccountID());resultMap.put("realName", userGetResponse.getRealName());resultMap.put("nickname", userGetResponse.getRealName());resultMap.put("username", userGetResponse.getEmail().split("@")[0]);resultMap.put("email", userGetResponse.getEmail());ResponseEntity<Object> responseEntity = new ResponseEntity<Object>(resultMap, HttpStatus.valueOf(200));return responseEntity;}}

定义ImitativeLoginGitLabUtil.java

package com.dossen.gitlab.adapter.util;import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Value;/*** 模拟登录gitlab请求获取重定向值* @Author wenfl* @Date 2021-10-14*/
public class ImitativeLoginGitLabUtil {public static String getLocation(String gitLabUrl){HttpResponse<String> response = null;try {//打开登录页面response =Unirest.get(gitLabUrl).asString();//得到document对象Document doc = Jsoup.parse(response.getBody());String authenticity_token = doc.select("meta[name=csrf-token]").get(0).attr("content");String cookeiValue = response.getHeaders().getFirst("Set-Cookie");response = Unirest.post(gitLabUrl+"/users/auth/OneID").header("Cookie", cookeiValue).header("Content-Type", "application/x-www-form-urlencoded").field("authenticity_token", authenticity_token).asString();//获得重定向地址String location = response.getHeaders().getFirst("Location")+"&&"+cookeiValue.split(";")[0];return location;} catch (Exception e) {e.printStackTrace();}return "";}
}

经过上面的操作就已完成常规的登录了,界面如下

后续

因公司已有一套用户中心,需要实现直接在用户中心点击就完成登录的过程跳转到首页。结合OAuthController中getGitLabStateVal方法完成模拟gitlab页面点击第三方登录按钮操作,主要还是设置cookie的动作,需要在gitlab的域中设置才能生效 :

修改gitlab的nginx配置/var/opt/gitlab/nginx/conf/gitlab-http.conf

# 以下操作是为了能让用户中心点击图标实现登录的过程
location /oneid/login{proxy_set_header Host $http_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header REMOTE-HOST $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass http://10.30.75.85:31900/login;
}

修改proxy_pass为java web项目地址

执行:gitlab-ctl restart nginx

注:不要执行gitlab-ctl reconfigure,否则配置会被覆盖

这样就可以在用户中心配置地址为:http://gitlaburl.com/oneid/login,就可以完成登录的动作了。

推荐

主流Java进阶技术(学习资料分享)

Java面试题宝典

加入Spring技术开发社区

PS:因为公众号平台更改了推送规则,如果不想错过内容,记得读完点一下“在看”,加个“星标”,这样每次新文章推送才会第一时间出现在你的订阅列表里。点“在看”支持我们吧!

GitLab 配置 OAuth2 实现第三方登录,简直太方便了!相关推荐

  1. OAuth2.0 第三方登录

    本篇内容以用户访问百度,通过新浪微博的第三方登录百度过程讲解 用户:资源拥有者 百度:客户端 新浪微博:认证服务器 前置条件: 百度已经向新浪微博申请注册,分配给百度的client_id是251245 ...

  2. QQ、微信、新浪微博和百度第三方登录

    对于大多数的APP都有第三方登录这个功能,自己也做过几次,最近又有一个新项目用到了第三方登录,所以特意总结了一下关于第三方登录的实现,并拿出来与大家一同分享: 各大开放平台注册账户获取AppKey和A ...

  3. 第三方登录——QQ、微信、新浪微博和百度登录

    实现QQ.微信.新浪微博和百度第三方登录(Android Studio) 前言:  对于大多数的APP都有第三方登录这个功能,自己也做过几次,最近又有一个新项目用到了第三方登录,所以特意总结了一下关于 ...

  4. Android 实现QQ、微信、新浪微博和百度第三方登录

    前言:  对于大多数的APP都有第三方登录这个功能,自己也做过几次,最近又有一个新项目用到了第三方登录,所以特意总结了一下关于第三方登录的实现,并拿出来与大家一同分享: 各大开放平台注册账户获取App ...

  5. Spring Boot+OAuth2使用GitHub登录自己的服务

    网站利用 OAuth2 提供第三方登录(GitHub) 授权码模式 A 网站让用户跳转到 GitHub GitHub 要求用户登录,然后询问"A 网站要求获得 xx 权限,你是否同意?&qu ...

  6. 第三方登录之支付宝登录

    title: 第三方登录之支付宝登录 date: 2017-08-06 21:02:00 tags: Android开发 categories: Android开发 公司一个需求让做一个支付宝的第三方 ...

  7. spring security oauth2.0 client集成第三方登录

       大家上网的时候可能会遇见这样的一个问题,就是我们去访问一个网站,但是又不想去注册这个网站的账号,账号太多了实在是记不来,于是我们可以用qq或者微信登录这个网站,简直不要太方便有没有.    这么 ...

  8. Spring Boot 接入 GitHub 第三方登录,只要两行配置!

    点击上方 好好学java ,选择 星标 公众号重磅资讯,干货,第一时间送达 今日推荐:14 个 github 项目!个人原创100W +访问量博客:点击前往,查看更多 本文地址:https://www ...

  9. Spring Security OAuth2——自定义OAuth2第三方登录(Gitee)并与UsernamePassword登录关联解决方案

    前文:Spring Security OAuth2--自定义OAuth2第三方登录(Gitee) Maven 主要 <!--Spring Security--><dependency ...

最新文章

  1. 青少年蓝桥杯_2020_每日一题_11.03_输出M与N之间符合要求的数据
  2. Boost:基于Boost的异步TCP回显服务器
  3. P5707 【深基2.例12】上学迟到(python3实现)
  4. jsp的九大内置对象和作用域
  5. 苹果高通“情变”祸及所有中国 iPhone 用户!
  6. 在PPT设计中如何制作有效的图表?
  7. 保存好用的工具---转载
  8. 使用scp从远程服务器下载文件到本地
  9. php结合美图秀秀,美图秀秀头像编辑器的使用?thinkphp+七牛方案
  10. csol修改本地服务器,CSOL简单地控制台优化+显卡优化 低配也能流畅运行
  11. VS2005 执行控制台程序的时候,窗口一闪就没了的问题
  12. 外接键盘Win键不能的使用问题以及FN键的使用
  13. html 学习笔记(9)
  14. 211大学计算机找工作,华为最青睐的5所大学,每年招聘大量毕业生,第一所只是211院校...
  15. 【论文调研】IJCAI-2019 论文投稿与录用调研报告
  16. AI - Intelligent Agents
  17. 【SolidWorks】模型状态显示设置介绍
  18. attr 与prop 的区别
  19. python方向是干什么的_为什么这么多人喜欢Python?Python的就业方向是什么?
  20. 如何让文本框中文字居中

热门文章

  1. 小米手表外观官宣:方表盘+黑银两色
  2. 支付宝:预计三年内智能出行将覆盖全国
  3. 全球首款五摄手机Nokia 9 PureView国行版发布:价格惊了!
  4. 使用python对url编码解码
  5. java多线程问题,线程交替执行
  6. linux和windows下的“回车符”和“换行符”
  7. python调用ping命令_python调用系统命令ping
  8. 剑指offer不用加减乘除做加法_剑指Offer-不用加减乘除做加法
  9. 安装工程造价课程设计_安装工程造价课程设计的图纸-上海装修报价
  10. python 加快计算速度_python怎么提高计算速度