注射AUTH头与angularjs和restangular [英] Injecting auth headers with angularjs and restangular

查看:181
本文介绍了注射AUTH头与angularjs和restangular的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个RESTful Web服务是从它使用restangular服务的angularjs应用调用。 REST服务要求的身份验证令牌与每个请求发送。我怎样才能得到这个发每个请求使用restangular?

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?

看着文件,我可以看到有我可以在它看起来像我可能可以使用配置设置setRequestInterceptor方法:

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:

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

我已经无法通过把它在我的配置得到这个居然连被调用。我不知道在哪里RestangularProvider提供商需要从注入获得htis工作?我也想创建一个外部服务,读取已生成的标记,我不知道在哪里把这个。是否有这样的任何地方工作的例子吗?我只能够找到使用$ ngresource场景。

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.

任何帮助AP preciated。

Any help appreciated.

推荐答案

这只是我自己的部分错误。我试图链这个,而不是包括它作为配置的一部分。一个很好的例子:

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=$p$ PVIEW

这篇关于注射AUTH头与angularjs和restangular的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆