1Ma*_*tup 1 rest node.js express backbone.js
如何在我的应用程序中保护我的快速"GET"路由,以便电子邮件和用户数据不会暴露给未经授权的客户端.我想知道,我应该像密码那样散列所有字段吗?
我的GET"/ users"路由发送这样的JSON ..
{
"name": "keven",
"email": "keveng@gmail.com",
"user": "keven",
"password": "EEOnGFritH1631671dc8da7431399f824b3925a49e",
"country": "America",
"date": "April 20th 2013, 10:34:22 pm",
"_id": "5173502e5g52676c1b000001"
}
Run Code Online (Sandbox Code Playgroud)
在我的主干和节点/快递应用程序中,我在我的主干集合中有一个URL,就像这样..
Users = Backbone.Collection.extend({
model: User,
url: '/users',
});
Run Code Online (Sandbox Code Playgroud)
快递路线是这样的:
app.get('/users', function(req, res){
User.find({}, function (err, accounts) {
res.send(accounts);
});
});
Run Code Online (Sandbox Code Playgroud)
谢谢.
虽然这并不理想,但如果每个请求都在发送用户和密码,那么您只需要一些中间件来在node.js应用程序中执行身份验证和授权.
认证功能:
function authenticate(user, password, fn) {
// Trivial, but not recommended
User.findOne({user: user, password: password}, function (err, user) {
if (err) {
return fn(err);
}
if (!user) {
return fn(new Error('no such user'));
}
return fn(null, user);
});
}
Run Code Online (Sandbox Code Playgroud)
授权中间件功能,依赖于身份验证:
function authorize(req, res, next) {
authenticate(req.params.user, req.params.password, function(err, user) {
if (err) {
res.redirect('/login');
}
// Probably other logic her to determine now *what* they can do
return next(null, user);
});
}
Run Code Online (Sandbox Code Playgroud)
现在,由于您使用的是Express,您可以在路由中使用授权中间件来限制访问:
app.get('/users', authorize, function(req, res){
// Can only get here if allowed
User.find({}, function (err, accounts) {
res.send(accounts);
});
});
Run Code Online (Sandbox Code Playgroud)
但请注意,这将针对每个请求搜索用户 - 这是我说它不理想的原因之一.为了正确的系统安全设计,您应该考虑使用http基本身份验证,基于会话的cookie或oauth.
以下是一些供您查看的链接:
归档时间: |
|
查看次数: |
991 次 |
最近记录: |