我想知道 AngularJS 中保护管理页面的最佳方法是什么,即普通用户不应该看到的页面。显然,会有后端身份验证,但由于 AngularJS 应用程序是客户端,理论上人们仍然可以查看路由并直接转到这些 URL 并查看管理页面。
我使用 Express、PassportJS 和 MongoDB (Mongoose) 作为我的后端。自然地,他们将无法与管理页面交互,因为在创建、删除时有服务器端身份验证……但我更愿意在用户没有适当的访问。由于该应用程序完全是客户端 JS,我认为这是不可能的,因为人们可以修改路由等等。最好的方法是什么?感谢您的任何输入!
最佳答案
我会在 routeProvider 中检查。必须为每条需要身份验证的路由完成此操作。您甚至可以编写一个单独的方法并将其粘贴到每个路由以避免重复。
$routeProvider
.when('/profile', {
templateUrl: 'views/profile.html',
controller: 'ProfileCtrl',
resolve: {
validate: function($q, $location) {
// Either you could maintain an array of hashes on client side
// a user do not have access to without login
// Or hit the backend url to check it more securely
var validateAccess = $q.defer();
var isAllowed = ['profile', 'index', 'dashboard'].indexOf($location.hash()) !== -1;
if (!isAllowed) {
$location.path('/login');
}
validateAccess.resolve();
return validateAccess.promise;
}
}
})
.otherwise({
redirectTo: '/'
});
关于angularjs - 防止访问 AngularJS 中的管理页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18943778/