这是客户端安全性,可以在常规Angular版本中实现。我已经尝试并测试了这一点。(请在这里找到我的文章:https : //www.intellewings.com/post/authorizationonangularroutes)除了客户端路由安全性之外,您还需要在服务器端保护访问权限。客户端安全性有助于避免服务器往返。但是,如果有人欺骗了浏览器,则服务器服务器端的安全性应该能够拒绝未经授权的访问。
希望这可以帮助!
步骤1:在应用程序模块中定义全局变量
-定义应用程序的角色
var roles = {
superUser: 0,
admin: 1,
user: 2
};
-为应用程序定义未经授权访问的路由
var routeForUnauthorizedAccess = '/SomeAngularRouteForUnauthorizedAccess';
步骤2:定义授权服务
appModule.factory('authorizationService', function ($resource, $q, $rootScope, $location) {
return {
permissionModel: { permission: {}, isPermissionLoaded: false },
permissionCheck: function (roleCollection) {
var deferred = $q.defer();
var parentPointer = this;
if (this.permissionModel.isPermissionLoaded) {
this.getPermission(this.permissionModel, roleCollection, deferred);
} else {
$resource('/api/permissionService').get().$promise.then(function (response) {
parentPointer.permissionModel.permission = response;
parentPointer.permissionModel.isPermissionLoaded = true;
parentPointer.getPermission(parentPointer.permissionModel, roleCollection, deferred);
}
);
}
return deferred.promise;
},
getPermission: function (permissionModel, roleCollection, deferred) {
var ifPermissionPassed = false;
angular.forEach(roleCollection, function (role) {
switch (role) {
case roles.superUser:
if (permissionModel.permission.isSuperUser) {
ifPermissionPassed = true;
}
break;
case roles.admin:
if (permissionModel.permission.isAdministrator) {
ifPermissionPassed = true;
}
break;
case roles.user:
if (permissionModel.permission.isUser) {
ifPermissionPassed = true;
}
break;
default:
ifPermissionPassed = false;
}
});
if (!ifPermissionPassed) {
$location.path(routeForUnauthorizedAccess);
$rootScope.$on('$locationChangeSuccess', function (next, current) {
deferred.resolve();
});
} else {
deferred.resolve();
}
}
};
});
步骤3:在路由中使用安全性:让我们使用到目前为止已完成的所有词汇来保护路由
var appModule = angular.module("appModule", ['ngRoute', 'ngResource'])
.config(function ($routeProvider, $locationProvider) {
$routeProvider
.when('/superUserSpecificRoute', {
templateUrl: '/templates/superUser.html',
caseInsensitiveMatch: true,
controller: 'superUserController',
resolve: {
permission: function(authorizationService, $route) {
return authorizationService.permissionCheck([roles.superUser]);
},
}
})
.when('/userSpecificRoute', {
templateUrl: '/templates/user.html',
caseInsensitiveMatch: true,
controller: 'userController',
resolve: {
permission: function (authorizationService, $route) {
return authorizationService.permissionCheck([roles.user]);
},
}
})
.when('/adminSpecificRoute', {
templateUrl: '/templates/admin.html',
caseInsensitiveMatch: true,
controller: 'adminController',
resolve: {
permission: function(authorizationService, $route) {
return authorizationService.permissionCheck([roles.admin]);
},
}
})
.when('/adminSuperUserSpecificRoute', {
templateUrl: '/templates/adminSuperUser.html',
caseInsensitiveMatch: true,
controller: 'adminSuperUserController',
resolve: {
permission: function(authorizationService, $route) {
return authorizationService.permissionCheck([roles.admin,roles.superUser]);
},
}
})
});