我在任何地方都找不到很多东西。大多数示例应用程序只是不谈论安全性等。假定将使用基于rest的API调用对用户进行身份验证。应如何构造应用程序以确保身份验证[也可以授权]。指向示例应用程序的指针会很棒。请根据单页应用程序和常规应用程序添加 View 。
[我相信每个观点都应该照顾好它]
最佳答案
几个月前做一个使用基于REST的api的单页应用程序时遇到了相同的问题。搜索答案后,我想到的是使用HTTP的现有401和403错误。我让我的API返回了这些错误。然后使用扩展的错误处理模型来捕获这些异常,以处理这些错误,并通过路由器navigate函数将其路由至我的登录名。
var ErrorHandlerModel = Backbone.Model.extend({
initialize: function(attributes, options) {
options || (options = {});
this.on("error", this.errorHandler);
this.init && this.init(attributes, options);
},
errorHandler: function(model, error) {
if (error.status == 401 || error.status == 403) {
app.history.navigate('login', true);
}
}
});
在事后看来,尽管我认为最好使用全局jquery ajaxError函数来代替。上面的片段基于几个月前的类似question posted here。
我还必须覆盖 Backbone 的默认获取行为,以便我可以使用ogin触发错误,以捕获api的json响应中包含的响应变量。
var Login = Backbone.Model.extend({
urlRoot: '/login',
parse: function(resp,xhr) {
if (resp.response == 'success') {
app.history.navigate('dashboard', true);
}
else {
this.trigger('loginError');
}
return false;
}
});
关于authentication - 什么是最佳管理设计/架构实践的方法,以在ribbon.js应用程序中管理身份验证( session )和授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10920860/