当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > AngularJS 应用身份认证的技巧总结

AngularJS 应用身份认证的技巧总结

2018年05月08日  | 移动技术网IT编程  | 我要评论

在web中很多时候都能应用到身份认证,本文介绍了angularjs 应用身份认证的技巧,废话不多说了一起往下看吧。

身份认证

最普遍的身份认证方式就是用用户名(或 email)和密码做登陆操作。这就意味要实现一个登陆的表单,以便用户能够用他们个人信息登陆。这个表单看起来是这样的:

<form name="loginform" ng-controller="logincontroller"
   ng-submit="login(credentials)" novalidate>
 <label for="username">username:</label>
 <input type="text" id="username"
     ng-model="credentials.username">
 <label for="password">password:</label>
 <input type="password" id="password"
     ng-model="credentials.password">
 <button type="submit">login</button>
</form>

既然这个是 angular-powered 的表单,我们使用 ngsubmit 指令去触发上传表单时的函数。注意一点的是,我们把个人信息传入到上传表单的函数,而不是直接使用 $scope.credentials 这个对象。这样使得函数更容易进行 unit-test 和降低这个函数与当前 controller 作用域的耦合。这个 controller 看起来是这样的:

.controller('logincontroller', function ($scope, $rootscope, auth_events, authservice) {
 $scope.credentials = {
  username: '',
  password: ''
 };
 $scope.login = function (credentials) {
  authservice.login(credentials).then(function (user) {
   $rootscope.$broadcast(auth_events.loginsuccess);
   $scope.setcurrentuser(user);
  }, function () {
   $rootscope.$broadcast(auth_events.loginfailed);
  });
 };javascript:void(0);
})

我们注意到这里是缺少实际的逻辑的。这个 controller 被做成这样,目的是使身份认证的逻辑跟表单解耦。把逻辑尽可能的从我们的 controller 里面抽离出来,把他们都放到 services 里面,这是个很好的想法。angularjs 的 controller 应该只管理 $scope 里面的对象(用 watching 或者 手动操作)而不是承担过多过分重的东西。

通知 session 的变化

身份认证会影响整个应用的状态。基于这个原因我更推荐使用事件(用 $broadcast)去通知 user session 的改变。把所有可能用到的事件代码定义在一个中间地带是个不错的选择。我喜欢用 constants 去做这个事情:

.constant('auth_events', {
 loginsuccess: 'auth-login-success',
 loginfailed: 'auth-login-failed',
 logoutsuccess: 'auth-logout-success',
 sessiontimeout: 'auth-session-timeout',
 notauthenticated: 'auth-not-authenticated',
 notauthorized: 'auth-not-authorized'
})

constants 有个很好的特性就是他们能随便注入到别的地方,就像 services 那样。这样使得 constants 很容易被我们的 unit-test 调用。constants 也允许你很容易地在随后对他们重命名而不需要改一大串文件。同样的戏法运用到了 user roles:

.constant('user_roles', {
 all: '*',
 admin: 'admin',
 editor: 'editor',
 guest: 'guest'
})

如果你想给予 editors 和 administrators 同样的权限,你只需要简单地把 ‘editor' 改成 ‘admin'。

the authservice

与身份认证和授权(访问控制)相关的逻辑最好被放到同一个 service:

.factory('authservice', function ($http, session) {
 var authservice = {};

 authservice.login = function (credentials) {
  return $http
   .post('/login', credentials)
   .then(function (res) {
    session.create(res.data.id, res.data.user.id,
            res.data.user.role);
    return res.data.user;
   });
 };

 authservice.isauthenticated = function () {
  return !!session.userid;
 };

 authservice.isauthorized = function (authorizedroles) {
  if (!angular.isarray(authorizedroles)) {
   authorizedroles = [authorizedroles];
  }
  return (authservice.isauthenticated() &&
   authorizedroles.indexof(session.userrole) !== -1);
 };
 return authservice;
})

为了进一步远离身份认证的担忧,我使用另一个 service(一个单例对象,using the service style)去保存用户的 session 信息。session 的信息细节是依赖于后端的实现,但是我还是给出一个较普遍的例子吧:

.service('session', function () {
 this.create = function (sessionid, userid, userrole) {
  this.id = sessionid;
  this.userid = userid;
  this.userrole = userrole;
 };
 this.destroy = function () {
  this.id = null;
  this.userid = null;
  this.userrole = null;
 };
 return this;
})

一旦用户登录了,他的信息应该会被展示在某些地方(比如右上角用户头像什么的)。为了实现这个,用户对象必须要被 $scope 对象引用,更好的是一个可以被全局调用的地方。虽然 $rootscope 是显然易见的第一个选择,但是我尝试克制自己,不过多地使用 $rootscope(实际上我只在全局事件广播使用 $rootscope)。用我所喜欢的方式去做这个事情,就是在应用的根节点,或者在别的至少高于 dom 树的地方,定义一个 controller 。 标签是个很好的选择:

<body ng-controller="applicationcontroller">
 ...
</body>

applicationcontroller 是应用的全局逻辑的容器和一个用于运行 angular 的 run 方法的选择。因此它要处于 $scope 树的根,所有其他的 scope 会继承它(除了隔离 scope)。这是个很好的地方去定义 currentuser 对象:

.controller('applicationcontroller', function ($scope,
                        user_roles,
                        authservice) {
 $scope.currentuser = null;
 $scope.userroles = user_roles;
 $scope.isauthorized = authservice.isauthorized;

 $scope.setcurrentuser = function (user) {
  $scope.currentuser = user;
 };
})

我们实际上不分配 currentuser 对象,我们仅仅初始化作用域上的属性以便 currentuser 能在后面被访问到。不幸的是,我们不能简单地在子作用域分配一个新的值给 currentuser 因为那样会造成 shadow property。这是用以值传递原始类型(strings, numbers, booleans,undefined and null)代替以引用传递原始类型的结果。为了防止 shadow property,我们要使用 setter 函数。如果想了解更多 angular 作用域和原形继承,请阅读 understanding scopes。

访问控制

身份认证,也就是访问控制,其实在 angularjs 并不存在。因为我们是客户端应用,所有源码都在用户手上。没有办法阻止用户篡改代码以获得认证后的界面。我们能做的只是显示控制。如果你需要真正的身份认证,你需要在服务器端做这个事情,但是这个超出了本文范畴。

限制元素的显示

angularjs 拥有基于作用域或者表达式来控制显示或者隐藏元素的指令: ngshow, nghide, ngif 和 ngswitch。前两个会使用一个 <style> 属性去隐藏元素,但是后两个会从 dom 移除元素。

第一种方式,也就是隐藏元素,最好用于表达式频繁改变并且没有包含过多的模板逻辑和作用域引用的元素上。原因是在隐藏的元素里,这些元素的模板逻辑仍然会在每个 digest 循环里重新计算,使得应用性能下降。第二种方式,移除元素,也会移除所有在这个元素上的 handler 和作用域绑定。改变 dom 对于浏览器来说是很大工作量的(在某些场景,和 ngshow/nghide 对比),但是在很多时候这种代价是值得的。因为用户访问信息不会经常改变,使用 ngif 或 ngshow 是最好的选择:

<div ng-if="currentuser">welcome, {{ currentuser.name }}</div>
<div ng-if="isauthorized(userroles.admin)">you're admin.</div>
<div ng-switch on="currentuser.role">
 <div ng-switch-when="userroles.admin">you're admin.</div>
 <div ng-switch-when="userroles.editor">you're editor.</div>
 <div ng-switch-default>you're something else.</div>
</div>

限制路由访问

很多时候你会想让整个网页都不能被访问,而不是仅仅隐藏一个元素。如果可以再路由(在ui router 里,路由也叫状态)使用一种自定义的数据结构,我们就可以明确哪些用户角色可以被允许访问哪些内容。下面这个例子使用 ui router 的风格,但是这些同样适用于 ngroute。

.config(function ($stateprovider, user_roles) {
 $stateprovider.state('dashboard', {
  url: '/dashboard',
  templateurl: 'dashboard/',
  data: {
   authorizedroles: [user_roles.admin, user_roles.editor]
  }
 });
})

下一步,我们需要检查每次路由变化(就是用户跳转到其他页面的时候)。这需要监听 $routechangstart(ngroute 里的)或者 $statechangestart(ui router 里的)事件:

.run(function ($rootscope, auth_events, authservice) {
 $rootscope.$on('$statechangestart', function (event, next) {
  var authorizedroles = next.data.authorizedroles;
  if (!authservice.isauthorized(authorizedroles)) {
   event.preventdefault();
   if (authservice.isauthenticated()) {
    // user is not allowed
    $rootscope.$broadcast(auth_events.notauthorized);
   } else {
    // user is not logged in
    $rootscope.$broadcast(auth_events.notauthenticated);
   }
  }
 });
})

session 时效

身份认证多半是服务器端的事情。无论你用什么实现方式,你的后端会对用户信息做真正的验证和处理诸如 session 时效和访问控制的处理。这意味着你的 api 会有时返回一些认证错误。标准的错误码就是 http 状态吗。普遍使用这些错误码:

  • 401 unauthorized — the user is not logged in
  • 403 forbidden — the user is logged in but isn't allowed access
  • 419 authentication timeout (non standard) — session has expired
  • 440 login timeout (microsoft only) — session has expired

后两种不是标准内容,但是可能广泛应用。最好的官方的判断 session 过期的错误码是 401。无论怎样,你的登陆对话框都应该在 api 返回 401, 419, 440 或者 403 的时候马上显示出来。总的来说,我们想广播和基于这些 http 返回码的时间,为此我们在 $httpprovider 增加一个拦截器:

.config(function ($httpprovider) {
 $httpprovider.interceptors.push([
  '$injector',
  function ($injector) {
   return $injector.get('authinterceptor');
  }
 ]);
})
.factory('authinterceptor', function ($rootscope, $q,
                   auth_events) {
 return {
  responseerror: function (response) { 
   $rootscope.$broadcast({
    401: auth_events.notauthenticated,
    403: auth_events.notauthorized,
    419: auth_events.sessiontimeout,
    440: auth_events.sessiontimeout
   }[response.status], response);
   return $q.reject(response);
  }
 };
})

这只是一个认证拦截器的简单实现。有个很棒的项目在 github ,它做了相同的事情,并且使用了 httpbuffer 服务。当返回 http 错误码时,它会阻止用户进一步的请求,直到用户再次登录,然后继续这个请求。

登录对话框指令

当一个 session 过期了,我们需要用户重新进入他的账号。为了防止他丢失他当前的工作,最好的方法就是弹出登录登录对话框,而不是跳转到登录页面。这个对话框需要监听 notauthenticated 和 sessiontimeout 事件,所以当其中一个事件被触发了,对话框就要打开:

.directive('logindialog', function (auth_events) {
 return {
  restrict: 'a',
  template: '<div ng-if="visible"
          ng-include="\'login-form.html\'">',
  link: function (scope) {
   var showdialog = function () {
    scope.visible = true;
   };

   scope.visible = false;
   scope.$on(auth_events.notauthenticated, showdialog);
   scope.$on(auth_events.sessiontimeout, showdialog)
  }
 };
})

只要你喜欢,这个对话框可以随便扩展。主要的思想是重用已存在的登陆表单模板和 logincontroller。你需要在每个页面写上如下的代码:

<div login-dialog ng-if="!isloginpage"></div>

注意 isloginpage 检查。一个失败了的登陆会触发 notauthenticated 时间,但我们不想在登陆页面显示这个对话框,因为这很多余和奇怪。这就是为什么我们不把登陆对话框也放在登陆页面的原因。所以在 applicationcontroller 里定义一个 $scope.isloginpage 是合理的。

保存用户状态

在用户刷新他们的页面,依旧保存已登陆的用户信息是单页应用认证里面狡猾的一个环节。因为所有状态都存在客户端,刷新会清空用户信息。为了修复这个问题,我通常实现一个会返回已登陆的当前用户的数据的 api (比如 /profile),这个 api 会在 angularjs 应用启动(比如在 “run” 函数)。然后用户数据会被保存在 session 服务或者 $rootscope,就像用户已经登陆后的状态。或者,你可以把用户数据直接嵌入到 ,这样就不用额外的请求了。第三种方式就是把用户数据存在 cookie 或者 localstorage,但这会使得登出或者清空用户数据变得困难一点。

最后……

鄙人才疏学浅,一点点经验,这是一篇翻译的文章,如有谬误,欢迎指正。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网