使用angularjs和restangular注入auth标头

时间:2021-03-08 16:55:24

I am creating a RESTFUL webservice that is called from an angularjs app which uses the restangular service. The REST service requires that an auth token is sent with each request. How can I get this sent with each request using restangular?

我正在创建一个RESTFUL Web服务,该服务是从使用restangular服务的angularjs app调用的。 REST服务要求每个请求都发送一个身份验证令牌。如何使用restangular将每个请求发送到此处?

Looking at the documentation I can see there is a setRequestInterceptor method that I can set in the config which looks like I might be able to use:

查看文档,我可以看到有一个setRequestInterceptor方法,我可以在配置中设置,看起来我可以使用:

RestangularProvider.setFullRequestInterceptor(function(element, operation, route, url, headers, params) {
      return {
        element: element,
        params: params,
        headers: _.extend(headers, {Authorization: 'Basic ' +  base64.encode('sometoken')})
      };
    });

I've not been able to get this to actually even get called by putting it in my config. I don't know where RestangularProvider provider needs to be injected from to get htis to work? I'd also want to create an external service which reads the token that has been generated and I'm not sure where to put this. Is there an example of this working anywhere? I've only been able to find scenarios that use $ngresource.

我无法通过将它放入我的配置中来实现甚至调用它。我不知道RestangularProvider提供者需要从哪里注入才能让htis工作?我还想创建一个外部服务,它读取已经生成的令牌,我不知道在哪里放这个。有没有这样的例子在任何地方工作?我只能找到使用$ ngresource的场景。

Any help appreciated.

任何帮助赞赏。

1 个解决方案

#1


2  

This was just an error on my own part. I was trying to chain this rather than include it as part of the config. A good example:

这只是我自己的一个错误。我试图将其链接而不是将其作为配置的一部分包含在内。一个很好的例子:

http://plnkr.co/edit/d6yDka?p=preview

#1


2  

This was just an error on my own part. I was trying to chain this rather than include it as part of the config. A good example:

这只是我自己的一个错误。我试图将其链接而不是将其作为配置的一部分包含在内。一个很好的例子:

http://plnkr.co/edit/d6yDka?p=preview