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 标头。
Negotiate(Kerberos / SPNEGO,在 Windows 上可回退到 NTLM)在 Authentication.Negotiate 子属性中配置,并且需要 sgcWebSockets Enterprise Edition。Negotiate.Enabled(Boolean,默认值为 False)用于启用该功能,并且需要 Authentication.Enabled。Negotiate.ServicePrincipal(String)是可选的 SPN,例如 HTTP/host.example.com@EXAMPLE.COM;为空时,接受 keytab 中的任意密钥或计算机帐户。Negotiate.Keytab(String,仅限 Linux)是服务 keytab 的路径;为空时使用 KRB5_KTNAME 或系统默认值。结果由 OnNegotiateAuthentication 报告。请参阅 Kerberos 和 Negotiate 身份验证。
oServer := TsgcWebSocketHTTPServer.Create(nil);
oServer.Authentication.Enabled := true;
oServer.Authentication.Basic.Enabled := true;
oServer.Authentication.AuthUsers.Add('user=secret');
oServer.Active := true;