我有一个使用yeoman,grunt和bower创建的AngularJS应用程序.
我有一个登录页面,其中包含一个检查身份验证的控制器.如果凭据正确,我将重新路由到主页.
app.js
'use strict';
//Define Routing for app
angular.module('myApp', []).config(['$routeProvider', '$locationProvider',
function($routeProvider,$locationProvider) {
$routeProvider
.when('/login', {
templateUrl: 'login.html',
controller: 'LoginController'
})
.when('/register', {
templateUrl: 'register.html',
controller: 'RegisterController'
})
.when('/forgotPassword', {
templateUrl: 'forgotpassword.html',
controller: 'forgotController'
})
.when('/home', {
templateUrl: 'views/home.html',
controller: 'homeController'
})
.otherwise({
redirectTo: '/login'
});
// $locationProvider.html5Mode(true); //Remove the '#' from URL.
}]);
angular.module('myApp').factory("page", function($rootScope){
var page={};
var user={};
page.setPage=function(title,bodyClass){
$rootScope.pageTitle = title;
$rootScope.bodylayout=bodyClass;
};
page.setUser=function(user){
$rootScope.user=user;
}
return page;
});
Run Code Online (Sandbox Code Playgroud)
LoginControler.js
'use strict';
angular.module('myApp').controller('LoginController', function($scope, $location, $window,page) …Run Code Online (Sandbox Code Playgroud) 我开始使用angularJS开发一个web应用程序,我不确定一切都是正确的安全(客户端和服务器端).安全性基于单个登录页面,如果检查完凭证,我的服务器会发回一个具有自定义时间有效性的唯一令牌.所有其他REST api都可以通过此令牌访问.应用程序(客户端)浏览到我的入口点ex:https://www.example.com/home.html用户插入凭据并接收一个唯一令牌.此唯一令牌使用AES或其他安全技术存储在服务器数据库中,不以明文格式存储.
从现在开始,我的AngluarJS应用程序将使用此令牌对所有暴露的REST Api进行身份验证.
我正在考虑将令牌临时存储在自定义的http cookie中; 基本上,当服务器验证凭据时,它会发回一个新的Cookie Ex.
app-token : AIXOLQRYIlWTXOLQRYI3XOLQXOLQRYIRYIFD0T
Run Code Online (Sandbox Code Playgroud)
该cookie具有安全和HTTP Only标志设置.Http协议直接管理新cookie并存储它.连续请求将使用新参数呈现cookie,而无需管理它并使用javascript存储它; 在每次请求时,服务器使令牌无效并生成一个新令牌并将其发送回客户端 - >使用单个令牌防止重放攻击.
当客户端从任何REST Api 收到HTTP状态401未授权响应时,角度控制器清除所有cookie并将用户重定向到登录页面.
我应该考虑其他方面吗?将令牌存储在新cookie或localStorage中更好吗?有关如何生成唯一强令牌的任何提示?
编辑(改进):
authentication cookies rest angularjs angularjs-authentication
我对Angular很新,而现在我只是试图让我的所有路线都按照我的意愿设置和工作.
设置:
当用户导航到某些页面时(/settings对于此示例),应用程序应检查是否有用户已登录.如果有照常继续.否则,用户应该转到登录页面(/login).
我想要的:
用户成功登录后,应该转到他们最初尝试访问的页面(/settings)
我的问题: 是否有"Angular方式"记住用户试图去的地方?
相关代码:
app.js
.when('/settings', {
templateUrl: '/views/auth/settings.html',
controller: 'SettingsCtrl',
resolve: {
currentUser: function($q, $location, Auth) {
var deferred = $q.defer();
var noUser = function() {
//remember where the user was trying to go
$location.path("/login")
};
Auth.checkLogin(function() {
if (Auth.currentUser()) {
deferred.resolve(Auth.currentUser());
} else {
deferred.reject(noUser());
}
});
return deferred.promise;
}
}
})
Run Code Online (Sandbox Code Playgroud)
login.js
$scope.submit = function() {
if(!$scope.logInForm.$invalid) {
Auth.login($scope.login, $scope.password, $scope.remember_me)
//go to the page the user …Run Code Online (Sandbox Code Playgroud) redirect login angularjs angularjs-routing angularjs-authentication
我希望通过外部服务为我的路由创建一个简单的身份验证检查.
我定义了路由对象的访问要求:
$routeProvider
.when('/', {
templateUrl: 'src/app/views/index.html',
controller: 'indexCtrl',
authenticated: true
})
.when('/login', {
templateUrl: 'src/app/views/login.html',
controller: 'loginCtrl',
anonymous: true
})
.otherwise({
redirectTo: '/'
})
;
Run Code Online (Sandbox Code Playgroud)
然后,我检查我是否在$routeChangeStart活动中获得了许可.
$rootScope.$on('$routeChangeStart', function (event, next) {
if(next.authenticated && !$myService.isLoggedin())
$location.path("/login");
else if(next.anonymous && $myService.isLoggedin())
$location.path("/secured");
});
Run Code Online (Sandbox Code Playgroud)
实际上,它是有效的 -
如果未经过身份验证的用户将他移动到登录页面,如果他已经过身份验证但路由仅供匿名用户使用,则将其移至另一个页面等.
但是 - 这个重定向实际上是在加载控制器和模板之后发生的! 它导致我的控制器对我的REST API做了一些不必要的请求,即使我没有经过身份验证.
如何在处理之前处理路线?
javascript authentication angularjs angularjs-routing angularjs-authentication
我正在尝试创建基本验证,无论用户是否可以访问某些路由.我在这方面取得了进展,但有一点我无法弄清楚.
我正在使用$ locationChangeStart来监控路由更改.场景是:1.如果用户已登录,则允许他访问除auth路由(登录,注册)之外的所有路由.我通过从我的AuthFactory 2调用方法isAuthenticated()来检查这个.如果用户没有登录,那么他只访问登录和注册路由.应该防止任何其他路由,并且在这种情况下应该将用户重定向到登录.
$rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl){
if(AuthFactory.isAuthenticated()){
if(AuthFactory.isAuthRoute(newUrl)){
event.preventDefault();
$location.path('/');
}
} else {
if(!AuthFactory.isAuthRoute(newUrl)){
event.preventDefault();
$location.path('/login');
}
}
});
Run Code Online (Sandbox Code Playgroud)
让我烦恼的是那个带有preventDefault()的人.如果应用程序到达带有preventDefault()的代码,location.path()那么之后就会发生这种情况.
但是,如果我删除event.preventDefault(),location.path()工作.问题是,我需要阻止,以防非记录尝试访问某些非auth页面.
基本上,我希望能够根据请求的路线阻止或重定向.这样做的正确方法是什么?
authentication angularjs url-redirection angularjs-routing angularjs-authentication