偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

沒錯,用三方 Github 做授權(quán)登錄就是這么簡單!

開源
最近在做自己的開源項目(fire),Springboot + vue 的前后端分離框架才搭建完,剛開始做登錄功能,做著做著覺得普通賬戶密碼登錄太簡單了沒啥意思,思來想去為顯得逼格高一點,決定再加上 GitHub授權(quán) 和 人臉識別等多種登錄方式。

[[333729]]

本文轉(zhuǎn)載自微信公眾號「程序員內(nèi)點事」,作者程序員內(nèi)點事 。轉(zhuǎn)載本文請聯(lián)系程序員內(nèi)點事公眾號。

最近在做自己的開源項目(fire),Springboot + vue 的前后端分離框架才搭建完,剛開始做登錄功能,做著做著覺得普通賬戶密碼登錄太簡單了沒啥意思,思來想去為顯得逼格高一點,決定再加上 GitHub授權(quán) 和 人臉識別等多種登錄方式。

在這里插入圖片描述

 

而GitHub授權(quán)登錄正好用到了OAuth2.0中最復(fù)雜的授權(quán)碼模式,正好拿我這個案例給大家分享一下OAuth2.0的授權(quán)過程,我把項目已經(jīng)部署到云服務(wù),文末有預(yù)覽地址,小伙伴們可以體驗一下,后續(xù)項目功能會持續(xù)更新。

一、授權(quán)流程

在具體做GitHub授權(quán)登錄之前,咱們再簡單回顧一下OAuth2.0授權(quán)碼模式的授權(quán)流程,如果 fire 網(wǎng)站允許 用GitHub 賬號登錄,流程大致如下圖。

在這里插入圖片描述

 

用戶想用GitHub 賬號去登錄 fire 網(wǎng)站:

  • fire 網(wǎng)站先讓用戶跳轉(zhuǎn)到 GitHub 進行授權(quán),會彈出一個授權(quán)框。
  • 用戶同意后,GitHub 會根據(jù)redirect_uri 重定向回 fire 網(wǎng)站,同時返回一個授權(quán)碼code。
  • fire 網(wǎng)站使用授權(quán)碼和客戶端密匙client_secret,向 GitHub 請求令牌token,檢驗通過返回令牌。
  • 最后fire 網(wǎng)站向GitHub 請求數(shù)據(jù),每次調(diào)用 GitHub 的 API 都要帶上令牌。

二、身份注冊

梳理完授權(quán)邏輯,接下來我們還有一些準(zhǔn)備工作。

要想得到一個網(wǎng)站的OAuth授權(quán),必須要到它的網(wǎng)站進行身份注冊,拿到應(yīng)用的身份識別碼 ClientID 和 ClientSecret。

注冊 傳送門 https://github.com/settings/applications/1334665,有幾個必填項。

  • Application name:我們的應(yīng)用名;
  • Homepage URL:應(yīng)用主頁鏈接;
  • Authorization callback URL:這個是github 回調(diào)我們項目的地址,用來獲取授權(quán)碼和令牌。

 

提交后會看到就可以看到客戶端ClientID 和客戶端密匙ClientSecret,到這我們的準(zhǔn)備工作就完事了。

在這里插入圖片描述

 

三、授權(quán)開發(fā)

1、獲取授權(quán)碼

為了更好的看效果,獲取授權(quán)碼我處理的比較粗暴,直接在JS里拼裝好了授權(quán)鏈接,但實際工作開發(fā)中一定要考慮到安全問題。

  1. https://github.com/login/oauth/authorize? 
  2. client_id=ad41c05c211421c659db& 
  3. redirect_uri=http://47.93.6.5:8080/authorize/redirect 

前端 vue 的邏輯也非常簡單,只需要 window.location.href 重定向一下。

  1. <script> 
  2. export default { 
  3.   methods: { 
  4.     loginByGithub: function () { 
  5.       window.location.href = 'https://github.com/login/oauth/authorize?client_id=ad41c05c211421c659db&redirect_uri=http://47.93.6.5:8080/authorize/redirect' 
  6.     } 
  7.   } 
  8. </script> 

 

請求后會提示讓我們授權(quán),同意授權(quán)后會重定向到authorize/redirect,并攜帶授權(quán)碼code;如果之前已經(jīng)同意過,會跳過這一步直接回調(diào)。

在這里插入圖片描述

 

2、獲取令牌

授權(quán)后緊接著就要回調(diào) fire 網(wǎng)站接口,拿到授權(quán)碼以后拼裝獲取令牌 access_token的請求鏈接,這時會用到客戶端密匙client_secret。

  1. https://github.com/login/oauth/access_token?  
  2.     client_id=${clientID}&  
  3.     client_secret=${clientSecret}&  
  4.     code=${requestToken} 

access_token 會作為請求響應(yīng)返回,結(jié)果是個串字符,需要我們截取一下。

  1. access_token=4dc43c2f43b773c327f97acf5dd66b147db9259c&scope=&token_type=bearer 

有了令牌以后開始獲取用戶信息,在 API 中要帶上access_token。

  1. https://api.github.com/user?access_token=4dc43c2f43b773c327f97acf5dd66b147db9259c 

返回的用戶信息是 JSON 數(shù)據(jù)格式,如果想把數(shù)據(jù)傳遞給前端,可以通過 url 重定向到前端頁面,將數(shù)據(jù)以參數(shù)的方式傳遞。

  1.     "login""chengxy-nds"
  2.     "id": 12745094, 
  3.     "node_id"""
  4.     "avatar_url""https://avatars3.githubusercontent.com/u/12745094?v=4"
  5.     "gravatar_id"""
  6.     "url""https://api.github.com/users/chengxy-nds"
  7.     "html_url""https://github.com/chengxy-nds"
  8.     "followers_url""https://api.github.com/users/chengxy-nds/followers"
  9.     "following_url""https://api.github.com/users/chengxy-nds/following{/other_user}"
  10.     "gists_url""https://api.github.com/users/chengxy-nds/gists{/gist_id}"
  11.     "starred_url""https://api.github.com/users/chengxy-nds/starred{/owner}{/repo}"
  12.     "subscriptions_url""https://api.github.com/users/chengxy-nds/subscriptions"
  13.     "organizations_url""https://api.github.com/users/chengxy-nds/orgs"
  14.     "repos_url""https://api.github.com/users/chengxy-nds/repos"
  15.     "events_url""https://api.github.com/users/chengxy-nds/events{/privacy}"
  16.     "received_events_url""https://api.github.com/users/chengxy-nds/received_events"
  17.     "type"""
  18.     "site_admin"false
  19.     "name""程序員內(nèi)點事"
  20.     "company"null
  21.     "blog"""
  22.     "location"null
  23.     "email"""
  24.     "hireable"null
  25.     "bio"null
  26.     "twitter_username"null
  27.     "public_repos": 7, 
  28.     "public_gists": 0, 
  29.     "followers": 14, 
  30.     "following": 0, 
  31.     "created_at""2015-06-04T09:22:44Z"
  32.     "updated_at""2020-07-13T06:08:57Z" 

下邊是 GitHub 回調(diào)我們 fire網(wǎng)站后端處理流程的部分代碼,寫的比較糙,后續(xù)繼續(xù)優(yōu)化吧!

  1. /** 
  2.      * @param code 
  3.      * @author xiaofu 
  4.      * @description 授權(quán)回調(diào) 
  5.      * @date 2020/7/10 15:42 
  6.      */ 
  7.    @RequestMapping("/authorize/redirect"
  8.     public ModelAndView authorize(@NotEmpty String code) { 
  9.  
  10.         log.info("授權(quán)碼code: {}", code); 
  11.  
  12.         /** 
  13.          * 重新到前端主頁 
  14.          */ 
  15.         String redirectHome = "http://47.93.6.5/home"
  16.  
  17.         try { 
  18.             /** 
  19.              * 1、拼裝獲取accessToken url 
  20.              */ 
  21.             String accessTokenUrl = gitHubProperties.getAccesstokenUrl() 
  22.                     .replace("clientId", gitHubProperties.getClientId()) 
  23.                     .replace("clientSecret", gitHubProperties.getClientSecret()) 
  24.                     .replace("authorize_code", code); 
  25.  
  26.             /** 
  27.              * 返回結(jié)果中直接返回token 
  28.              */ 
  29.             String result = OkHttpClientUtil.sendByGetUrl(accessTokenUrl); 
  30.             log.info(" 請求 token 結(jié)果:{}", result); 
  31.  
  32.             String accessToken = null
  33.             Pattern p = Pattern.compile("=(\\w+)&"); 
  34.             Matcher m = p.matcher(result); 
  35.             while (m.find()) { 
  36.                 accessToken = m.group(1); 
  37.                 log.info("令牌token:{}", m.group(1)); 
  38.                 break; 
  39.             } 
  40.  
  41.             /** 
  42.              * 成功獲取token后,開始請求用戶信息 
  43.              */ 
  44.             String userInfoUrl = gitHubProperties.getUserUrl().replace("accessToken", accessToken); 
  45.  
  46.             String userResult = OkHttpClientUtil.sendByGetUrl(userInfoUrl); 
  47.  
  48.             log.info("用戶信息:{}", userResult); 
  49.  
  50.             UserInfo userInfo = JSON.parseObject(userResult, UserInfo.class); 
  51.  
  52.             redirectHome += "?name=" + userInfo.getName(); 
  53.  
  54.         } catch (Exception e) { 
  55.             log.error("授權(quán)回調(diào)異常={}", e); 
  56.         } 
  57.         return new ModelAndView(new RedirectView(redirectHome)); 
  58.     } 

最后我們動圖看一下整體的授權(quán)流程,由于GitHub的訪問速度比較慢,偶爾會有請求超時的現(xiàn)象。

在這里插入圖片描述

 

線上預(yù)覽地址:http://47.93.6.5/login ,歡迎體驗~

項目 GitHub 地址:https://github.com/chengxy-nds/fire.git

 

總結(jié)

從整個GitHub授權(quán)登錄的過程來看,OAuth2.0的授權(quán)碼模式還是比較簡單的,搞懂了一個GitHub的登錄,像微信、圍脖其他三方登錄也就都會了,完全是大同小異的東西,感興趣的同學(xué)可以試一試。

 

責(zé)任編輯:武曉燕 來源: 程序員內(nèi)點事
相關(guān)推薦

2015-11-05 16:44:37

第三方登陸android源碼

2021-02-04 10:12:50

程序員SQLIBM

2021-12-28 16:54:03

2021-12-06 09:44:30

鴻蒙HarmonyOS應(yīng)用

2017-11-28 15:29:04

iPhone X網(wǎng)頁適配

2021-05-24 10:50:10

Git命令Linux

2021-07-16 06:56:50

授權(quán)機制Session

2011-11-22 08:59:30

虛擬化虛擬桌面windows虛擬桌面

2020-06-16 10:57:20

搭建

2024-08-28 08:42:21

API接口限流

2024-03-04 10:36:39

2023-03-24 16:31:55

2016-07-22 15:12:12

Win10技巧重裝

2019-03-04 11:24:52

存儲

2015-01-20 17:01:30

Android源碼QQdemo

2021-02-26 10:21:35

比特幣投資金融

2020-04-20 10:47:57

Redis數(shù)據(jù)開發(fā)

2023-08-26 21:42:08

零拷貝I/O操作

2021-12-27 07:31:37

JavaNeo4J數(shù)據(jù)庫

2023-07-27 08:26:36

零拷貝I/O操作
點贊
收藏

51CTO技術(shù)棧公眾號