密钥斗篷未知提供程序错误

Keycloak Unknown Provider error

本文关键字:程序 错误 未知 密钥      更新时间:2023-09-26

我正在使用Keycloak.js与Keycloak进行交互并得到以下错误

Uncaught Error: [$injector:unpr] Unknown provider: AuthProvider <- Auth <- authInterceptor <- $http <- $templateRequest <- $compile

使用以下代码:

module.factory('authInterceptor', ['$q', 'Auth', function($q, Auth) {
  return {
    request: function (config) {
      var deferred = $q.defer();
      if (Auth.authz.token) {
        Auth.authz.updateToken(5).success(function() {
          config.headers = config.headers || {};
          config.headers.Authorization = 'Bearer ' + Auth.authz.token;
          deferred.resolve(config);
        }).error(function() {
          deferred.reject('Failed to refresh token');
        });
      }
      return deferred.promise;
    }
  };
}]);
module.config(['$httpProvider', function($httpProvider) {
  $httpProvider.responseInterceptors.push('errorInterceptor');
  $httpProvider.interceptors.push('authInterceptor');
}]);

发生这种情况有什么原因吗?

我还在我的索引中包括钥匙斗篷.js.html该索引与鲍尔一起插入

我还在 dom 中实例化了以下身份验证工厂:

angular.element(document).ready(function($http) {
  var keycloakAuth = new Keycloak('keycloak.json');
  auth.loggedIn = false;
  keycloakAuth.init().success(function () {
    auth.loggedIn = true;
    auth.authz = keycloakAuth;
    auth.logoutUrl = keycloakAuth.authServerUrl + "/realms/demo/tokens/logout?redirect_uri=http://localhost:3000";
    module.factory('Auth', function () {
      return auth;
    });
  }).error(function () {
    window.location.reload();
  });
});

问题是你在 dom ready 上实例化"Auth",但依赖注入器试图在 dom 就绪之前注入(简化)。

问题是为什么在 dom 准备好了?

下面是两个示例:

http://jsbin.com/lulin/1/edit(使用 dom 就绪定义,不起作用,相同的错误)

http://jsbin.com/wajeho/2/edit(没有 dom 就绪定义,工作)


编辑:

你必须做这样的事情:http://jsbin.com/xusiva/1/edit?html,js,console

我在 domready 之外改进工厂,在 domready 之后在控制器内部我正在使用它。

我有同样的问题,到目前为止,我能说的是Keycloak的示例正在运行角度1.2,而您可能使用的是最新的1.3。
如果您尝试使用角度 1.2 的 Keycloak 角度示例,它会起作用,但是当您尝试使用 1.3 时,会出现该错误。

我一直在阅读有关此内容的信息,这与拦截器有关,它们在角度 1.3 中的声明方式是不同的。此外,responseInterceptors在angular 1.3中完全不推荐使用。