支持方
*需要 sgcWebSockets Enterprise Edition。在其他版本中,启动启用了 Negotiate 的服务器会引发异常 "Negotiate authentication requires sgcWebSockets Enterprise"。
Negotiate 是 Windows 集成身份验证所采用的 HTTP 身份验证方案。客户端通过 Kerberos(SPNEGO)证明其身份,在 Windows 上则可回退到 NTLM,因此已经登录域的用户无需输入密码即可连接。处于 Intranet 区域的浏览器、curl --negotiate 以及使用默认凭据的 .NET 客户端都可以应答该质询。
Authentication.Negotiate.Enabled(Boolean,默认值为 False):启用 Negotiate。它需要 Authentication.Enabled。Authentication.Negotiate.ServicePrincipal(String):可选的 SPN,例如 HTTP/host.example.com@EXAMPLE.COM。为空时,接受 keytab 中的任意密钥或计算机帐户。仅适用于 Indy 服务器,http.sys 会忽略它。Authentication.Negotiate.Keytab(String):仅限 Linux,服务 keytab 的路径。为空时使用 KRB5_KTNAME 或系统默认值。在 Windows 上以及 http.sys 会忽略它。OnNegotiateAuthentication(Connection: TsgcWSConnection; const aPrincipal: String; var Authenticated: Boolean):在 Kerberos 或 NTLM 交换完成时触发。Authenticated 传入时为 True,因此在没有处理程序的情况下,Windows 或 Kerberos 已验证的每个主体都会被接受。将其设为 False 即可拒绝(客户端收到 401)。主体在 Windows 上为 DOMAIN\user,在 Linux 上为 user@REALM。在 Indy 服务器上,之后可通过 TsgcWSConnectionServer(Connection).Authentication.User 读取,此时 AuthType = authNegotiate。下面的服务器只接受两个主体。事件按主体名称限制访问。
uses
sgcWebSocket, sgcWebSocket_Classes;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
oServer: TsgcWebSocketHTTPServer;
procedure OnNegotiateAuthenticationEvent(Connection: TsgcWSConnection;
const aPrincipal: String; var Authenticated: Boolean);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
oServer := TsgcWebSocketHTTPServer.Create(Self);
oServer.Port := 8080;
oServer.Authentication.Enabled := True;
oServer.Authentication.Negotiate.Enabled := True;
oServer.Authentication.Negotiate.ServicePrincipal :=
'HTTP/host.example.com@EXAMPLE.COM';
oServer.OnNegotiateAuthentication := OnNegotiateAuthenticationEvent;
oServer.Active := True;
end;
procedure TForm1.OnNegotiateAuthenticationEvent(Connection: TsgcWSConnection;
const aPrincipal: String; var Authenticated: Boolean);
begin
Authenticated := SameText(aPrincipal, 'EXAMPLE\alice') or
SameText(aPrincipal, 'EXAMPLE\bob');
end;
在 Linux 上,请将 Authentication.Negotiate.Keytab 设置为 keytab 的路径,例如 /etc/sgc/http.keytab,并在事件中使用 Kerberos 主体,例如 alice@EXAMPLE.COM。
401 以及 WWW-Authenticate: Negotiate,如果同时启用了 Basic,还会附带 Basic realm 质询。Authorization: Negotiate <token> 应答。Authentication.AllowNonAuth 为 True 时,不带凭据的请求会以匿名方式被接受,而携带 Negotiate 令牌的请求仍会进行身份验证。curl --negotiate -u : http://host.example.com:8080/
HTTP/host。浏览器只会向 Intranet 区域内或允许列表中的站点发送 Negotiate。Keytab 指向它。