第三方登录服务 Web OAuth 示例
该项目只是用户走通整个第三方的流程,并写出对应的思路,代码不提供参考价值。
请注意修改
src/config/adapter.js
中 MySQL 数据库配置。
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for `oauth`
-- ----------------------------
DROP TABLE IF EXISTS `oauth`;
CREATE TABLE `oauth` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`type` char(50) NOT NULL COMMENT '类型,有 qq、github、weibo',
`uid` varchar(255) NOT NULL COMMENT '唯一标识',
`info` varchar(255) DEFAULT '' COMMENT '其他信息,JSON 形式',
`user_id` int(11) NOT NULL COMMENT '用户ID',
`create_time` bigint(13) NOT NULL COMMENT '创建时间',
`name` varchar(255) DEFAULT NULL COMMENT '显示名称',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`username` varchar(255) NOT NULL COMMENT '用户名',
`password` varchar(255) NOT NULL COMMENT '密码',
`create_time` bigint(13) NOT NULL COMMENT '创建时间',
`update_time` bigint(13) NOT NULL COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4;
SET FOREIGN_KEY_CHECKS = 1;
当然你可以根据自己实际项目需求修改,比如:
https://github.com/login/oauth/authorize?
code
code
请求 https://github.com/login/oauth/access_token
来获取 access_token
,有个小坑是,在想使用 JSON 返回值时,需要在请求头里添加 'Accept': 'application/json'
access_token
请求 https://api.github.com/
获取用户信息:id
- 唯一标识name
- 显示名称avatar_url
- 用户头像参考链接:https://developer.github.com/apps/building-oauth-apps/authorization-options-for-oauth-apps
https://graph.qq.com/oauth2.0/show?which=Login&display=
,需要区分下 PC 端和移动端传,参数 display
不一样,需要单独处理下redirect_uri
中,并携带 code
code
请求 https://graph.qq.com/oauth2.0/token?
获取 access_token
,有个大坑是成功时返回 access_token=xxx
,错误时返回 callback( {code: xxx} )
,好尴尬。。。access_token
请求 https://graph.qq.com/oauth2.0/me?
获取 openid
,而这里又是返回个 callback({"openid": "1"})
access_token
和 openid
请求 https://graph.qq.com/user/get_user_info
来获取用户信息,最终为:openid
- 唯一标识nickname
- 显示名称figureurl_qq_2
- 用户头像参数链接:http://wiki.connect.qq.com/开发攻略_server-side
https://api.weibo.com/oauth2/authorize
redirect_uri
中,并携带 code
code
请求 https://api.weibo.com/oauth2/access_token?
获取 access_token
access_token
请求 https://api.weibo.com/2/users/show.json
获取用户信息,最终为:access_token
- 唯一标识screen_name
- 显示名称avatar_hd
- 用户头像参考链接:http://open.weibo.com/wiki/授权机制说明
http://openapi.baidu.com/oauth/2.0/authorize?
,需要区分下 PC 端和移动端传,参数 display
不一样,需要单独处理下redirect_uri
中,并携带 code
code
请求 https://openapi.baidu.com/oauth/2.0/token
获取 access_token
access_token
请求 https://openapi.baidu.com/rest/2.0/passport/users/getInfo
来获取用户信息,最终为:userid
- 唯一标识username
- 显示名称http://tb.himg.baidu.com/sys/portrait/item/${userinfo.portrait}
- 用户头像参考链接:http://developer.baidu.com/wiki/index.php?title=docs/oauth/application
# 导入 MySQL 数据
...
# 申请第三方开发平台
...
# 克隆代码
git clone https://github.com/xuexb/web-oauth-app.git && cd web-oauth-app
# 修改数据库配置
vi src/config/adapter.js
# 修改配置信息
vi src/config/config.js
# 安装依赖
yarn install
# 本地开发
yarn start
# 构建
docker build --no-cache -t demo/web-oauth-app:latest .
# 命令式运行
docker run \
-p 8080:8080 \
-d \
--name web-oauth-app \
--env MYSQL_USER=root \
--env MYSQL_PASSWORD=123456 \
--env MYSQL_DATABASE=app \
--env MYSQL_HOST=locaclhost \
--env MYSQL_PORT=3306 \
--env OAUTH_GITHUB_CLIENT_ID= \
--env OAUTH_... \
demo/web-oauth-app:latest
# 配置文件式运行
docker run \
-p 8080:8080 \
-d \
--name web-oauth-app \
-v "$(pwd)/config.js":/usr/src/app/src/config/config.js \
-v "$(pwd)/adapter.js":/usr/src/app/src/config/adapter.js \
demo/web-oauth-app:latest
MIT