TsgcWebSocketServer | 服务器身份验证

TsgcWebSocket 服务器支持 3 种类型的身份验证:

 

 

您可以使用 AuthUsers 属性设置已认证用户列表,只需按以下格式设置用户:user=password

OnAuthentication

每当服务器从客户端收到身份验证请求时,都会调用此事件,以指示用户是否已通过身份验证。

使用 Authenticated 参数来接受或拒绝连接。

 


procedure OnAuthentication(Connection: TsgcWSConnection; aUser, aPassword: string; 
  var Authenticated: Boolean);
begin
  if ((aUser = 'user') and (aPassword = 'secret')) then
    Authenticated := true
  else
    Authenticated := false;
end;

 

 

OnUnknownAuthentication

如果身份验证类型不受默认支持(例如 JWT),您仍可使用此事件来接受或拒绝连接。只需读取参数并决定是否接受连接即可。

 


procedure OnUnknownAuthentication(Connection: TsgcWSConnection; AuthType, AuthData: string; 
  var aUser, aPassword: string; var Authenticated: Boolean);
begin
  if AuthType = 'Bearer' then
  begin
    if AuthData = 'jwt_token' then
      Authenticated := true
    else
      Authenticated := false;
  end
  else
    Authenticated := false;
end;