服务器端
1.TDSAuthenticationManager有两个主要的事件
在这个事件里面,看看检测连上来的用户名,密码是否合法,valid如果置为false,这就为非法连接了,DSServer会立刻抛出异常后close连接。
另外,UserRoles的设计,我觉得比RO高明。
procedure TServerContainer1.DSAuthenticationManager1UserAuthenticate(
Sender: TObject; const Protocol, Context, User, Password: string;
var valid: Boolean; UserRoles: TStrings);
begin
valid := User = 'zyz';
if User = 'admin' then
UserRoles.Add('admins');
end;
在这个事件里面,判断已经连接上来的用户,对ServerMethod的调用是否合法,注视里也写了,默认是如何检测是否合法的。
procedure TServerContainer1.DSAuthenticationManager1UserAuthorize(
Sender: TObject; EventObject: TDSAuthorizeEventObject;
var valid: Boolean);
begin
{ TODO : Authorize a user to execute a method.
Use values from EventObject such as UserName, UserRoles, AuthorizedRoles and DeniedRoles.
Use DSAuthenticationManager1.Roles to define Authorized and Denied roles
for particular server methods. }
//valid := True;
end;
上面我说UserRoles的设计比较高明,主要还是因为这个UserRole的设计用到了java的那种注释类的技术,比如服务器上这么定义一个方法
[TRoleAuth('admins')]
function EchoString(Value: string): string;
这样定义后,就算不写DSAuthenticationManager1UserAuthorize,TDSAuthenticationManager也会自动帮你检查该角色是否有权利调用该ServerMethod。RTTI估计是学Java的Annotation才增加了TCustomAttribute。2.客户端
客户端很简单了,设置SQLConnection的DSAuthUser和DSAuthPassword就行了。
没有评论:
发表评论