Typescript Servicestack 客户端对于 SSE 事件的认证

4
我正在尝试使用TypeScript servicestack-client 连接到ServiceStack服务器的SSE事件。
通过发送Authenticate类并接收会话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的问题!因此,任何以下的频道订阅都会失败,因为servicestack客户端没有发送Cookies头。
我该如何实现经过身份验证的SSE连接呢?
1个回答

1
我刚刚为this commit添加了支持指定withCredentials EventSource选项的功能,默认值为true,因此如果您升级到最新的v0.0.34servicestack-client,它将自动启用。
如果需要,可以通过以下方式禁用:
var sseClient = new ServerEventsClient('http://ss_api_url', ['*'], { ... });

sseClient.withCredentials = false;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接