现在的位置: 首页 > 编程语言 > 正文

使用SpringBoot+OkHttp+fastjson实现Github的OAuth第三方登录

2020年02月18日 编程语言 ⁄ 共 2930字 ⁄ 字号 评论关闭

一、在GitHub上创建一个OAuth

二、OAuth的原理

Spring官方文档

三、OkHttp的使用

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对象名,因为一会使用fastjson变量会重名 OkHttpClient client = new OkHttpClient();//第二句照抄 RequestBody body = RequestBody.create(json,mediaType);//直接复制方法体中的内容 Request request = new Request.Builder() .url("")//填写要发送请求的地址 .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()//直接复制方法体中的内容 .url(url)//同上 .build(); try (Response response = client.newCall(request).execute()) { return response.body().string();//同上 }

四、fastJson的使用

JSON.toJSONString(实体类)//将实体类转换为JSON字符串JSON.parseObject(string, 实体类.class);//将JSON字符串转换为实体类

五、代码示例

前端代码

<a href="https://github.com/login/oauth/authorize?client_id=xxx&redirect_uri=http://127.0.0.1:8080/xxx&scope=user&state=1" rel="external nofollow" >Login</a>//scope和state不写可能会报错

@Controllerpublic 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"; } }

@Componentpublic 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将实体类转换为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字符串转换为实体类 return githubUser; } catch (IOException e) { e.printStackTrace(); } return null; } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

以上就上有关使用SpringBoot+OkHttp+fastjson实现Github的OAuth第三方登录的相关介绍,要了解更多springboot, fastjson, OkHttp内容请登录学步园。

抱歉!评论已关闭.