我花了很多时间试图弄清楚这一点。
我的后端是带有devise_token_auth的Rails 4.x
要在登录后授权请求,我需要为每个请求发送多个标头,如下所示(已通过curl验证):
curl -X GET -H "Content-Type: application/vnd.api+json" -H "Access-Token: 33YPWz2Kr4eMimYjblDg7w" -H "Client: godv0EDuuc-2qZ6kvrVLzQ" -H "Token-Type: Bearer" -H "Accept: application/vnd.api+json" -H "Uid: [email protected]" -H "Expiry: 1459295877" -H "Provider: Email" "http://localhost:3000/api/v1/forms"
我完全不知道授权api的实际工作方式。我看不到如何在DeviseAuthorizer#authorize方法中设置多个请求标头。
如果有人知道如何执行此操作并可以回答问题,我将立即打开拉取请求以修复此区域中的Ember Simple Auth文档。
设计授权者将函数作为第二个参数传递给#authorize。
http://ember-simple-auth.com/api/classes/BaseAuthorizer.html#method_authorize
authorize(data, block(headerName,headerContent))
争论
数据:对象会话当前保存的数据
block(headerName,headerContent):函数使用授权数据进行调用的回调;将接收标头名称和标头内容作为参数
如果要添加自己的标头,则可以创建从设计授权者扩展的授权者类。然后像这样重写authorize方法:
import Ember from 'ember';
import Devise from 'ember-simple-auth/authorizers/devise';
export default Devise.extend({
authorize(data, header) {
this._super(data, header);
header('X-Custom-Header', "The custom 1 header");
header('X-Other-Custom-Header', "The custom 2 header");
}
});
之所以有效,是因为在数据适配器mixin中,它正在传递此函数:
this.get('session').authorize(authorizer, (headerName, headerValue) => {
xhr.setRequestHeader(headerName, headerValue);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句