AngularJS访问令牌安全问题

时间:2014-09-05 17:50:29

标签: angularjs oauth thinktecture-ident-server

从授权服务器检索访问令牌后,在AngularJS中存储访问令牌的最佳做法是什么?我已经看到很多使用localStorage服务的建议,但后来我读过其他帖子/博客说永远不会使用localStorage存储令牌,它不安全等。

由于上面的混合信息,我很难用Angular来解决安全问题。

1 个答案:

答案 0 :(得分:1)

我想,

  1. 生成令牌(服务器端的敏感信息)
  2. 使用仅为服务器所知的机器密钥对生成的令牌进行签名和加密。并获取加密令牌。
  3. 然后将在步骤2中获得的加密令牌保存在cookie中。
  4. Cookie失效应该非常少。制作httponly cookie。
  5. 验证cookie时

    1. 验证Cookie
    2. 使用机器密钥解密并验证它是否仅由我们的服务器发送并使用相同的crc。
    3. 如果上面的步骤2是好的,请验证获取的令牌。
    4. Angularjs在每个$ http请求中自动添加标题

      AngularAppFactory.GetApp=function(appName){
          var app = angular.module(appName,  []);
      
          app.factory('httpRequestInterceptor', ['$rootScope', function($rootScope)
          {
              return {
                  request: function($config) {
                   if( $rootScope.user.authToken )
                   {
                    $config.headers['id'] = $rootScope.user.id;
                     $config.headers['auth-token'] = $rootScope.user.authToken;
                   }
      
                   return $config;
              }
          };
          }]);
      
          app.config(function ($httpProvider) {
            $httpProvider.interceptors.push('httpRequestInterceptor');
          });
      
          return app;
      }
      
      
      
      //Whenever you need to get new angular app, you can call this function.
      app = AngularAppFactory.GetApp('appName');
      
相关问题