我尝试使用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服务器获取任何数据,它就不会收到任何"授权:承载[令牌]"总而言之。 我的环境配置:
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);
}
有人能说出我失踪了吗?简单的身份验证令牌不应该自动添加标题吗? 谢谢你的帮助, 人
答案 0 :(得分:1)
我遇到了同样的问题,REST适配器在不同的端口上进行调用。
解决了添加
ENV['simple-auth'] = {
crossOriginWhitelist: ['*']
}