Okhttp3实现GitHub第三方登录

Okhttp3具体简介及使用:https://www.jianshu.com/p/9ee7b47d4f43

GitHubOAuth 服务:

  • 打开 Setting > Developer setting > OAuth applications
  • 点击 Register a new application
  • 填入基本的信息 创建成功

第三方登录具体流程:

  • 用户点击Github登录
  • 应用发送授权请求到GitHub(https://github.com/login/oauth/authorize)
  • 输入账号密码授权登录
  • 返回回调地址(code:授权码) http://?/oauth/callback?code=?
  • post请求获取access_token(https://github.com/login/oauth/access_token)并带上client_id,client_secret,code参数
  • 返回access_token 并请求用户信息 返回用户信息(https://api.github.com/user?access_token=xxx)

具体例子代码:

  • 用户返回信息类
public class GithubUser {
    private String name;
    private Long id;
    private String bio;
    private String avatarUrl;
}
  • OAuth参数类
public class AccessTokenDTO {
    private String client_id;
    private String client_secret;
    private String code;
    private String redirect_url;
    private String state;

}
  • 实现类
    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(mediaType, JSON.toJSONString(accessTokenDTO));
            Request request = new Request.Builder()
                        .url("https://github.com/login/oauth/access_token")
                        .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 (Exception e) {
                    e.printStackTrace();
                }
                return null;
        }
        public GithubUser getUser(String accessToken){
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url("https://api.github.com/user?access_token="+accessToken)
                    .build();
            try {
                Response response = client.newCall(request).execute();
                String string = response.body().string();
                GithubUser githubUser = JSON.parseObject(string, GithubUser.class);
                return githubUser;
            } catch (IOException e) {
                return null;
            }
        }
    }
    
    
    AccessTokenDTO accessTokenDTO = new AccessTokenDTO();
            accessTokenDTO.setClient_id("XXX");
            accessTokenDTO.setClient_secret("XXX");
            accessTokenDTO.setCode(code);
            accessTokenDTO.setRedirect_url("http://XXX/callback");
            accessTokenDTO.setState(state);
            String accessToken = githubProvider.getAccessToken(accessTokenDTO);
            GithubUser githubUser = githubProvider.getUser(accessToken);
            if(githubUser != null && githubUser.getId() !=null){
                User user = new User();
                String token = UUID.randomUUID().toString();
                user.setToken(token);
                user.setName(githubUser.getName());
                user.setAccountId(String.valueOf(githubUser.getId()));
                user.setAvatarUrl(githubUser.getAvatarUrl());
                userService.createOrUpdate(user);
                response.addCookie(new Cookie("token",token));
                return "redirect:/";
            }else{
                //登录失败 重新登录
                return "redirect:/";
            }
        }

     

你可能感兴趣的:(Github)