• Java

集成通过github登录网站功能

使用第三方网址登录系统,使用github

工作流程:

  • 页面点击使用github登录,
  • 重定向到回调地址
  • 回调地址后台获取token,获取用户信息,完成登录,将页面重定向到某页面
  • 页面正常显示

github新建App

github设置页,deleveper setting页 新建app,注册回调地址。

html页中增加登录按钮

 <a class="btn btn-link-2" href="#" onclick="git_login()">
     <i class="fa fa-github"></i> Github
 </a>

获取code

const FULL_CHARTER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopgrstuvwxyz';
const git_clientId='269c706502923e741920';
const redirect_uri = 'http://luangeng.leanapp.cn/test';
const git_auth = 'https://github.com/login/oauth/authorize?';
const scope	= 'read';

function getState(){
var state='';
for (var a=0;a<6;a++){
    state+=FULL_CHARTER[Math.floor(Math.random() * 52)];
}
token_storage.setItem("git_state", state);
return state;
}

function git_login(){
    var url=git_auth+'client_id='+git_clientId+'&redirect_uri='+redirect_uri+'&scope='+scope+'&state='+getState();
    window.location = url;
}

获取token

@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String toke = " https://github.com/login/oauth/access_token";
        String user = "https://api.github.com/user?access_token=";
        String state = req.getParameter("state");
        String code = req.getParameter("code");

        OkHttpClient client = new OkHttpClient();
        RequestBody formBody = new FormEncodingBuilder()
                .add("client_id", "269c706502923e741920")
                .add("client_secret", "a8594616a65fb12bc87d5088819b84fcb65ccdc7")
                .add("code", code)
                .add("redirect_uri", "http://luangeng.leanapp.cn/test")
                .add("state", state)
                .build();
        Request request = new Request.Builder()
                .url(toke)
                .post(formBody)
                .build();
        Response response = client.newCall(request).execute();
        String ret = response.body().string();
        String token = ret.substring(ret.indexOf("=") + 1, ret.indexOf("&"));
        //GET
        Request.Builder builder = new Request.Builder();
        builder.url(user + token).get();
        client = new OkHttpClient();
        Call call = client.newCall(builder.build());
        try {
            response = call.execute();
            resp.getWriter().append("ok: ").append("token: " + token + "  ");
        } catch (Exception e) {
            resp.getWriter().append(e.getMessage());
        }
        Gson gson = new Gson();
        User u = gson.fromJson(response.body().string(), User.class);
        }
   }

获取用户信息

后台调用

https://api.github.com/user?access_token=0013d0335c9d38104a55bb2973045160c9648474

返回的数据:

{
  "login": "luangeng",
  "id": 10940569,
  "node_id": "MDQ6VXNlcjEwOTQwNTY5",
  "avatar_url": "https://avatars2.githubusercontent.com/u/10940569?v=4",
  "gravatar_id": "",
  "url": "https://api.github.com/users/luangeng",
  "html_url": "https://github.com/luangeng",
  "followers_url": "https://api.github.com/users/luangeng/followers",
  "following_url": "https://api.github.com/users/luangeng/following{/other_user}",
  "gists_url": "https://api.github.com/users/luangeng/gists{/gist_id}",
  "starred_url": "https://api.github.com/users/luangeng/starred{/owner}{/repo}",
  "subscriptions_url": "https://api.github.com/users/luangeng/subscriptions",
  "organizations_url": "https://api.github.com/users/luangeng/orgs",
  "repos_url": "https://api.github.com/users/luangeng/repos",
  "events_url": "https://api.github.com/users/luangeng/events{/privacy}",
  "received_events_url": "https://api.github.com/users/luangeng/received_events",
  "type": "User",
  "site_admin": false,
  "name": "luangeng",
  "location": "中国 西安",
  "email": "luangeng2015@qq.com",
  "hireable": true,
  "bio": "🍉🍉🍉🍉🍉🍉🍉",
  "public_repos": 45,
  "public_gists": 0,
  "followers": 6,
  "following": 30,
  "created_at": "2015-02-10T12:04:47Z",
  "updated_at": "2018-09-18T01:42:47Z"
}

接下来是绑定用户,确定登录者,完成登录

参考

github帮助文档


相关

最新