datasnap 授权验证DSAuthenticationManager方法应用

服务端


1.服务端只需要增加DSAuthenticationManager1并且把dshttpservice的AuthenticationManager属性设置为DSAuthenticationManager1即可。

2.在DSAuthenticationManager1的OnUserAuthenticate方法中进行用户名密码验证操作具体示例代码如下:

procedure TfrmServerContainer.DSAuthenticationManager1UserAuthenticate(
  Sender: TObject; const Protocol, Context, User, Password: string;
  var valid: Boolean; UserRoles: TStrings);
begin
  if (User ='admin') and (Password ='pwd123') then
  begin
    valid := True;
  end
  else
    valid := False;
end;

3.也可以在dshttpservice中设置DSAuthUser及DSAuthPassword进行验证限制,并在DSAuthenticationManager1的roles中增加用户组对方法的访问,只需在OnUserAuthenticate中把用户加到相应的用户组即可。

代码如下:UserRoles.Add(AdminGroup‘); //加入到AdminGroup组别

 

客户端


1.可以设置SQLConnection的DSAuthUser和DSAuthPassword就行了

2.如果是用普通http协议的话,例如idhttp控件,则客户端代码如下:

procedure TForm15.Button2Click(Sender: TObject);
var
  url, params, Text: string;
  code: Integer;
  http: TIDHttp;
begin
  http:= TIDHttp.Create(nil);
  http.Request.BasicAuthentication := True;
  http.request.password := '密码';
  params := Edit1.Text;

  url:= 'http://192.168.10.182:8081/datasnap/rest/TSM/EchoString/';
  try
    text := http.Get(URL+TIdURI.ParamsEnCode(params));
    Edit2.Text := Text;
  except
    on E: Exception do
    begin

    end;
  end;
end;

 

posted @ 2016-10-09 10:57  胖达没有海  阅读(1716)  评论(0编辑  收藏  举报