用户通过QQ扫码就登录了咱们的网站,B格拉高了,减少了输入密码的烦恼
首先需要去QQ互联申请应用
填写网站的相关信息,以及回调地址,需要进行审核。
申请流程暂时不说了,百度一下挺多申请失败案例的解决方案的,你懂的现在越来越严格了,甚至一个错别字都不让有。

1、根据QQ互联申请到的信息,请求QQ生成登录二维码(扫码页面)
2、QQ服务器校验这些东西,带着code去调咱们的服务器(QQ互联的回调地址就是咱们的服务器访问地址)
4、拿到code去拿access_token,接着去拿open_id,在接着去拿userInfo
5、完事之后直接跳转前端,将信息带给前端完成登录了!
1、使用统一暗号(state)进行三方面的交流
方案如图所示

2、在前端请求信息的时候,生成一个state,并且QQ在回调的时候也带着这个state值,后端处理用户信息的时候,用这个state存储用户信息
3、后端重定向到前端的时候,前端主动在调一次后端(带这state)
4、后端可以通过state找到用户信息了,那就登录成功,将用户信息返回给前端。
扫码登录地址
https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=102009021&redirect_uri=http%3A%2F%2Fcilicili.top%2Fqqcalback&state=cilicilicilicili
根据code获取AccessToken
public String getAccessToken(QqAccessTokenBo qqAccessTokenDTO) {
MediaType mediaType = MediaType.get("application/x-www-form-urlencoded; charset=utf-8");
OkHttpClient client = new OkHttpClient();
String s = "grant_type=" + qqAccessTokenDTO.getGrant_type() + "&code=" + qqAccessTokenDTO.getCode() + "&client_id=" + qqAccessTokenDTO.getClient_id() + "&client_secret=" + qqAccessTokenDTO.getClient_secret() + "&redirect_uri=" + qqAccessTokenDTO.getRedirect_uri();
RequestBody body = RequestBody.create(mediaType, s);
Request request = new Request.Builder()
.url("https://graph.qq.com/oauth2.0/token")
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
String string = response.body().string();
System.out.println(string);
return string;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
根据access_token获取open_id
public String getOpenID(String accessToken) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://graph.qq.com/oauth2.0/me?access_token=" + accessToken)
.build();
try {
Response response = client.newCall(request).execute();
String string = response.body().string();
String jsonString = string.split(" ")[1].split(" ")[0];
//System.out.println(jsonString);
JSONObject obj = JSONObject.parseObject(jsonString);
String openid = obj.getString("openid");
//System.out.println(openid);
return openid;
} catch (IOException e) {
}
return null;
}
根据openId获取用户信息
public QqUserDTO getUser(String accessToken, String oauth_consumer_key, String openid) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://graph.qq.com/user/get_user_info?access_token=" + accessToken + "&oauth_consumer_key=" + oauth_consumer_key + "&openid=" + openid)
.build();
try {
Response response = client.newCall(request).execute();
String string = response.body().string();
System.out.println(string);
QqUserDTO qqUserDTO = JSON.parseObject(string, QqUserDTO.class);
qqUserDTO.setOpenId(openid);
return qqUserDTO;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}