TsgcWebSocketHTTPServer › 属性 › Authentication
启用并配置对传入 WebSocket 和 HTTP 连接的用户名/密码认证。
property Authentication: TsgcWSAuthenticationServer_Options read FAuthentication write SetAuthentication;
Enabled=False
当 Authentication.Enabled 为 True 时,服务器会根据 AuthUsers 列表(格式为每行 user=password)对每个传入连接进行身份验证。支持三种内置方案:Session(客户端先发起 HTTP GET,接收会话 id,然后将 id 作为参数传入 WebSocket 连接)、URL(用户名和密码在 WebSocket 握手的查询字符串中发送)和 Basic(HTTP 基本访问身份验证,仅供 VCL 客户端/服务器和 HTTP 请求使用)。OnAuthentication 事件允许您插入自定义凭据检查;OnUnknownAuthentication 处理非标准方案(如 Bearer/JWT)。使用 CustomHeaders 可添加在身份验证错误时返回的额外 HTTP 标头。
oServer := TsgcWebSocketHTTPServer.Create(nil);
oServer.Authentication.Enabled := true;
oServer.Authentication.Basic.Enabled := true;
oServer.Authentication.AuthUsers.Add('user=secret');
oServer.Active := true;