千家信息网

Node.JS, Mongoose和Jade怎样搭建OAuth2服务器

发表于:2025-11-10 作者:千家信息网编辑
千家信息网最后更新 2025年11月10日,今天就跟大家聊聊有关Node.JS, Mongoose和Jade怎样搭建OAuth2服务器,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。今天我
千家信息网最后更新 2025年11月10日Node.JS, Mongoose和Jade怎样搭建OAuth2服务器

今天就跟大家聊聊有关Node.JS, Mongoose和Jade怎样搭建OAuth2服务器,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

今天我们来看一个Node.JS的实际应用。这是国外的Paper应用开发者所搭建的OAuth3服务器,使用的主要技术包括:

- Node.JS 的Express框架

- Mongoose工具集,Mongodb的一个流行库,方便建立模型。

- bcrypt,用于密码加密

- superagent,用于测试

Papers是一项论文数据库移动应用,有iOS和Android版本。写论文的同学们会很需要,关于它的介绍,请看开发者的官方博客。http://blog.papersapp.com/oauth-server-in-node-js/

虽然Papers并不是开源的,但是作者已经把写好的node-oauth3-server模块以及Papers的认证过程一起打包放在了GitHub上,我们可以下载研究:

https://github.com/mekentosj/oauth3-example

在代码中的Models目录下,我们可以清楚的看到Model的Schema定义。从这里,我们可以明白OAuth3的需要处理的主要数据结构,包括access_token, refresh_token, oauth_client.

var OAuthAccessTokensSchema = new Schema({    accessToken: { type: String, required: true, unique: true },    clientId: String,    userId: { type: String, required: true },    expires: Date  });   var OAuthRefreshTokensSchema = new Schema({    refreshToken: { type: String, required: true, unique: true },    clientId: String,    userId: { type: String, required: true },    expires: Date  });   var OAuthClientsSchema = new Schema({    clientId: String,    clientSecret: String,    redirectUri: String  });   var OAuthUsersSchema = new Schema({    email: { type: String, unique: true, required: true },    hashed_password: { type: String, required: true },    password_reset_token: { type: String, unique: true },    reset_token_expires: Date,    firstname: String,    lastname: String  });

通过运行代码中的seed.js,我们创建了一个user.

var app = require('./app');  var models = require('./models');   models.User.create({    email: 'alex@example.com',    hashed_password: '$2a$10$aZB36UooZpL.fAgbQVN/j.pfZVVvkHxEnj7vfkVSqwBOBZbB/IAAK' }, function() {    models.OAuthClientsModel.create({      clientId: 'papers3',      clientSecret: '123',      redirectUri: '/oauth/redirect'   }, function() {      process.exit();    });  });

这样我们就可以开始体验这个Node.JS的OAuth3服务器了。先让Mongo运行起来,负责后台数据库, 比如"mongod -dbpath ./", 之后运行"npm start".

oliverluan@localhost:~/Documents/EvWork/node_oauth3_example/oauth3-example$ npm start   > oauth3-experiment@0.0.1 start /Users/oliverluan/Documents/EvWork/node_oauth3_example/oauth3-example  > ./node_modules/.bin/nodemon server.js   14 Apr 07:02:43 - [nodemon] v1.0.17  14 Apr 07:02:43 - [nodemon] to restart at any time, enter `rs`  14 Apr 07:02:43 - [nodemon] watching: *.*  14 Apr 07:02:43 - [nodemon] starting `node server.js`  connect.multipart() will be removed in connect 3.0  visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives  connect.limit() will be removed in connect 3.0  Express server listening on port: 3000  POST /oauth/token 200 102ms - 175b  GET /secret 200 2ms - 11b

模拟一个Oauth3的access token请求,运行这份文件(node getToken.js)

var request = require('request');   //client_id  var t_client_id = 'papers3';  //client_secret  var t_client_secret = '123';  //clientCredentials  以client_id:client_secret形式组合并转换成Base64-encoded  var clientCredentials = (t_client_id + ':'+t_client_secret).toString('base64');  //用户名  var t_username = 'alex@example.com';  //密码  var t_password = 'test';   console.log(clientCredentials);   //发送Post请求获取Token  request.post({      url: 'http://' + clientCredentials + '@localhost:3000/oauth/token',    form: {      grant_type: 'password',      username: t_username,      password: t_password,      client_id: t_client_id,      client_secret: t_client_secret    },  }, function(err, res, body) {    console.log(body);    //获得Token    var accessToken = JSON.parse(body).access_token;     request.get({      url: 'http://localhost:3000/secret',      headers: { Authorization: 'Bearer ' + accessToken }    }, function(err, res, body) {      console.log(body);     });  });

成功获得access token.

oliverluan@localhost:~/Documents/EvWork/node_oauth3_example/oauth3-example$ node getToken.js  papers3:123  {    "token_type": "bearer",    "access_token": "620bb362f32857d5174802e06065305874953597",    "expires_in": 3600,    "refresh_token": "569be5f4cc1ea943021b3676eaa2a51825c2c257" }  Secret area

看完上述内容,你们对Node.JS, Mongoose和Jade怎样搭建OAuth2服务器有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。

服务器 服务 运行 内容 数据 应用 代码 密码 开发者 数据库 开发 清楚 成功 作者 写论文 同学 后台 官方 实际 工具 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 网易mc服务器手机和电脑互通吗 软件开发团队建设文档 台州启商网络技术有限公司 软件开发的语言是什么 两个数据库并行写入 软件开发销售人员薪酬报告 服务器安全级别底 数据库表更新视图是不是也会更新 服务器的功能是访问各类共享资源 河南培训网络技术班 企业网络安全需求分析报告 河北网络安全宣传广告 pubg服务器上不去 网络安全宣传村书记发言 多措并举保护青少年网络安全 安卓直播软件开发制作 centos服务器没有网卡 数据库如何加序列 河南刀片服务器 dell服务器系统恢复 新华互联网科技受邀参加国际大赛 数据库中级联删除视频教程 落实网络安全责任实施细则 软件开发和ui设计一样吗 金联创网络技术有限公司 拥有了域名和服务器怎样搭建网站 南威互联网科技 阿坝分销行业直销软件开发 .net数据库开发 扬州网络营销软件开发哪个好
0