【问题标题】:FeathersJS socketio jwt authenticationFeathersJS socketio jwt 身份验证
【发布时间】:2019-04-18 22:27:27
【问题描述】:

我正在尝试使用 feathersjs 向我的应用程序添加身份验证。我无法理解为什么我的jwt 令牌没有被发送,即使我从服务器接收到jwt

这是我的angular 代码:

import {CookieStorage} from 'cookie-storage';
import {JwtHelperService} from '@auth0/angular-jwt';
import feathers from '@feathersjs/feathers';
import socketio from '@feathersjs/socketio-client';
import auth from '@feathersjs/authentication-client';
import {tap} from 'rxjs/operators';
import * as io from 'socket.io-client';

const socket = io('http://localhost:3030');
const cookieStorage = new CookieStorage();
const client = feathers();

client.configure(socketio(socket));
client.configure(auth({ storage: cookieStorage }));

socket.emit('authenticate', userData, function(message, data) {
  console.log(message); // message will be null
  console.log(data); // data will be {"accessToken": "your token"}

  // You can now send authenticated messages to the server
});

此代码有效,我将jwt accessToken 返回到浏览器,但cookieStorage 部分没有保存cookie。如果我使用以下代码切换socket.emit('authenticate') 部分,它可以工作:

client.authenticate(userData)

但是,我想使用socketio 进行身份验证并将jwt 保存在cookie 中,而不是使用以下方法:

如何获取socketio 身份验证事件以在cookie 中设置jwt

【问题讨论】:

    标签: cookies socket.io jwt feathersjs


    【解决方案1】:

    client.authenticate(userData) 使用 SocketIO 进行身份验证(因为您已经在使用 client.configure(socketio(socket));)并将其存储在您传递的存储中(在您的情况下为 cookieStorage)。

    【讨论】:

    • 感谢您澄清这一点。我很难理解使用socket.emit('authenticate') 版本的身份验证方案的正确方法。此外,当我使用client.authenticate(userData) 方法时,似乎没有从default.config 获取cookie 名称(我在配置的cookie 部分下将其更改为自定义名称),而是得到一个cookie 名为 feathers-jwt,你知道为什么会这样吗?
    • 如果您使用的是 Feathers 客户端,则不必直接使用套接字。发送事件仅适用于直接使用 Socket.io 时。作为documented here,您必须将client.configure(auth()) 中的storageKey 选项设置为您需要的密钥。
    猜你喜欢
    • 2019-09-08
    • 2020-11-14
    • 2019-09-25
    • 2019-08-20
    • 1970-01-01
    • 2020-05-15
    • 2020-03-18
    • 2020-01-05
    • 1970-01-01
    相关资源
    最近更新 更多