Feathersjs - 多个身份验证端点
Feathersjs - Multiple authentication endpoints
我有来自两个不同客户端(angular 客户端和一个 node.js feathers 客户端)的传入连接,我希望它们使用两个不同的身份验证端点(基于两个单独表中的数据)。一个应该针对 /users 服务进行身份验证,另一个应该针对 /users2 服务进行身份验证。
如何实现?
这是它与一个身份验证端点一起工作的方式:
// default.json
"authentication": {
"secret": "<secret>",
"strategies": [
"jwt",
"local"
],
"path": "/authentication",
"service": "users",
"jwt": {
"header": {
"typ": "access"
},
"audience": "https://yourdomain.com",
"subject": "anonymous",
"issuer": "feathers",
"algorithm": "HS256",
"expiresIn": "1d"
},
"local": {
"entity": "user",
"usernameField": "email",
"passwordField": "password"
}
}
// authentication.js
const authentication = require('@feathersjs/authentication');
const jwt = require('@feathersjs/authentication-jwt');
const local = require('@feathersjs/authentication-local');
module.exports = function (app) {
const config = app.get('authentication');
app.configure(authentication(config));
app.configure(jwt());
app.configure(local());
app.service('authentication').hooks({
before: {
create: [
authentication.hooks.authenticate(config.strategies),
],
remove: [
authentication.hooks.authenticate('jwt')
]
}
});
};
谢谢!
不确定过去是否可行,但使用当前的 FeathersJS (4.5.0),您可以创建多个具有不同配置的 AuthenticationService 实例:
//default.json
"authentication": {
"entity": "user",
"service": "users",
"secret": ** ** ** ** ** * ,
"authStrategies": [
"jwt",
"local"
],
...
},
"authentication": {
"entity": "user2",
"service": "users2",
"secret": ** ** ** ** ** * ,
"authStrategies": [
"jwt",
"local"
],
...
},
...
// authentication.ts
...
export default function(app: Application) {
const authentication = new AuthenticationService(app, 'authentication');
authentication.register('jwt', new JWTStrategy());
authentication.register('local', new LocalStrategy());
app.use('/authentication/users', authentication2);
const authentication2 = new AuthenticationService(app, 'authentication2');
authentication2.register('jwt', new JWTStrategy());
authentication2.register('local', new LocalStrategy());
app.use('/authentication/users2', authentication2);
app.configure(expressOauth());
}
...
// user.hooks.ts / user2.hooks.ts
import * as feathersAuthentication from '@feathersjs/authentication';
import * as local from '@feathersjs/authentication-local';
// Don't remove this comment. It's needed to format import lines nicely.
const {
authenticate
} = feathersAuthentication.hooks;
const {
hashPassword,
protect
} = local.hooks;
export default {
before: {
all: [],
find: [authenticate('jwt')],
get: [authenticate('jwt')],
create: [hashPassword('password')],
update: [hashPassword('password'), authenticate('jwt')],
patch: [hashPassword('password'), authenticate('jwt')],
remove: [authenticate('jwt')]
},
after: {
all: [
// Make sure the password field is never sent to the client
// Always must be the last hook
protect('password')
],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},
error: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
}
};
我有来自两个不同客户端(angular 客户端和一个 node.js feathers 客户端)的传入连接,我希望它们使用两个不同的身份验证端点(基于两个单独表中的数据)。一个应该针对 /users 服务进行身份验证,另一个应该针对 /users2 服务进行身份验证。
如何实现?
这是它与一个身份验证端点一起工作的方式:
// default.json
"authentication": {
"secret": "<secret>",
"strategies": [
"jwt",
"local"
],
"path": "/authentication",
"service": "users",
"jwt": {
"header": {
"typ": "access"
},
"audience": "https://yourdomain.com",
"subject": "anonymous",
"issuer": "feathers",
"algorithm": "HS256",
"expiresIn": "1d"
},
"local": {
"entity": "user",
"usernameField": "email",
"passwordField": "password"
}
}
// authentication.js
const authentication = require('@feathersjs/authentication');
const jwt = require('@feathersjs/authentication-jwt');
const local = require('@feathersjs/authentication-local');
module.exports = function (app) {
const config = app.get('authentication');
app.configure(authentication(config));
app.configure(jwt());
app.configure(local());
app.service('authentication').hooks({
before: {
create: [
authentication.hooks.authenticate(config.strategies),
],
remove: [
authentication.hooks.authenticate('jwt')
]
}
});
};
谢谢!
不确定过去是否可行,但使用当前的 FeathersJS (4.5.0),您可以创建多个具有不同配置的 AuthenticationService 实例:
//default.json
"authentication": {
"entity": "user",
"service": "users",
"secret": ** ** ** ** ** * ,
"authStrategies": [
"jwt",
"local"
],
...
},
"authentication": {
"entity": "user2",
"service": "users2",
"secret": ** ** ** ** ** * ,
"authStrategies": [
"jwt",
"local"
],
...
},
...
// authentication.ts
...
export default function(app: Application) {
const authentication = new AuthenticationService(app, 'authentication');
authentication.register('jwt', new JWTStrategy());
authentication.register('local', new LocalStrategy());
app.use('/authentication/users', authentication2);
const authentication2 = new AuthenticationService(app, 'authentication2');
authentication2.register('jwt', new JWTStrategy());
authentication2.register('local', new LocalStrategy());
app.use('/authentication/users2', authentication2);
app.configure(expressOauth());
}
...
// user.hooks.ts / user2.hooks.ts
import * as feathersAuthentication from '@feathersjs/authentication';
import * as local from '@feathersjs/authentication-local';
// Don't remove this comment. It's needed to format import lines nicely.
const {
authenticate
} = feathersAuthentication.hooks;
const {
hashPassword,
protect
} = local.hooks;
export default {
before: {
all: [],
find: [authenticate('jwt')],
get: [authenticate('jwt')],
create: [hashPassword('password')],
update: [hashPassword('password'), authenticate('jwt')],
patch: [hashPassword('password'), authenticate('jwt')],
remove: [authenticate('jwt')]
},
after: {
all: [
// Make sure the password field is never sent to the client
// Always must be the last hook
protect('password')
],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},
error: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
}
};