首页 文章

simple-auth-token JWT授权不设置授权标头

提问于
浏览
1

我正在尝试使用JWT身份验证设置一个简单的Ember.js应用程序与自定义API服务器通信 . 我可以在API服务器上登录并获取JWT令牌,但在后续API服务器调用中没有设置Authorization标头 .

我的登录控制器是:

import Ember from 'ember';
export default Ember.Controller.extend({

    actions: {
        authenticate: function() {
          var credentials = this.getProperties('identification', 'password'),
            authenticator = 'simple-auth-authenticator:jwt';

          this.get('session').authenticate(authenticator, credentials).then(function() {
              // authentication was successful
              console.log('OK');
            }, function(err) {
              // authentication failed
              console.log('FAIL ' + JSON.stringify(err));
            });

        },
        logOut: function() {
            this.get('session').invalidate();
        }
      }
});

我可以成功登录并获取令牌 . 我的登录路线:

import Ember from 'ember';

export default Ember.Route.extend({

    actions: {
         sessionAuthenticationFailed: function(error) {
            console.log('Login error: ' + error.ErrorDesc);
            this.controllerFor('login').set('loginErrorMessage', error.ErrorDesc);
            this.controllerFor('login').set('ErrorMoreInfo', error.MoreInfo);
        },

        sessionAuthenticationSucceeded: function() {
            console.log('Session authenticated: ' + this.get('session').content.secure.token);

            // redirect to last route requested, or to default route
            var attemptedTransition = this.get('session').get('attemptedTransition');
              if (attemptedTransition) {
                attemptedTransition.retry();
                this.get('session').set('attemptedTransition', null);
              } else {
                this.transitionTo('index');
              }
        }
    }
});

...告诉我令牌已正确获取,并正确地将我重定向到我的受保护路线(例如索引) . 从那时起,如果我尝试从API服务器获取任何数据,它根本不会收到任何“授权:承载[令牌]” Headers . 我的环境配置:

ENV['simple-auth'] = {
  authorizer: 'simple-auth-authorizer:token'
};
ENV['simple-auth-token'] = {
  refreshAccessTokens: true,
  timeFactor: 1000,
  refreshLeeway: 300, // Refresh the token 5 minutes (300s) before it expires.
  serverTokenEndpoint: 'https://localhost:8000/login',
  crossOriginWhitelist:[
     'http://localhost:4200',
     'https://localhost:8000'
    ],
  identificationField: 'user',
  passwordField: 'password',
  tokenPropertyName: 'token',
  authorizationPrefix: 'Bearer ',
  authorizationHeaderName: 'Authorization',
  // headers: {},
};

我还尝试通过调用jqXHR.setRequestHeader来覆盖我的登录路由中的授权功能来手动设置标头,但没有成功:

authorize: function(jqXHR, requestOptions) {
        var auth= "Bearer " + this.get('session').content.secure.Token;
        console.log('Add authorization header ' + auth);
        console.log( JSON.stringify(requestOptions));
        jqXHR.setRequestHeader("Authorization", auth);
    }

任何人都可以告诉我缺少什么吗?不应该使用simple-auth-token来自动添加标头吗?谢谢你的帮助,al .

2 回答

  • 0

    我有同样的问题,REST适配器在不同的端口上调用 .

    解决了添加

    ENV['simple-auth'] = {
            crossOriginWhitelist: ['*']
        }
    
  • 1

    Xabi的答案对我有用 . 但我发现它并不直观 .

    “授权请求”符合限制性CORS策略:在CORS问题的情况下不添加授权 .

    docs

    Ember Simple Auth永远不会授权请求来自与加载Ember.js应用程序的请求不同的请求 .

    但是,不需要授权者的请求(在JWT的情况下没有'Authorization' Headers )是允许的并且正常工作 .

相关问题