前言:
实现个人网站的第三方登录,qq登录,网站也可以调用腾讯方提供的API实现网站的分享、评论、邀请等功能,即网站的社交化功能。且可以将相关信息同步到QQ空间、腾讯朋友、腾讯微博等平台,建立网站与腾讯各平台的互动关系,利用庞大的QQ用户群来实现网站的快速传播。
官方文档 :https://wiki.connect.qq.com/%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C_oauth2-0
强烈建议配合官方文档学习
大致思路:
1、在QQ互联上创建个人开发者并创建应用(都需要审核认证,大概一到两天),获得APPID、APP Key,链接:
https://connect.qq.com/manage.html#/ (创建的应用审核不通过的情况下只能当前的qq好能测试登录)。
2、在项目里放置qq登录图标或者按钮。
3、编写后端逻辑,用户获得用户信息,存到数据库或者修改。
具体流程(图文):
1创建应用:
2、放置登录按钮,发起登录请求
点击qq登录跳转登录授权页面
跳转页面有两种方式:
(1):打开新的页面,使用a标签 <a href="https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=101717170&redirect_uri=http://127.0.0.1:8080/api/callback&state=dfsdf1232313" target="_blank"></a>
client_id是你创建应用的APPID、redirect_uri是你的回调地址,就是你登录授权后调转的地址。
(2):打开新的小窗口
qq_login(){
var iTop = (window.screen.availHeight - 30 - 500) / 2;
//获得窗口的水平位置
var iLeft = (window.screen.availWidth - 10 - 650) / 2;
window.open("https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=101717170&redirect_uri=http://127.0.0.1:8080/api/callback&state=dfsdf1232313", "TencentLogin", "width=650,height=500,top="+iTop+",left="+iLeft+",menubar=0,scrollbars=1,resizable=1,status=1,titlebar=0,toolbar=0,location=1");
}
3、根据回调带回来的code,获取Access Token,然后根据Access Token获取openid,最后根据openid获取用户信息
引入依赖
<!-- 以下是 qq wx 联合登陆需要的相关依赖工具 commons-io, commons-lang3,httpclient,fastjson--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.38</version> </dependency>
@GetMapping("/callback")
public ResponseEntity getAccount(HttpServletRequest request, HttpServletResponse response) {
//client_id 是你的APPID,client_secret是你的APP Key,redirect_uri是你创建应用时填写的网站地址,一般为首页地址
String url1 = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id="
+ "101777990" + "&client_secret=" + "ab040bb67e1a88657cd83f5e073246ba" + "&redirect_uri="
+ "http://127.0.0.1:8080/callback" + "&code=" + request.getParameter("code");
//发送url请求获取数据
try {
URL url = new URL(url1);
HttpURLConnection conn = null;
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("GET");
InputStream inStream = conn.getInputStream();
byte[] data = toByteArray(inStream);
String result = new String(data, "UTF-8");
System.out.println(result);
//使用正则表达式解析网址
Pattern p = Pattern.compile("access_token=(\\w*)&");
Matcher m = p.matcher(result);
m.find();
//得到access_token
String access_token = m.group(1);
//System.out.println(access_token);
String url2 = "https://graph.qq.com/oauth2.0/me?access_token=" + access_token;
url = new URL(url2);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("GET");
inStream = conn.getInputStream();
data = toByteArray(inStream);
String result2 = new String(data, "UTF-8");
//System.out.println(result2);
p = Pattern.compile("client_id\":\"(\\w*)\",");
m = p.matcher(result2);
m.find();
String appid = m.group(1);
p = Pattern.compile("openid\":\"(\\w*)\"");
m = p.matcher(result2);
m.find();
//得到openid
String openid = m.group(1);
System.out.println(openid);
//判断数据库中是否有此
// usrShow = usrService.findUsrByid(openid);
//通过openid计算token
// String token = getMD5(openid);
//如果数据库中没有
if (true) {
String url3 = "https://graph.qq.com/user/get_user_info?access_token=" + access_token
+ "&oauth_consumer_key=" + appid + "&openid=" + openid;
url = new URL(url3);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("GET");
inStream = conn.getInputStream();
data = toByteArray(inStream);
String result3 = new String(data, "UTF-8");
//System.out.println(result3);
//json字符串转化为json对象
JSONObject jsonObject = JSON.parseObject(result3);
String myname = jsonObject.getString("nickname");
System.out.println("user_info:"+jsonObject.toString());
return ResponseEntity.ok(jsonObject.toString());
// myname = nameToDb(myname);
//生成usr对象,并插入数据库
// Usr usr = new Usr("", myname
// , jsonObject.getString("figureurl_2"), jsonObject.getString("gender")
// , access_token, openid, token);
// if (!usrService.insertUsr(usr))
// System.out.println("添加失败");
// //生成usrshow对象
// usrShow = new UsrShow(null, usr.getName()
// , usr.getIcon(), usr.getGender());
}
//改变名称格式
// usrShow.setName(nameToshow(usrShow.getName()));
// //加入session
// sessoin.setAttribute("usrshow", usrShow);
// //cookie中存入token
// Cookie cookie = new Cookie("token", token);//创建新cookie
// cookie.setMaxAge(2592000);// 设置存在时间为一个月
// cookie.setPath("/");//设置作用域
// response.addCookie(cookie);
} catch (IOException e) {
e.printStackTrace();
}
return ResponseEntity.ok().build();
}
4、获得qq用户信息后,可以根据openid判断判断数据库是否存在,然后新增或者修改。
总结:
本篇文章只是实现了qq登录的具体流程,其他的用户登录逻辑需要自己编写,例如用户token的存放,用户退出登录时,用户在session里保存的信息清除等。
本篇文章里的配置数据都需要替换你创建应用后得到的数据。
有不懂的问题随时评论区里咨询我,随时解答