【问题标题】:Ember simple-auth-token won't whitelist API serverEmber simple-auth-token 不会将 API 服务器列入白名单
【发布时间】:2015-04-30 01:14:58
【问题描述】:

我一直在努力找出我错过了什么;我正在使用simple-authsimple-auth-token 库(通过ember-cli 0.2.3)并且似乎无法让我的应用程序设置适当的Athentication: HTTP 标头。根据我的阅读,最常见的疏忽是人们没有在simple-auth ENV 变量上设置crossOriginWhitelist: 属性。但是,即使值为['*'],我似乎也无法让 Ember 将标头与我的 API 请求一起发送。请注意,我正在替换以前的手动(虽然,半生不熟!)身份验证解决方案,所以我知道我的 API 服务器可以工作,并且在正确的凭据下进行身份验证。

当我运行login 操作时,一切正常。如果我在那之后点击了受保护的 Ember 路线,它也可以正常工作。当Ember-data 尝试访问我的 API(http://localhost:3000)时,问题就来了;它返回一个401(因为它没有设置Authorization: 标头)并转换到我网站的索引。

以下是相关的代码部分:

config/environments.js

...

ENV['simple-auth'] = {
    authenticationRoute: 'login',
    authorizer: 'simple-auth-authorizer:token',
    crossOriginWhitelist: ['*']
};
ENV['simple-auth-token'] = {
    identificationField: 'email',
    passwordField: 'password',
    tokenPropertyName: 'token',
    authorizationPrefix: 'Bearer ',
    authorizationHeaderName: 'Authorization'
};
...

routes/login.js

import Ember from 'ember';

export default Ember.Route.extend({
  actions: {
    login: function(){
      var creds = this.controller.getProperties('identification', 'password');
      this.get('session').authenticate('simple-auth-authenticator:jwt', creds)
        .then(function() {
          // +
          }, function() {
          // -
      });
    }
  }
});

routes/application.js

import Ember from 'ember';
import ApplicationRouteMixin from 'simple-auth/mixins/application-route-mixin';

export default Ember.Route.extend(ApplicationRouteMixin);

提前致谢。

【问题讨论】:

    标签: ember.js ember-simple-auth


    【解决方案1】:

    config/environment.js,尝试添加:

    var ENV = {
      ...
      contentSecurityPolicy: {
        'default-src': "'none'",
        'script-src': "'self'",
        'font-src': "'self'",
        'connect-src': "'self' *",
        'img-src': "'self'",
        'style-src': "'self'",
        'media-src': "'self'"
      }
      ...
    };
    

    【讨论】:

    • 没有骰子,很遗憾。可能是我的服务器没有以正确格式的方式发回数据吗?它似乎将它存储在本地存储中就好了——而且,正如人们所期望的那样,密钥被设置为token
    • 好吧,暂时我只是在我的适配器中直接引用了localStorage中的值并在那里设置了标题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多