SSE 事件的 Typescript Servicestack 客户端身份验证
Typescript Servicestack Client authentication for SSE events
我正在尝试使用打字稿 servicestack-client 从 ServiceStack 服务器挂钩 SSE 事件。
通过发送 Authenticate
class 并接收会话 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()
方法在所有登录验证完成后调用,并在 this.sseClient
.
中创建 eventSource
object 作为 属性
但是这个object有withCredentials: false
!
因此,以下任何频道订阅都因未通过身份验证而失败,因为 servicestack 客户端未发送 Cookies
header.
如何实现经过身份验证的 SSE 连接?
我刚刚在 this commit which defaults to true
so it will be automatically enabled if you upgrade to the latest v0.0.34 of servicestack-client 中添加了对指定 withCredentials
EventSource 选项的支持。
如果需要,可以通过以下方式禁用它:
var sseClient = new ServerEventsClient('http://ss_api_url', ['*'], { ... });
sseClient.withCredentials = false;
我正在尝试使用打字稿 servicestack-client 从 ServiceStack 服务器挂钩 SSE 事件。
通过发送 Authenticate
class 并接收会话 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()
方法在所有登录验证完成后调用,并在 this.sseClient
.
eventSource
object 作为 属性
但是这个object有withCredentials: false
!
因此,以下任何频道订阅都因未通过身份验证而失败,因为 servicestack 客户端未发送 Cookies
header.
如何实现经过身份验证的 SSE 连接?
我刚刚在 this commit which defaults to true
so it will be automatically enabled if you upgrade to the latest v0.0.34 of servicestack-client 中添加了对指定 withCredentials
EventSource 选项的支持。
如果需要,可以通过以下方式禁用它:
var sseClient = new ServerEventsClient('http://ss_api_url', ['*'], { ... });
sseClient.withCredentials = false;