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

时间:2017-05-01 13:33:57

标签: authentication typescript cookies servicestack server-sent-events

我正在尝试使用typescript servicestack-client挂钩来自ServiceStack Server的SSE事件。

通过发送Authenticate类并接收sesssion 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... }
        }
    }

}    

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

但此对象有withCredentials: false 因此,以下任何通道订阅都会因未经过身份验证而失败,因为servicestack客户端未发送Cookies标头。

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

1 个答案:

答案 0 :(得分:1)

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

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

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

sseClient.withCredentials = false;