【问题标题】:Feathersjs - Multiple authentication endpointsFeathersjs - 多个身份验证端点
【发布时间】:2019-08-20 13:22:26
【问题描述】:

我有来自两个不同客户端(角度客户端和 node.js 羽毛客户端)的传入连接,我希望他们使用两个不同的身份验证端点(基于两个单独表中的数据)。一个人应该针对 /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 feathers-authentication


    【解决方案1】:

    不确定过去是否可行,但使用当前的 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: []
      }
    };
    

    【讨论】:

      猜你喜欢
      • 2019-04-18
      • 2020-11-14
      • 2019-01-14
      • 1970-01-01
      • 1970-01-01
      • 2020-02-21
      • 1970-01-01
      • 2014-12-25
      • 2019-09-08
      相关资源
      最近更新 更多