轉載 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實現能夠參考