本文介绍了AngularJS拦截决不渔获401错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的角v1.2.20。我已经看到了有关如何使用一个拦截器,将其推入 $ httpProvider.interceptors 阵列来处理来自服务器的401错误,很多文章和帖子。

I'm using Angular v1.2.20 with the Ionic Framework. I have seen many articles and posts about how to handle 401 errors from the server by using an interceptor and pushing it into the $httpProvider.interceptors array.

由于某些原因,401的反应永远不会在我的拦截器 repsonseError 函数捕获。我期待从来没有拒绝承诺拒绝和我的应用程序似乎只是挂起(加载:隐藏从下不会被调用)。我可以从401绝对是从服务器返回我的网络调试代理看看。

For some reason, 401 responses never get caught by the repsonseError function in my interceptor. The promise I am expecting to reject never rejects and my app just seems to hang (loading:hide from below never gets called). I can see from my web debugging proxy that a 401 is definitely returned from the server.

我的拦截器:

.factory('interceptorService', function($rootScope, $q, Session, AuthorizationHeader) {
  var interceptorService = {
    request: function(config) {
      $rootScope.$broadcast('loading:show');
      config.headers['Authentication-Key'] = Session.key();
      config.headers['Authentication-Timestamp'] = (new Date().toJSON());
      config.headers['Authentication-Account'] = Session.accountName();
      if(typeof config.headers['Authorization'] === 'undefined') {
        config.headers['Authorization'] = 'Custom-HMAC-SHA512 ' + AuthorizationHeader.buildHashed(config, Session.key());
      }
      return config || $q.when(config);
    },

    response: function(response) {
      if(response.status === 401) {
        console.log('401');
      }
      $rootScope.$broadcast('loading:hide');
      return response || $q.when(response);
    },

    requestError: function(config) {
      $rootScope.$broadcast('loading:hide');
      return $q.reject(config);
    },

    responseError: function(response) {
      console.log('response error');
      $rootScope.$broadcast('loading:hide');
      return $q.reject(response);
    }
  };

  return interceptorService;
})

我可以证实,拦截器的工作,当我得到一个403从服务器返回的,我可以在我的控制台日志中看到响应错误。另请注意,这种情况下使用的HTTP动词无关。它的发生与HEAD电话和DELETE电话。

I can confirm that the interceptor is working as, when I get a 403 back from the server, I can see 'response error' in my console log. Please also note that this happens regardless of HTTP verb used. It's happening with HEAD calls and DELETE calls.

任何帮助非常多AP preciated。

Any help is very much appreciated.

看来,作为参考的这个问题是由科尔多瓦/ PhoneGap的不能够处理 WWW验证的401响应头引起的在这个博客帖子。

It appears that this problem is caused by cordova/phonegap not being able to handle the www-authenticate header in the 401 response as referenced in this blog post.

如果任何人有关于如何最好地处理任何想法,我洗耳恭听。

If anyone has any ideas on how best to handle that, I'm all ears.

推荐答案

单从响应删除WWW-Authenticate头它会返回到您的应用程序之前。这就是固定它适合我。

Just remove the WWW-Authenticate header from the response before it gets back to your app. That's what fixed it for me.

handle 401在Windows Phone与PhoneGap的未经授权的误差

编辑:这是我的工作中使用我们的F5(负载均衡)做,因为这就是我们的基本身份验证时,会出现执行

The implementation that was used at my work was done at our F5 (load balancer) since that's where our Basic Authentication occurs.

这里的东西我刮起了,还没有经过测试。你可能把这个中的Global.asax.cs文件中Application_EndRequest方式:

Here's something I whipped up, that has NOT been tested. You could possibly place this in the Application_EndRequest method in Global.asax.cs file:

    if (context.Response.Headers.Contains("WWW-Authenticate"))
        context.Response.Headers.Remove("WWW-Authenticate");

这篇关于AngularJS拦截决不渔获401错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 01:08