栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

网站授权QQ登录

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

网站授权QQ登录

网站授权QQ登录 功能流程

用户通过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;
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1039517.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号