自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Node.JS, Mongoose和Jade搭建OAuth2服務(wù)器

開發(fā) 前端
Papers是一項(xiàng)論文數(shù)據(jù)庫移動(dòng)應(yīng)用,有iOS和Android版本。寫論文的同學(xué)們會(huì)很需要,關(guān)于它的介紹,請(qǐng)看開發(fā)者的官方博客。http://blog.papersapp.com/oauth-server-in-node-js/

今天我們來看一個(gè)Node.JS的實(shí)際應(yīng)用。這是國外的Paper應(yīng)用開發(fā)者所搭建的OAuth2服務(wù)器,使用的主要技術(shù)包括:

- Node.JS 的Express框架

- Mongoose工具集,Mongodb的一個(gè)流行庫,方便建立模型。

- bcrypt,用于密碼加密

- superagent,用于測(cè)試

Papers是一項(xiàng)論文數(shù)據(jù)庫移動(dòng)應(yīng)用,有iOS和Android版本。寫論文的同學(xué)們會(huì)很需要,關(guān)于它的介紹,請(qǐng)看開發(fā)者的官方博客。http://blog.papersapp.com/oauth-server-in-node-js/

雖然Papers并不是開源的,但是作者已經(jīng)把寫好的node-oauth2-server模塊以及Papers的認(rèn)證過程一起打包放在了GitHub上,我們可以下載研究:

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

在代碼中的Models目錄下,我們可以清楚的看到Model的Schema定義。從這里,我們可以明白OAuth2的需要處理的主要數(shù)據(jù)結(jié)構(gòu),包括access_token, refresh_token, oauth_client.

  1. var OAuthAccessTokensSchema = new Schema({  
  2.   accessToken: { type: String, required: true, unique: true },  
  3.   clientId: String,  
  4.   userId: { type: String, required: true },  
  5.   expires: Date  
  6. });  
  7.  
  8. var OAuthRefreshTokensSchema = new Schema({  
  9.   refreshToken: { type: String, required: true, unique: true },  
  10.   clientId: String,  
  11.   userId: { type: String, required: true },  
  12.   expires: Date  
  13. });  
  14.  
  15. var OAuthClientsSchema = new Schema({  
  16.   clientId: String,  
  17.   clientSecret: String,  
  18.   redirectUri: String  
  19. });  
  20.  
  21. var OAuthUsersSchema = new Schema({  
  22.   email: { type: String, unique: true, required: true },  
  23.   hashed_password: { type: String, required: true },  
  24.   password_reset_token: { type: String, unique: true },  
  25.   reset_token_expires: Date,  
  26.   firstname: String,  
  27.   lastname: String  
  28. }); 

通過運(yùn)行代碼中的seed.js,我們創(chuàng)建了一個(gè)user.

  1. var app = require('./app');  
  2. var models = require('./models');  
  3.  
  4. models.User.create({  
  5.   email: 'alex@example.com',  
  6.   hashed_password: '$2a$10$aZB36UooZpL.fAgbQVN/j.pfZVVvkHxEnj7vfkVSqwBOBZbB/IAAK' 
  7. }, function() {  
  8.   models.OAuthClientsModel.create({  
  9.     clientId: 'papers3',  
  10.     clientSecret: '123',  
  11.     redirectUri: '/oauth/redirect' 
  12.   }, function() {  
  13.     process.exit();  
  14.   });  
  15. }); 

這樣我們就可以開始體驗(yàn)這個(gè)Node.JS的OAuth2服務(wù)器了。先讓Mongo運(yùn)行起來,負(fù)責(zé)后臺(tái)數(shù)據(jù)庫, 比如"mongod -dbpath ./", 之后運(yùn)行"npm start".

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

模擬一個(gè)Oauth2的access token請(qǐng)求,運(yùn)行這份文件(node getToken.js)

  1. var request = require('request');  
  2.  
  3. //client_id  
  4. var t_client_id = 'papers3';  
  5. //client_secret  
  6. var t_client_secret = '123';  
  7. //clientCredentials  以client_id:client_secret形式組合并轉(zhuǎn)換成Base64-encoded  
  8. var clientCredentials = (t_client_id + ':'+t_client_secret).toString('base64');  
  9. //用戶名  
  10. var t_username = 'alex@example.com';  
  11. //密碼  
  12. var t_password = 'test';  
  13.  
  14. console.log(clientCredentials);  
  15.  
  16. //發(fā)送Post請(qǐng)求獲取Token  
  17. request.post({    
  18.   url: 'http://' + clientCredentials + '@localhost:3000/oauth/token',  
  19.   form: {  
  20.     grant_type: 'password',  
  21.     username: t_username,  
  22.     password: t_password,  
  23.     client_id: t_client_id,  
  24.     client_secret: t_client_secret  
  25.   },  
  26. }, function(err, res, body) {  
  27.   console.log(body);  
  28.   //獲得Token  
  29.   var accessToken = JSON.parse(body).access_token;  
  30.  
  31.   request.get({  
  32.     url: 'http://localhost:3000/secret',  
  33.     headers: { Authorization: 'Bearer ' + accessToken }  
  34.   }, function(err, res, body) {  
  35.     console.log(body);  
  36.    });  
  37. });  

成功獲得access token.

  1. oliverluan@localhost:~/Documents/EvWork/node_oauth2_example/oauth2-example$ node getToken.js  
  2. papers3:123  
  3. {  
  4.   "token_type""bearer",  
  5.   "access_token""620bb362f32857d5174802e06065305874953597",  
  6.   "expires_in": 3600,  
  7.   "refresh_token""569be5f4cc1ea943021b3676eaa2a51825c2c257" 
  8. }  
  9. Secret area 

原文鏈接:http://blog.csdn.net/u011581005/article/details/23650917

責(zé)任編輯:林師授 來源: neokidd的博客
相關(guān)推薦

2020-10-12 08:06:28

HTTP 服務(wù)器證書

2019-02-15 10:49:37

Node.jsweb服務(wù)器

2021-11-15 13:58:00

服務(wù)器配置授權(quán)

2021-08-29 23:33:44

OAuth2服務(wù)器Keycloak

2020-03-17 13:24:04

微服務(wù)架構(gòu)數(shù)據(jù)

2022-02-15 07:35:12

服務(wù)器KeycloakOAuth2

2013-05-02 14:13:44

Android開發(fā)OAuth2服務(wù)認(rèn)證

2011-06-17 10:29:04

Nodejavascript

2022-06-05 13:52:32

Node.jsDNS 的原理DNS 服務(wù)器

2021-09-02 10:49:25

Node.jsPHP服務(wù)器開發(fā)

2011-07-26 11:07:08

JavaScript

2022-05-13 15:15:18

服務(wù)器OAuth2控制臺(tái)

2021-02-04 09:18:20

服務(wù)器認(rèn)證自定義

2011-09-08 10:21:50

Node.js

2011-10-19 14:38:46

Node.js

2020-10-29 16:00:03

Node.jsweb前端

2013-11-01 09:34:56

Node.js技術(shù)

2015-03-10 10:59:18

Node.js開發(fā)指南基礎(chǔ)介紹

2022-08-28 16:30:34

Node.jsDocker指令

2022-08-22 07:26:32

Node.js微服務(wù)架構(gòu)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)