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协议一样,就可以成功转换,如果还有什么不懂的欢迎再评论区留言。

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