首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Feathersjs -多个身份验证端点

Feathersjs -多个身份验证端点
EN

Stack Overflow用户
提问于 2019-03-29 14:57:51
回答 1查看 1.1K关注 0票数 1

我有来自两个不同客户端(角客户端和node.js羽毛客户端)的传入连接,我希望它们使用两个不同的身份验证端点(基于两个不同表中的数据)。一个应该针对/users服务进行身份验证,另一个应该针对/users2服务进行身份验证。

如何才能做到这一点?

这是它与一个身份验证端点的工作方式:

代码语言:javascript
复制
// 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')
      ]
    }
  });

};

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-19 16:41:56

不确定过去是否可行,但使用当前的FeathersJS (4.5.0),您可以使用不同的配置创建多个AuthenticationService实例:

代码语言:javascript
复制
//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: []
  }
};
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55420184

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档