如何在 Delphi REST 服务器中实现“授权:承载<Token>”?

问题描述 投票:0回答:1

我有一个正在运行的 REST 服务器(悉尼),它使用自定义标头(AuthToken:'Token')来授权用户。

当我尝试使用“Authorization: Bearer 'Token'”作为 POST 请求中的标头时,我在其余服务器中遇到异常,如下所示。 enter image description here

看起来我必须将一个过程分配给

TIdHTTPParseAuthenticationEvent
,但我找不到在哪里执行此操作。

rest delphi authorization indy delphi-10.4-sydney
1个回答
0
投票

当您创建 Datasnap REST 服务器项目时,它会为您创建一个默认的

TIdHTTPWebBrokerBridge
对象,它是
TIdCustomHTTPServer
后代。您可以在代码中为其
OnParseAuthentication
事件分配一个处理程序(因为它不是表单上的可视组件),例如:

procedure TForm1.FormCreate(Sender: TObject);
begin
  FServer := TIdHTTPWebBrokerBridge.Create(Self);
  FServer.OnParseAuthentication := ParseAuthentication; // <-- here
end;

procedure TForm1.ParseAuthentication(AContext: TIdContext;
  const AAuthType, AAuthData: String; var VUsername, VPassword: String;
  var VHandled: Boolean);
begin
  if TextIsSame(AAuthType, 'Bearer') then begin
    // process AAuthData and assign VUsername and VPassword as needed...
    VHandled := True;
  end;
end;
© www.soinside.com 2019 - 2024. All rights reserved.