9

我正试图围绕构建基于 express.js / node.js 的 REST API 进行思考。我有一些问题...

  1. 如果我现在只关心 Web 应用程序(不一定是电话应用程序),我的 api 是否需要基于令牌/oauth 1 或 2 的安全性

  2. 是否有任何资源可以学习如何从头开始构建它?我已经阅读了谷歌搜索“rest api with oauth2 authentication express.js”的前 3 页,但我仍然没有掌握它。

4

2 回答 2

8

你想在节点中做一个 REST API 很好。它非常适合构建基于 API 的请求。

对于您的问题:

  1. 如果您只是构建一个基本的 API,带有简单的 GET 和 POST 请求,那么您可能想问自己,您正在显示或操作的数据是否需要“安全性”。如果不是,那么很可能,您不需要实施 OAuth。

但是如果你的数据是敏感的,比如私人用户数据,那么你需要在你的 API 上放置某种安全层。此外,使用 OAuth 或其他基于令牌的安全性可以帮助您在整个用户群中构建更好的权限检查。

  1. 您首先需要掌握 OAuth 的概念。一旦您了解了 OAuth 的工作原理,那么用您选择的语言实现它就真的很容易了。这里有一些关于如何更好地理解 OAuth 的好读物

http://www.slideshare.net/MindfireSolutions/oauth-and-rest?qid=09a7d224-78bb-4b47-8957-3f0a0ce809a4&v=qf1&b=&from_search=3

有关 OAuth 的更多详细信息:https ://www.rfc-editor.org/rfc/rfc6749

同样,一旦您了解了 OAuth 的工作流程,您就可以轻松实现它。:P

于 2014-09-01T18:49:14.083 回答
2
  1. It does not depend if you use your REST server for web applications or for any other clients. If the service available in internet you should consider any client application as an "enemy". I mean you should not rely on any "trusted" client app, you should always perform authentication, if the client gets secured resource. Is the resource secured, it depend on your app. I prefer to use oauth2 in both cases. If resource is not secured, I use Client Credentials (https://www.rfc-editor.org/rfc/rfc6749#section-1.3.4), if it's secured I use Access Token (https://www.rfc-editor.org/rfc/rfc6749#section-1.4). It allows you to keep in the same tech, and easily change the things in the future, if it's needed. Based on my personal experience, I created module oauthifizer (https://github.com/vedi/oauthifizer)。它实际上是对 passport.js 的包装,这使得它在那些特定情况下更加友好。

  2. 你可以看看这篇文章:http ://aleksandrov.ws/2013/09/12/restful-api-with-nodejs-plus-mongodb/ 。同样,您可以考虑尝试 restifizer ( https://github.com/vedi/restifizer ) - 另一个模块,它允许您更快地创建 RESTful 服务。还有一个简短的例子:https ://github.com/vedi/restifizer-example

希望它会有所帮助。

于 2014-09-03T04:57:01.080 回答