TsgcWebSocketProxyServer › 属性 › Authentication
为连接到代理的传入 WebSocket 客户端启用并配置用户名/密码身份验证。
property Authentication: TsgcWSAuthenticationServer_Options read FAuthentication write SetAuthentication;
Enabled=False
当 Authentication.Enabled 为 True 时,代理在对 Proxy 中定义的上游服务器建立匹配链路之前,先根据 AuthUsers 列表(格式为每行 user=password)对每个传入的下游连接进行身份验证。支持三种内置方案:Session(客户端先发 HTTP GET,收到会话 id,然后将 id 作为参数打开 WebSocket)、URL(用户名和密码在 WebSocket 握手的查询字符串中发送)和 Basic(HTTP 基本访问认证)。OnAuthentication 事件允许您插入自定义凭证检查;OnUnknownAuthentication 处理 Bearer/JWT 等非标准方案。认证仅在下游端执行;代理不会自动将凭证转发给上游服务器。
oProxy := TsgcWebSocketProxyServer.Create(nil);
oProxy.Authentication.Enabled := true;
oProxy.Authentication.Basic.Enabled := true;
oProxy.Authentication.AuthUsers.Add('user=secret');
oProxy.Proxy.Host := 'upstream.example.com';
oProxy.Proxy.Port := 8080;
oProxy.Active := true;