qq登录功能实现,如何实现第三方QQ登录
关于【qq登录功能实现】,如何实现第三方QQ登录,今天乾乾小编给您分享一下,如果对您有所帮助别忘了关注本站哦。
1.2 扫码登录现在的第三方平台基本都支持扫码登录,因为扫码登录更加安全,
而且不用记账号和密码
如:微信、qq、支付宝
2. QQ第三方登录2.1 qq互联
https://connect.qq.com/
2.2 注册开发者
2.3 文档
3. OAuth2.0 介绍3.1. QQ登录OAuth2.0总体处理流程
QQ登录OAuth2.0总体处理流程如下:
Step1:申请接入,获取appid和apikey;
Step2:开发应用,并设置协作者帐号进行测试联调;
Step3:放置QQ登录按钮;
Step4:通过用户登录验证和授权,获取Access Token;
Step5:通过Access Token获取用户的openid;
Step6:调用OpenAPI,来请求访问或修改用户授权的资源。
4. 项目集成qq登录4.1 maven导入包
<!-- QQ第三方登录-->
<dependency>
<groupId>net.gplatform</groupId>
<artifactId>Sdk4J</artifactId>
<version>2.0</version>
</dependency>
4.2. 配置
最好查看qq给出的demo例子
创建 qqconnectconfig.properties放在resources根目录下
app_ID = 101543517
app_KEY = 3d43255254a6f50876661fe2bec86684
redirect_URI = http://javatv.cn/loginAfter
scope = get_user_info,add_topic,add_one_blog,add_album,upload_pic,list_album,add_share,check_page_fans,add_t,add_pic_t,del_t,get_repost_list,get_info,get_other_info,get_fanslist,get_idollist,add_idol,del_ido,get_tenpay_addr
baseURL = https://graph.qq.com/
getUserInfoURL = https://graph.qq.com/user/get_user_info
accessTokenURL = https://graph.qq.com/oauth2.0/token
authorizeURL = https://graph.qq.com/oauth2.0/authorize
getOpenIDURL = https://graph.qq.com/oauth2.0/me
addTopicURL = https://graph.qq.com/shuoshuo/add_topic
addBlogURL = https://graph.qq.com/blog/add_one_blog
addAlbumURL = https://graph.qq.com/photo/add_album
uploadPicURL = https://graph.qq.com/photo/upload_pic
listAlbumURL = https://graph.qq.com/photo/list_album
addShareURL = https://graph.qq.com/share/add_share
checkPageFansURL = https://graph.qq.com/user/check_page_fans
addTURL = https://graph.qq.com/t/add_t
addPicTURL = https://graph.qq.com/t/add_pic_t
delTURL = https://graph.qq.com/t/del_t
getWeiboUserInfoURL = https://graph.qq.com/user/get_info
getWeiboOtherUserInfoURL = https://graph.qq.com/user/get_other_info
getFansListURL = https://graph.qq.com/relation/get_fanslist
getIdolsListURL = https://graph.qq.com/relation/get_idollist
addIdolURL = https://graph.qq.com/relation/add_idol
delIdolURL = https://graph.qq.com/relation/del_idol
getTenpayAddrURL = https://graph.qq.com/cft_info/get_tenpay_addr
getRepostListURL = https://graph.qq.com/t/get_repost_list
version = 2.0.0.0
4.3. 建包oauth2并扫描
<context:component-scan base-package="cn.itsource.oauth2" />
4.4. 编写qqcontroller
@Controllerpublic class QQController { @Autowired RestTemplate restTemplate; @Autowired UserService userService; // 将页面重定向到qq第三方的登录页面 @requestMapping(value = "/qqLogin") public void qqLogin(HttpServletRequest request, HttpServletResponse response) { try { response.setContentType("text/html;charset=utf-8"); response.sendRedirect(new Oauth().getAuthorizeURL(request));//将页面重定向到qq第三方的登录页面 } catch (Exception e) { e.printStackTrace(); } } // 登录之后回调地址 @RequestMapping(value = "/loginAfter") public String qqLoginAfter(HttpServletRequest request, HttpServletResponse response) { try { System.err.println("============登录之后回调地址=========="); AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request); String accessToken = null, openID = null; long tokenExpireIn = 0L; if (accessTokenObj.getAccessToken().equals("")) {// 我们的网站被CSRF攻击了或者用户取消了授权 System.out.print("没有获取到响应参数"); } else { accessToken = accessTokenObj.getAccessToken(); tokenExpireIn = accessTokenObj.getExpireIn(); // 利用获取到的accessToken 去获取当前用的openid -------- start OpenID openIDObj = new OpenID(accessToken); openID = openIDObj.getUserOpenID(); System.err.println("===============" openID); // 获取qq信息 String url = "https://graph.qq.com/user/get_user_info?access_token=" accessToken "&oauth_consumer_key=101543517" "&openid=" openID "&format=json "; String json = restTemplate.getForObject(url, String.class); ObjectMapper objectMapper = new ObjectMapper(); HashMap map = objectMapper.readValue(json, HashMap.class); System.err.println(map); System.err.println(map.get("figureurl_qq_1")); // 如果用户表没有当前openid,那就添加用户,如果就有就直接跳转主界面main.jsp\ User user = new User(); user.setQqOpenId(openID); User u = userService.queryByParam(user); if (u != null) { System.out.println(u); return "main"; } else { // 添加用户 user.setHeadImg((String) map.get("figureurl_qq_1")); user.setLoginName(UUID.randomUUID().toString()); user.setLoginPwd("123456"); userService.insert(user); return "main"; } } } catch (Exception e) { e.printStackTrace(); } return null; }}
4.5 登录页面加入QQ登录
页面加入
绑定事件
4.6. 修改 hosts
C:\Windows\System32\drivers\etc
修改hosts文件,在末尾加入
127.0.0.1 javatv.cn
本文关键词:qq登录功能流程图,登录qq使用什么功能,qq直接登录,qq登录状态可以设置吗,qq用户登陆。这就是关于《qq登录功能实现,如何实现第三方QQ登录》的所有内容,希望对您能有所帮助!更多的知识请继续关注《犇涌向乾》百科知识网站:http://www.029ztxx.com!
版权声明: 本站仅提供信息存储空间服务,旨在传递更多信息,不拥有所有权,不承担相关法律责任,不代表本网赞同其观点和对其真实性负责。如因作品内容、版权和其它问题需要同本网联系的,请发送邮件至 举报,一经查实,本站将立刻删除。