当前位置:网站首页 > 姓字取名 > 正文

qq网站 直接登录(qq网页登录入口_在线qq登录)

评论:0 发布时间: 2024年02月14日 18:34 浏览: 67

前期准备

1、首先开发者需要登录QQ互联,进行开发者认证,这里需要

个人基本信息

一张手持身份证的张片

2、进入应用管理页面,依次点击:应用管理 -> 网站应用 -> 创建应用,应用信息提交后,等待审核通过即可,这一步我们需要注意的是:

qq网站 直接登录(qq网页登录入口_在线qq登录)-第1张图片

网站域名需要提前备案

网站信息要和备案信息一致

QQ登录实现

这里为了简单,我们使用JustAuth来实现QQ登录,该项目集成了Github、Gitee、QQ、微博等等第三方登录,号称史上最全的整合第三方登录的开源库。

另外为了方便演示,就不使用SpringBoot了,只用Vert.x搭建简单的服务。

1、导入依赖,其中hutool是一个工具类库

????

cn.hutool

????

hutool-all

????

5.3.3

????

me.zhyd.oauth

????

JustAuth

????

1.15.2-alpha

????

io.vertx

????

vertx-core

????

3.2.0

????

io.vertx

????

vertx-web

????

3.2.0

2、实现服务端代码

package?com.qianyu;import?cn.hutool.json.*;import?io.vertx.core.*;import?io.vertx.core.http.*;import?io.vertx.ext.web.*;import?me.zhyd.oauth.config.*;import?me.zhyd.oauth.model.*;import?me.zhyd.oauth.request.*;import?me.zhyd.oauth.utils.*;public?class?Server?{????private?static?AuthQqRequest?authQqRequest;????private?static?AuthRequest?createAuthRequest()?{????????if?(authQqRequest?==?null)?{????????????authQqRequest?=?new?AuthQqRequest(AuthConfig.builder()????????????????????.clientId("你的client?id")????????????????????.clientSecret("你的client?secret")????????????????????.redirectUri("你的回调地址")????????????????????.build());????????}????????return?authQqRequest;????}????public?static?void?main(String[]?args)?{????????Vertx?vertx?=?Vertx.vertx();????????Router?router?=?Router.router(vertx);????????router.get("/comm/user/callback").blockingHandler(context?->?{????????????HttpServerRequest?req?=?context.request();????????????AuthCallback?callback?=?new?AuthCallback();????????????callback.setCode(req.getParam("code"););????????????callback.setState(req.getParam("state"));????????????AuthRequest?authRequest?=?createAuthRequest();????????????AuthResponse?auRes?=?authRequest.login(callback);????????????HttpServerResponse?res?=?context.response();????????????res.putHeader("Content-Type","text/json;charset=utf-8");????????????res.end(JSONUtil.toJsonStr(auRes));????????});????????router.get("/login").blockingHandler(context?->?{????????????String?url?=?createAuthRequest().authorize(AuthStateUtils.createState());????????????HttpServerResponse?res?=?context.response();????????????res.putHeader("location",url);????????????res.setStatusCode(302);????????????res.end();????????});????????HttpServer?httpServer?=?vertx.createHttpServer();????????httpServer.requestHandler(router::accept);????????httpServer.listen(8886);????}}

qq网站 直接登录(qq网页登录入口_在线qq登录)-第2张图片

代码很好理解,主要可以分为以下几个步骤

构建一个QQ登录的工具类,监听两个路由

当我们访问/login的时候,生成登录地址,并且重定向到登录地址

当我们登录之后,系统跳往回调地址,即/comm/user/callback,在这里我们获取参数code和state封装成AuthCallback对象,执行登录方法

登录成功后会返回用户信息,格式如下:

登录成功后返回的用户信息

需要注意的是:创建AuthQqRequest对象的时候,必须是单例,也就是说,必须保证生成登录地址的对象的执行登录方法的对象是同一个。

TAGS:

Powered By Z-BlogPHP,Theme By 冀ICP备2022013555号,Copyright Your WebSite.Some Rights Reserved.