authentication - SSE 事件的 Typescript Servicestack 客户端身份验证

标签 authentication typescript cookies servicestack server-sent-events

我正在尝试使用 typescript servicestack-client从 ServiceStack 服务器 Hook SSE 事件。

通过发送 Authenticate 类并接收 session cookie 进行身份验证:

import { ServerEventsClient } from 'servicestack-client';

export class ServicestackService {
    private sseClient: ServerEventsClient;

    constructor() {
        this.createSseClient();
    }

    login(username: string, password: string) {
        let request = new Authenticate(username, password);
        return this.sseClient.serviceClient.post(request);
    }

    startClient() {
        this.sseClient.start();
        console.log('eventSource created');
    }

    subscribeChannel(channel: string) {
        this.sseClient.subscribeToChannels(channel);
    }

    private createSseClient() {
        this.sseClient = new ServerEventsClient('http://ss_api_url', ['*'], {
        handlers: { ...define handlers for onConnect & onMessage here... }
        }
    }

}    

上面的代码成功地再次验证了 servicestack API,并且安全的 API 请求运行良好。 startClient() 方法在所有登录身份验证完成后调用,并创建 eventSource 对象作为 this.sseClient 中的属性。

但是这个对象有 withCredentials: false! 因此,以下任何 channel 订阅都因未通过身份验证而失败,因为 servicestack 客户端未发送 Cookies header 。

如何实现经过身份验证的 SSE 连接?

最佳答案

我刚刚在 this commit 中添加了对指定 withCredentials EventSource 选项的支持默认为 true,因此如果您升级到 servicestack-client 的最新 v0.0.34,它将自动启用.

如果需要,可以通过以下方式禁用它:

var sseClient = new ServerEventsClient('http://ss_api_url', ['*'], { ... });

sseClient.withCredentials = false;

关于authentication - SSE 事件的 Typescript Servicestack 客户端身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43719968/

相关文章:

javascript - 是否有一种通用的方法来了解用户是否登录(或进行了身份验证)适用于所有(或至少大多数网站)的网站?

javascript - ECharts LinearGradient 的 API 是如何工作的?

typescript - Typescript 中的通用身份函数

ruby-on-rails - Rails Cookie 问题

security - Spring MVC : how to require POST parameters in the request body and ignore query string parameters?

javascript - Koa Auth 流程与 compose

angular - 使用 ngx-bootstrap modal 将数据传递给 modal

PHP - 关闭浏览器后 session 销毁

javascript - 在 JavaScript(和开发工具)中无法访问 Cookie,但会与 XHR 请求一起发送(不使用 httponly)

reactjs - Next.js 客户端保护路由,无需服务器端渲染