自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

[[333729]]

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

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

在這里插入圖片描述

 

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

一、授權(quán)流程

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

在這里插入圖片描述

 

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

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

二、身份注冊(cè)

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

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

注冊(cè) 傳送門 https://github.com/settings/applications/1334665,有幾個(gè)必填項(xiàng)。

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

 

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

在這里插入圖片描述

 

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

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

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

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

前端 vue 的邏輯也非常簡(jiǎn)單,只需要 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> 

 

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

在這里插入圖片描述

 

2、獲取令牌

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

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

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

  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ù)傳遞給前端,可以通過(guò) url 重定向到前端頁(yè)面,將數(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)點(diǎn)事"
  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.          * 重新到前端主頁(yè) 
  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(" 請(qǐng)求 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后,開始請(qǐng)求用戶信息 
  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.     } 

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

在這里插入圖片描述

 

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

項(xiàng)目 GitHub 地址:https://github.com/chengxy-nds/fire.git

 

總結(jié)

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

 

責(zé)任編輯:武曉燕 來(lái)源: 程序員內(nèi)點(diǎn)事
相關(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)用

2021-07-16 06:56:50

授權(quán)機(jī)制Session

2017-11-28 15:29:04

iPhone X網(wǎng)頁(yè)適配

2021-05-24 10:50:10

Git命令Linux

2024-03-04 10:36:39

2011-11-22 08:59:30

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

2020-06-16 10:57:20

搭建

2024-08-28 08:42:21

API接口限流

2023-03-24 16:31:55

2015-01-20 17:01:30

Android源碼QQdemo

2019-03-04 11:24:52

存儲(chǔ)

2016-07-22 15:12:12

Win10技巧重裝

2023-12-10 22:07:57

JustAuth授權(quán)登錄庫(kù)

2014-04-08 15:16:00

2021-12-27 07:31:37

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

2023-08-26 21:42:08

零拷貝I/O操作

2020-04-20 10:47:57

Redis數(shù)據(jù)開發(fā)
點(diǎn)贊
收藏

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