http形式的oauth2客戶端

http形式的oauth2客戶端

上篇博客講到springboot+oauth2搭建oauth2客戶端和服務端,但是現在很多公司都是前後端分離,這樣可能無法使用java作爲oauth2客戶端,那怎麼辦呢?其實,oauth2就是一個協議,org.apache.oltu.oauth2包裝的再好,本質上還是使用http交互,那麼我用wireshark抓包工具來探索一下org.apache.oltu.oauth2包裝下的http協議是什麼樣子的。

首先瀏覽器到客戶端的協議簡單,就一個get請求:
瀏覽器->requestServerCode
協議:http://localhost:8081/oauthclient01/server/requestServerCode

requestServerCode->responseCode
協議:http://localhost:8082/oauthserver/responseCode?response_type=type GET

responseCode->callbackCode
協議:http://localhost:8082/oauthserver/responseCode?code=code GET

下面是比較複雜的,看一下wireshark抓到的包:
callbackCode->responseAccessToken

POST /oauthserver/responseAccessToken?code=authorizationCode&grant_type=authorization_code&client_secret=clientSecret&redirect_uri=http%3A%2F%2Flocalhost%3A8081%2Foauthclient01%2Fserver%2FaccessToken&client_id=clientId HTTP/1.1
Content-Type: application/x-www-form-urlencoded
User-Agent: Java/1.8.0_161
Host: 192.168.1.99:8082
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Length: 4

返回:

HTTP/1.1 200 
Content-Type: text/html;charset=UTF-8
Content-Length: 51
Date: Tue, 08 Oct 2019 08:28:43 GMT

{"access_token":"bd8a0c17230e81b64534e2c91ff1d101"}

由此可知,雖然callbackCode->responseAccessToken是post請求,但是參數也是以表單的形式拼接在url後面,並且以json的形式返回。所以我們請求responseAccessToken的時候只要按照上面的格式發送請求,接收請求就可以了。

callbackCode->accessToken
wireshark抓的包:

GET /oauthserver/userInfo?access_token=bd8a0c17230e81b64534e2c91ff1d101 HTTP/1.1
User-Agent: Java/1.8.0_161
Host: 192.168.1.99:8082
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive

返回

HTTP/1.1 200 
Content-Type: text/html;charset=UTF-8
Content-Length: 60
Date: Tue, 08 Oct 2019 08:28:46 GMT

bd8a0c17230e81b64534e2c91ff1d101---0.5888943574176393----tom

請求get請求,參數接問號後面,返回的是文本
下面我用POSTMAN mock了一下請求資源文件:
在這裏插入圖片描述
抓到的包:
請求:

GET /oauthserver/userInfo?access_token=bd8a0c17230e81b64534e2c91ff1d101 HTTP/1.1
User-Agent: PostmanRuntime/7.17.1
Accept: */*
Cache-Control: no-cache
Postman-Token: 86e33bd3-f4c9-428f-b41a-74b82997cfab
Host: 192.168.1.99:8082
Accept-Encoding: gzip, deflate
Connection: keep-alive

響應:

HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 61
Date: Tue, 08 Oct 2019 08:38:38 GMT

bd8a0c17230e81b64534e2c91ff1d101---0.08481990442501297----tom

雖然postman多帶了一些參數,不過還是正確返回了token

所以雖然請求經過了

OAuthTokenRequest oauthRequest = new OAuthTokenRequest(request);

這樣的封裝,但是隻要http協議一樣,就可以成功轉換,如果還有什麼不懂的歡迎再評論區留言。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章