Springboot集成QQ平台互联授权登入应用

1、QQ互联文档地址:

QQ互联平台文档

注册地址:

个人开发
在这里插入图片描述
Springboot集成QQ平台互联授权登入应用_第1张图片

2、OAuth 2.0

是目前最流行的授权机制,用来授权第三方应用,获取用户数据。
OAuth 2.0文档

配置文件模板:()

app_ID = 101420900
app_KEY = bd56a336f6ac49a65005595c2a41201a
redirect_URI = http://127.0.0.1/qqLoginCallback
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

应用场景

在APP或者网页接入一些第三方应用时,时常会需要用户登录另一个合作平台,比如QQ,微博,微信的授权登录,第三方应用通过oauth2方式获取用户信息

第三方平台QQ互联

1、根据sdk4j生成授权链接地址
2、执行回调地址
3、两种场景:
1、数据库查询不到openid,
获取accessToken
然后根据accessToken得到openid,
会跳转进入另一个登入地址,
生成token,数据库存入生成的openid
2、数据库能查询到openid,
获取accessToken
然后根据accessToken得到openid,
返回用户信息,获取token令牌
4、进入首页

平台互联调用步奏:
Springboot集成QQ平台互联授权登入应用_第2张图片

控制层:

 // 生成qq授权登录链接
    @RequestMapping("/locaQQLogin")
    public String locaQQLogin(HttpServletRequest reqest) throws QQConnectException {
        String authorizeURL = new Oauth().getAuthorizeURL(reqest);
        return "redirect:" + authorizeURL;

    }
    //回调地址
    @RequestMapping("/qqLoginCallback")
    public String qqLoginCallback(HttpServletRequest reqest, HttpServletResponse response,HttpSession httpSession) throws QQConnectException {

        // 1.获取授权码COde
        // 2.使用授权码Code获取accessToken
        AccessToken accessTokenOj = new Oauth().getAccessTokenByRequest(reqest);
        if (accessTokenOj == null) {
            reqest.setAttribute("error", "QQ授权失败");
            return "error";
        }
        String accessToken = accessTokenOj.getAccessToken();
        if (accessToken == null) {
            reqest.setAttribute("error", "accessToken为null");
            return "error";
        }
        // 3.使用accessToken获取openid
        OpenID openidOj = new OpenID(accessToken);
        String userOpenId = openidOj.getUserOpenID();
        // 4.调用会员服务接口 使用userOpenId 查找是否已经关联过账号
        ResponseBase openUserBase = memberService.findByOpenIdUser(userOpenId);
        if(openUserBase.getCode().equals(Constants.HTTP_RES_CODE_201)){
            // 5.如果没有关联账号,跳转到关联账号页面
            httpSession.setAttribute("qqOpenid", userOpenId);
            return QQRELATION;
        }
        //6.已经绑定账号  自动登录 将用户token信息存放在cookie中
        LinkedHashMap dataTokenMap = (LinkedHashMap) openUserBase.getData();
        String memberToken=(String) dataTokenMap.get("memberToken");
        setCookie(memberToken, response);
        return INDEX;
    }

    // 没有关联openid,进行登录请求具体提交实现
    @RequestMapping(value = "/qqRelation", method = RequestMethod.POST)
    public String qqRelation(UserEntity userEntity, HttpServletRequest request, HttpServletResponse response, HttpSession httpSession) {
        // 1.获取openid
        String qqOpenid=(String) httpSession.getAttribute("qqOpenid");
        if(StringUtils.isEmpty(qqOpenid)){
            request.setAttribute("error", "没有获取到openid");
            return "error";
        }

        // 2.调用登录接口,获取token信息
        userEntity.setOpenid(qqOpenid);
        ResponseBase loginBase = memberService.qqLogin(userEntity);
        if (!loginBase.getCode().equals(Constants.HTTP_RES_CODE_200)) {
            request.setAttribute("error", "账号或者密码错误!");
            return LOGIN;
        }

        LinkedHashMap loginData = (LinkedHashMap) loginBase.getData();
        String memberToken = (String) loginData.get("memberToken");
        if (StringUtils.isEmpty(memberToken)) {
            request.setAttribute("error", "会话已经失效!");
            return LOGIN;
        }
        // 3.将token信息存放在cookie里面
        setCookie(memberToken, response);
        return INDEX;
    }

你可能感兴趣的:(SpringBoot)