转载 www.byteblogs.com/article/162html
JAVA-网站应用接入GitHub第三方登陆 相对 网站应用接入 QQ 登陆,简单不少,Github 直接建立应用就能够用,不须要长时间的审核前端
GitHub 开发者官方文档:developer.github.com/apps/buildi…vue
(一)准备,建立应用java
(1)开打开发者(不须要审核) 访问:github.com/settings/pr… git
(2)建立应用 github
(3)填写信息 json
(三)后台处理流程 (1)前端请求登陆,无参数 (2)后端重定向到后端
地址:github.com/login/oauth… 参数:client_id=(AppID) redirect_uri=(回调地址) state=(原样返回)api
返回时app
code=(受权码) state=(原样返回)
(3) 返回回调地址,经过 Authorization Code 获取 AccessToken
请求地址:github.com/login/oauth… 参数:client_id=(AppId) client_secret=(密钥) code=(回调地址携带的 code) redirect_uri=(回调地址,和上面回调地址同样)
返回时
access_token=(访问受权码) token_type=bearer(固定)
(4)经过 access_token 获取用户信息
请求地址:api.github.com/user 参数:access_token(返回的访问受权码)
前端vue处理:
window.open
打开窗口export function openWindow(url, title, w, h) {
// Fixes dual-screen position Most browsers Firefox
const dualScreenLeft = window.screenLeft !== undefined ? window.screenLeft : screen.left
const dualScreenTop = window.screenTop !== undefined ? window.screenTop : screen.top
const width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width
const height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height
const left = ((width / 2) - (w / 2)) + dualScreenLeft
const top = ((height / 2) - (h / 2)) + dualScreenTop
const newWindow = window.open(url, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left)
// Puts focus on the newWindow
if (window.focus) {
newWindow.focus()
}
}
复制代码
而后拿到后面返回的数据,去请求登陆
loginGithub () {
// 获取请求的地址 https://github.com/login/oauth/authorize?client_id=xxx
this.$store.dispatch("xxx").then(res => {
openWindow(res.model.authorizeUrl,"github",540,540)
window.addEventListener('message', this.loginGithubHandler, false);
})
},
loginGithubHandler(e) {
let { socialId } = e.data;
if (socialId) {
this.$store.dispatch("xxxx", e.data).then(res =>{
window.removeEventListener('message',this.loginGithubHandler,false)
})
}
}
复制代码
后端java处理:
@Override
public String saveUserByGithub(String code, String state) {
log.debug("code {},state {}", code, state);
GithubOauth githubOauth = new GithubOauth();
String accessToken = githubOauth.getAccessToken(code);
Map<String, Object> objectObjectMap = JsonUtil.parseHashMap(accessToken);
String userInfo = githubOauth.getUserInfo((String) objectObjectMap.get("access_token"));
GithubVO githubVO = JsonUtil.parseObject(userInfo, GithubVO.class);
// 初始化用户
if (usersOpenOauth == null) {
......
}
result.put("socialId", githubVO.getId());
// vue前端获取这个数据,去登陆。
String html = "<head>\n" +
" <meta charset=\"UTF-8\">\n" +
"</head>" +
"<body>\n" +
" <p style=\"text-align: center;\"><h3>登陆中....</h3></p>\n" +
"</body>" +
"\n" +
" window. function () {\n" +
" var message =" + JsonUtil.toJsonString(result) + ";\n" +
" window.opener.parent.postMessage(message, '*');\n" +
" parent.window.close();\n" +
" }\n" +
"\n";
return html;
}
复制代码
private static final String AUTH_URL = "https://github.com/login/oauth/authorize";
private static final String TOKEN_URL = "https://github.com/login/oauth/access_token";
private static final String USER_INFO_URL = "https://api.github.com/user";
public String getAccessToken(String code) {
Map<String, Object> params = new HashMap<>();
params.put("code", code);
params.put("client_id", getClientId());
params.put("client_secret", getClientSecret());
HttpRequest post = HttpRequest.post(TOKEN_URL);
post.body(JsonUtil.toJsonString(params)).contentType("application/json").header(Header.ACCEPT, "application/json");
String result = post.execute().body();
log.debug("github -> getAccessToken -> result -> {}", result);
return result;
}
复制代码
这样基本就能够了。
vue实现能够参考