发现循环依赖:Http Response Interceptor 401

时间:2015-10-08 15:25:29

标签: angularjs angular-http-interceptors

基本思想是使用Http Response Interceptor重定向我的网页,如果它给出401状态。但我不知道我是否正确地做到了这一点:我认为它是这样的,但我似乎比看起来更难。目前我发现了循环依赖。

我是否需要在其他地方推动拦截器?拦截器如何知道我是否收到401请求。是否也可以定义哪些401需要拦截以及哪些被忽略

   (function () {
        'use strict';

        angular
            .module('app', ['ngRoute','ngCookies','ngMessages'])
            .config(routeConfig);

        routeConfig.$inject = ['$routeProvider','$httpProvider'];


        function routeConfig($routeProvider,$httpProvider) {
            $routeProvider

                .when('/', {

                    templateUrl: 'login.html',
                    controller : 'LoginController'
                })
                .when('/register', {

                    templateUrl: 'register.html',
                    controller : 'RegisterController'
                })
                .when('/main', {
                  //This gives an 401 status when the user has not been logged in
                    templateUrl: 'home.html',
                    controller : 'HomeController'

                })

                .otherwise('/');


            // We need to add this for csrf protection
            $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

            //this gives an Circular dependency error
            $httpProvider.interceptors.push('HttpResponseInterceptor');

        }

    })();

这是我的服务:

(function () {
    'use strict';

    angular
        .module('app')
        .factory('HttpResponseInterceptor', HttpResponseInterceptor);

    HttpResponseInterceptor.$inject = ['$rootScope','$http','$q','$location'];
    function HttpResponseInterceptor($rootScope,$http,$q,$location) {

        return {
            response: function(response){
                if (response.status === 401) {

                }
                return response || $q.when(response);
            },
            responseError: function(rejection) {
                if (rejection.status === 401) {

                    $location.path('/login');
                }
                return $q.reject(rejection);
            }
        };

    }

})();

UPDATE2

正如评论中提到的那样,我注入了很多东西。所以这是一个问题已修复,但现在当我进入登录页面时,它会在加载页面时发出请求(localhost:8080 / user),导致无限循环重定向到登录页面并导致浏览器崩溃

那么有什么方法我可以对拦截器说哪个url需要被重定向而哪些不需要

2 个答案:

答案 0 :(得分:2)

这是第二个问题的答案......

您查询拒绝对象并为您的IF语句添加一些其他条件...

您可以使用...

转储拒绝对象

的console.log(JSON.stringify(拒绝));

然后为你的IF添加条件...

if (rejection.status === 401 && rejection.config.url !== "/url/you/do/not/want/to/change/state/on") {

    // do state.go or something else here

 }

答案 1 :(得分:1)

使用$ injector帮助注入服务吗?

(function () {

  'use strict';

  angular
    .module('admin')
    .factory('HttpInterceptor', httpInterceptor);

  httpInterceptor.$inject = [
    '$q',         // return promises
    '$injector'   // login service injection
  ];

  function httpInterceptor(q, injector) {
return {
  responseError: function (rejection) {

    var url = rejection.config ? rejection.config.url : undefined;
    var state = injector.get("$state");

    if (rejection.status === 401) {
      if (!(url === "/api/logout" || state.current.name === "login" && url === "/api/authenticated")) {
        var loginService = injector.get('LoginService');
        loginService.logout();
      }
    }

    if (rejection.status === 403) {
      state.go("home");
    }
    return q.reject(rejection);
  }
};
  }
}());