使用SpringBoot+OkHttp+fastjson實(shí)現(xiàn)Github的OAuth第三方登錄
一、在GitHub上創(chuàng)建一個(gè)OAuth
二、OAuth的原理
三、OkHttp的使用
1.Post
代碼示例
//官方文檔: public static final MediaType JSON // = MediaType.get("application/json; charset=utf-8"); MediaType mediaType = MediaType.get("application/json; charset=utf-8");//去掉前綴,并且修改MediaType對(duì)象名,因?yàn)橐粫?huì)使用fastjson變量會(huì)重名 OkHttpClient client = new OkHttpClient();//第二句照抄 RequestBody body = RequestBody.create(json,mediaType);//直接復(fù)制方法體中的內(nèi)容 Request request = new Request.Builder() .url("")//填寫要發(fā)送請(qǐng)求的地址 .post(body) .build(); try (Response response = client.newCall(request).execute()) { return response.body().string();//返回的字符串(json) }
2.Get
代碼示例
OkHttpClient client = new OkHttpClient();//同上 Request request = new Request.Builder()//直接復(fù)制方法體中的內(nèi)容 .url(url)//同上 .build(); try (Response response = client.newCall(request).execute()) { return response.body().string();//同上 }
四、fastJson的使用
JSON.toJSONString(實(shí)體類)//將實(shí)體類轉(zhuǎn)換為JSON字符串 JSON.parseObject(string, 實(shí)體類.class);//將JSON字符串轉(zhuǎn)換為實(shí)體類
五、代碼示例
前端代碼
<a rel="external nofollow" >Login</a> //scope和state不寫可能會(huì)報(bào)錯(cuò)
@Controller public class AuthorizeController { @Autowired GithubProvider githubProvider; @GetMapping("/callback") public String callback(@RequestParam(name ="code") String code, @RequestParam(name ="state") String state){ AccessTokenDTO accessTokenDTO = new AccessTokenDTO(); accessTokenDTO.setClient_id(""); accessTokenDTO.setClient\_secret(""); accessTokenDTO.setCode(code); accessTokenDTO.setState(state); accessTokenDTO.setRedirect\_uri("https://github.com/login/oauth/access_token"); String token = githubProvider.getAccessToken(accessTokenDTO); GithubUser githubUser = githubProvider.getUser(token); return "index"; } }
@Component public class GithubProvider { public String getAccessToken(AccessTokenDTO accessTokenDTO){ MediaType mediaType = MediaType.get("application/json; charset=utf-8"); OkHttpClient client = new OkHttpClient(); RequestBody body = RequestBody.create(JSON.toJSONString(accessTokenDTO),mediaType);//用fastjson將實(shí)體類轉(zhuǎn)換為json字符串傳入 Request request = new Request.Builder() .url("https://github.com/login/oauth/access_token?cilen_id=xxx&client_secret=xxx"+accessTokenDTO.getCode()+ "&redirect_uri=http://127.0.0.1:8080/callback&state=1") .post(body) .build(); try (Response response = client.newCall(request).execute()) { String string = response.body().string(); String token = string.split("&")\[0\].split("=")\[1\]; return token; } catch (IOException e) { e.printStackTrace(); } return null; } public GithubUser getUser(String token){ OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("https://api.github.com/user?access_token="+token) .build(); try (Response response = client.newCall(request).execute()) { String string = response.body().string(); GithubUser githubUser = JSON.parseObject(string, GithubUser.class);//用fastjson將json字符串轉(zhuǎn)換為實(shí)體類 return githubUser; } catch (IOException e) { e.printStackTrace(); } return null; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Minio與SpringBoot使用okhttp3問(wèn)題解決
- SpringBoot Java后端實(shí)現(xiàn)okhttp3超時(shí)設(shè)置的方法實(shí)例
- 基于springboot的RestTemplate、okhttp和HttpClient對(duì)比分析
- Android使用OKhttp3實(shí)現(xiàn)登錄注冊(cè)功能+springboot搭建后端的詳細(xì)過(guò)程
- Android的簡(jiǎn)單前后端交互(okHttp+springboot+mysql)
- SpringBoot 配置 okhttp3的操作
- SpringBoot中OKHttp和壓縮文件的使用實(shí)戰(zhàn)教程
相關(guān)文章
Java多線程實(shí)現(xiàn)模擬12306火車站售票系統(tǒng)
12360火車票售票系統(tǒng)基本上大家都用過(guò),那你知道是怎么實(shí)現(xiàn)的嗎,今天我們就模擬12306火車站售票系統(tǒng)來(lái)實(shí)現(xiàn),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05struts2.3.24+spring4.1.6+hibernate4.3.11+mysql5.5.25開(kāi)發(fā)環(huán)境搭建圖文
這篇文章主要介紹了struts2.3.24+spring4.1.6+hibernate4.3.11+mysql5.5.25開(kāi)發(fā)環(huán)境搭建圖文教程,感興趣的小伙伴們可以參考一下2016-06-06javaweb如何實(shí)現(xiàn)請(qǐng)求和響應(yīng)
這篇文章主要為大家詳細(xì)介紹了javaweb如何實(shí)現(xiàn)請(qǐng)求和響應(yīng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10mybatis 如何利用resultMap復(fù)雜類型list映射
這篇文章主要介紹了mybatis 如何利用resultMap復(fù)雜類型list映射的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Mybatis實(shí)現(xiàn)自定義的typehandler三步曲
這篇文章主要介紹了Mybatis實(shí)現(xiàn)自定義的typehandler三步曲的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07