软件编程
位置:首页>> 软件编程>> java编程>> 使用SpringBoot+OkHttp+fastjson实现Github的OAuth第三方登录

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

作者:技不如人甘拜下风  发布时间:2023-03-18 01:27:34 

标签:SpringBoot,OkHttp,fastjson

一、在GitHub上创建一个OAuth

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

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

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

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

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

二、OAuth的原理

Spring官方文档

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

三、OkHttp的使用

OkHttp官方网站

1.Post

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

代码示例


//官方文档:    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

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

代码示例


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不写可能会报错

@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将实体类转换为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;
}

}

来源:https://segmentfault.com/a/1190000021669924

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com