วิธีแก้ปัญหาคือการถ่ายทอดเหตุการณ์ที่ 'ไม่ได้รับอนุญาต' และจับมันในขอบเขตหลักเพื่อเปลี่ยนสถานที่ใหม่ ฉันคิดว่ามันไม่ใช่ทางออกที่ดีที่สุด แต่มันใช้ได้ผลสำหรับฉัน:
myApp.run(['$rootScope', 'LoginService',
function ($rootScope, LoginService) {
$rootScope.$on('$routeChangeStart', function (event, next, current) {
var authorizedRoles = next.data ? next.data.authorizedRoles : null;
if (LoginService.isAuthenticated()) {
if (!LoginService.isAuthorized(authorizedRoles)) {
$rootScope.$broadcast('notAuthorized');
}
}
});
}
]);
และในตัวควบคุมหลักของฉัน:
$scope.$on('notAuthorized', function(){
$location.path('/forbidden');
});
หมายเหตุ: มีการอภิปรายเกี่ยวกับปัญหานี้ในไซต์เชิงมุมซึ่งยังไม่ได้รับการแก้ไข:
https://github.com/angular/angular.js/pull/4192
แก้ไข:
หากต้องการตอบความคิดเห็นนี่คือข้อมูลเพิ่มเติมเกี่ยวกับการใช้งาน LoginService ประกอบด้วย 3 ฟังก์ชั่น:
- login () (ชื่อทำให้เข้าใจผิด) ส่งคำขอไปยังเซิร์ฟเวอร์เพื่อรับข้อมูลเกี่ยวกับผู้ใช้ที่เข้าสู่ระบบ (ก่อนหน้านี้) มีหน้าเข้าสู่ระบบอีกหน้าซึ่งเพิ่งเติมข้อมูลสถานะผู้ใช้ปัจจุบันในเซิร์ฟเวอร์ (โดยใช้กรอบงาน SpringSecurity) บริการบนเว็บของฉันไม่ได้เป็นคนไร้สัญชาติ แต่ฉันต้องการให้เฟรมเวิร์กที่มีชื่อเสียงนั้นจัดการความปลอดภัยของฉัน
- isAuthenticated () เพียงค้นหาว่าเซสชันไคลเอ็นต์เต็มไปด้วยข้อมูลหรือไม่ซึ่งหมายความว่าได้รับการพิสูจน์ตัวตนก่อนหน้านี้
- isAuthorized () จัดการสิทธิ์การเข้าถึง (อยู่นอกขอบเขตสำหรับหัวข้อนี้)
(*) เซสชันของฉันจะถูกเติมเมื่อเส้นทางเปลี่ยนไป ฉันได้แทนที่แล้วเมื่อ () วิธีการเติมเซสชันเมื่อว่างเปล่า
นี่คือรหัส:
services.factory('LoginService', ['$http', 'Session', '$q',
function($http, Session, $q){
return {
login: function () {
var defer = $q.defer();
$http({method: 'GET', url: restBaseUrl + '/currentUser'})
.success(function (data) {
defer.resolve(data);
});
return defer.promise;
},
isAuthenticated: function () {
return !!Session.userLogin;
},
isAuthorized: function (authorizedRoles) {
if (!angular.isArray(authorizedRoles)) {
authorizedRoles = [authorizedRoles];
}
return (this.isAuthenticated() && authorizedRoles.indexOf(Session.userRole) !== -1);
}
};
}]);
myApp.service('Session', ['$rootScope',
this.create = function (userId,userLogin, userRole, userMail, userName, userLastName, userLanguage) {
this.userId = userId;
this.userLogin = userLogin;
this.userRole = userRole;
this.userMail = userMail;
this.userName = userName;
this.userLastName = userLastName;
this.userLanguage = userLanguage;
};
this.destroy = function () {
this.userId = null;
this.userLogin = null;
this.userRole = null;
this.userMail = null;
this.userName = null;
this.userLastName = null;
this.userLanguage = null;
sessionStorage.clear();
};
return this;
}]);
myApp.config(['$routeProvider', 'USER_ROLES', function ($routeProvider, USER_ROLES) {
$routeProvider.accessWhen = function (path, route) {
if (route.resolve == null) {
route.resolve = {
user: ['LoginService','Session',function (LoginService, Session) {
if (!LoginService.isAuthenticated())
return LoginService.login().then(function (data) {
Session.create(data.id, data.login, data.role, data.email, data.firstName, data.lastName, data.language);
return data;
});
}]
}
} else {
for (key in route.resolve) {
var func = route.resolve[key];
route.resolve[key] = ['LoginService','Session','$injector',function (LoginService, Session, $injector) {
if (!LoginService.isAuthenticated())
return LoginService.login().then(function (data) {
Session.create(data.id, data.login, data.role, data.email, data.firstName, data.lastName, data.language);
return func(Session, $injector);
});
else
return func(Session, $injector);
}];
}
}
return $routeProvider.when(path, route);
};
$routeProvider.
accessWhen('/home', {
templateUrl: 'partials/dashboard.html',
controller: 'DashboardCtrl',
data: {authorizedRoles: [USER_ROLES.superAdmin, USER_ROLES.admin, USER_ROLES.system, USER_ROLES.user]},
resolve: {nextEvents: function (Session, $injector) {
$http = $injector.get('$http');
return $http.get(actionBaseUrl + '/devices/nextEvents', {
params: {
userId: Session.userId, batch: {rows: 5, page: 1}
},
isArray: true}).then(function success(response) {
return response.data;
});
}
}
})
...
.otherwise({
redirectTo: '/home'
});
}]);