【发布时间】:2026-02-09 08:55:01
【问题描述】:
您好,我正在尝试使用 ember 简单身份验证编写自己的授权器。然而 Ember-cli 并没有看到它,它仍然在说 No authorizer was configured for Ember Simple Auth - specify one if backend requests need to be authorized.
我正在使用 ember-cli 0.1.1
这是我的初始化程序:
var CustomAuthorizer = SimpleAuth.Authorizers.Base.extend({
authorize: function(jqXHR, requestOptions){
console.log('authCCC', jqXHR, requestOptions);
}
});
export default {
name: 'authorization',
before: 'simple-auth',
initialize: function(container, application) {
container.register('authorizer:custom', CustomAuthorizer);
}
};
然后根据文档,我需要在我的config/environment.js 中执行此操作
ENV['simple-auth'] = {
authorizer: 'authorizer:custom'
};
不知道这里出了什么问题。从作者那里我得到的只是一个没有帮助的文档链接:/
提前致谢。
编辑
这是我的完整环境.js
module.exports = function(environment) {
var ENV = {
modulePrefix: 'app-client',
environment: environment,
baseURL: '/',
locationType: 'auto',
EmberENV: {
FEATURES: {
// Here you can enable experimental features on an ember canary build
// e.g. 'with-controller': true
}
},
APP: {
// Here you can pass flags/options to your application instance
// when it is created
},
contentSecurityPolicy: {
'font-src': "'self' https://fonts.gstatic.com"
}
};
ENV['simple-auth'] = {
authorizer: 'authorizer:custom'
};
if (environment === 'development') {
ENV.APP.LOG_RESOLVER = true;
ENV.APP.LOG_ACTIVE_GENERATION = true;
ENV.APP.LOG_VIEW_LOOKUPS = true;
ENV.APP.SERVER_URL = 'http://localhost:3000';
// ENV.APP.SERVER_URL = 'http://0.0.0.0:3000';
}
if (environment === 'test') {
// Testem prefers this...
ENV.baseURL = '/';
ENV.locationType = 'auto';
// keep test console output quieter
ENV.APP.LOG_ACTIVE_GENERATION = false;
ENV.APP.LOG_VIEW_LOOKUPS = false;
ENV.APP.rootElement = '#ember-testing';
}
if (environment === 'staging') {
ENV.APP.SERVER_URL = 'http://apistaging.server.io';
}
if (environment === 'production') {
ENV.APP.SERVER_URL = 'https://api.server.io';
}
return ENV;
};
【问题讨论】:
-
这看起来是对的,也许您只为一个环境配置授权器?
-
@marcoow 我已经添加了完整的 environment.js 文件。你看有什么问题吗?我不认为是环境的范围对吗?我使用的是简单身份验证 (0.6.7) 的凉亭版本,而不是 ember-cli 会产生差异的版本?
-
您正在使用 SimpleAuth 全局,在使用 Ember CLI 时不应该这样做。确保您使用的是 ember-cli-simple-auth,运行生成器等。
-
@marcoow 谢谢你。事实上,必须使用简单身份验证的 ember-cli 版本。现在我已经创建了一个授权人!下一个问题是什么时候调用授权方法?有几个地方我希望调用它。所有ajax请求和页面刷新时。我似乎无法让它发挥作用。我读了文档,但不是很清楚..
-
每个 XHR 请求都会调用授权方的
authorize方法(除非它是跨源请求并且源未被列入白名单) - 请在此处查看文档:github.com/simplabs/ember-simple-auth#authorizers。
标签: ember.js ember-simple-auth