【问题标题】:Authentication to Featherjs using socket.io-client使用 socket.io-client 对 Featherjs 进行身份验证
【发布时间】:2019-08-17 05:26:49
【问题描述】:

如何使用直接连接 (https://docs.feathersjs.com/api/client/socketio.html#direct-connection) 向 Feathersjs (https://docs.feathersjs.com/api/client/socketio.html#authentication) 进行身份验证?下面的代码表明我的 accessToken 格式不正确,但我怀疑还有更多的东西可以让它工作。我在哪里获取 accessToken?

app.js(客户端):

import express from 'express';

const socket = require('socket.io-client')('http://localhost:3030', {
  transports: ['websocket']
});

socket.emit('authenticate', { 
  strategy: 'jwt',
  accessToken: 'what to enter here'
}, (message: any, data: any) => {
  console.log(message);
  console.log(data);
});

const app = express();

app.get('/', (req, res) => res.send('Up and running!'));

app.listen(4390, () => console.log('Example app listening on port 4390!'));

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');

  // Set up authentication with the secret
  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')
      ]
    }
  });
};

我尝试将秘密用作 accessToken,但它没有用 :) default.json(羽毛服务器配置)

"authentication": {
    "secret": "r323r32rada86700f18d82ea1d3e74fb58141dbefcd7460ef71736759265e347151a12d68dff50aa59c05e2f18db88661be8ae91a3f12932fddea8891b5ca9f63b5e4fc650edabc59d0d14e8fe4ea54256e7e386845321ab58a320a9ec99438bd058a3fbbda65dadf97bc9585ea82f72201f932beqwdqwdqwd5761a0d0be3e95474db5b9c8a3f4c7303beed0344a1768ba5dad6a1c916d183ea5dd923587768661ff0b08f25ed85dff4ff4e6b58327fe5914e5e7fb2356ee67754b102434f22686444a35fc38c75bcdd6386240a22e0cf62bdc7f227200868da387174b365af2afa7dec378c4ccf22956b134a3ec961fd1ba8d3dc85a7594ab711",
    "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"
    }
  },
...

感谢所有回复!

【问题讨论】:

  • 嗨罗宾,您需要通过登录或使用自定义策略创建访问令牌,从而使两者都有一个共享密钥,用于相互验证。
  • 谢谢@mchaffe!但是登录到哪里?再次感谢。

标签: node.js socket.io feathersjs feathers-authentication


【解决方案1】:

为了获得accessToken,您通常需要使用email/password 或oauth 的策略进行身份验证。这将返回一个accessToken,然后您可以将其用于jwt 身份验证。

另一种方法是使用custom authentication strategy,这将允许您拥有两个服务器可以用来相互通信的共享密钥。

【讨论】:

    【解决方案2】:

    谢谢@mchaffe!在您的帮助下,我设法解决了它。这是使用的代码:

    import dotenv from 'dotenv';
    
    // Load environments
    const config = dotenv.config()
    if (config.error) throw config.error
    
    const io = require('socket.io-client');
    const feathers = require('@feathersjs/client');
    const localStorage = require('localstorage-memory');
    
    const client = feathers();
    
    const socket = io('http://localhost:3030/', {
      transports: ['websocket'],
      forceNew: true
    });
    
    client.configure(feathers.socketio(socket), {
      timeout: 10000
    });
    
    client.configure(feathers.authentication({
      jwtStrategy: 'jwt',
      storage: localStorage,
      storageKey: 'some-token'
    }));
    
    const payload = {
      strategy: 'local',
      email: process.env.FEATHERS_AUTHENTICATION_EMAIL,
      password: process.env.FEATHERS_AUTHENTICATION_PASSWORD
    };
    
    client.authenticate(payload).then((response: any) => {
      // Do stuff to hooray here
      console.log('Access Token: ' + response.accessToken);
    
      // Works!
      socket.emit('get', 'logger', 1, (error: any, log: any) => {
        console.log('Found log: ' + JSON.stringify(log));
      });
    
    }).catch((e: any) =>  {
      console.log('Error: ' + e); 
    });
    
    

    如果您有改进的建议,我会全力以赴! :) 看来我可以使用 socket.emit 方法从数据库中访问数据。我需要验证返回的 accessToken 吗?再次感谢!

    【讨论】:

    • 如果你使用的是客户端,应该可以使用客户端调用feathers服务,而不是直接使用socket。 client.service(‘messages’).create({ text: ‘new message’ })这个会使用sockets发送,所以不需要直接使用socket连接。
    猜你喜欢
    • 2017-11-24
    • 2018-08-03
    • 2013-11-02
    • 2018-06-08
    • 2014-12-01
    • 1970-01-01
    • 2022-07-11
    • 2010-11-18
    • 1970-01-01
    相关资源
    最近更新 更多