任何信息系统的一个非常重要的部分是能够对用户进行身份验证。 kbmMW在这里提供了非常强大的机制。 TkbmMWSimpleClient提供简单的用户身份验证机制,您可以在连接到应用程序服务器时传递UserName和Password。 但是,要创建最灵活和最强大的身份验证机制,有必要编写一些代码......这里的标准方法是使用令牌(tokens)来验证客户端请求,这样用户名和密码就不会在所有客户端请求上传递,这种方式是更安全的。

服务器生成令牌而不是客户端。 客户只是接收回传的内容。 您可以使用clientident.customdata来存储用户的id,但是您也可以使用它来返回在“perm”中定义的内容。 我认为这种方法更好,所以看下面的代码:

unit Unit11;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Data.DB,
kbmMWServer, kbmMemTable, kbmMWSecurity, kbmMWExceptions,kbmMWuniDAC,
DBAccess, Uni, kbmMWCustomConnectionPool; type
TForm11 = class(TForm)
mwServer: TkbmMWServer;
Memo1: TMemo;
tbmusers: TkbmMemTable;
mwcpool: TkbmMWUNIDACConnectionPool;
UniConnection1: TUniConnection;
procedure mwServerAuthenticate(Sender: TObject;
ClientIdent: TkbmMWClientIdentity; var Perm: TkbmMWAccessPermissions;
var AMessage: string);
private
function IsUserLegit(UserName,password:string;var customdata,token:string):Boolean;
function IsTokenLegit(token:string):Boolean;
public
{ Public declarations }
end; var
Form11: TForm11; implementation {$R *.dfm} function TForm11.IsTokenLegit(token: string): Boolean;
begin
tbmusers.Lock;
try
if tbmusers.Locate('token',token,[]) then
begin
Result:=true;
tbmusers.Edit;
tbmusers.FieldByName('exppiry').AsDateTime:=Now+(/(*));//当前时间+15分钟
tbmusers.Post;
end
else
Result:=false;
finally
tbmusers.Unlock;
end;
end; function TForm11.IsUserLegit(UserName, password: string; var customdata,
token: string): Boolean;
var
conn:TkbmMWUNIDACConnection;
qry:TUniQuery;
begin
Result := False;
qry := TUniQuery.Create(nil);
conn := TkbmMWUNIDACConnection(mwcpool.GetBestConnection(true, -, nil, ));
try
qry.Connection := conn.Database;
qry.SQL.Add('select id from employees where name=:username and password=:password');
qry.ParamByName('username').AsString := UserName;
qry.ParamByName('password').AsString := password;
qry.Open;
if qry.Eof then
begin
Result := False;//用户名或密码错误
end
else
begin
//用户与密码正确,建立token并返回customdata.
customdata := qry.Fields[].AsString;
//建立简单的token
token := customdata + IntToStr(GetTickCount);
Result := True;
end;
finally
FreeAndNil(qry);
FreeAndNil(conn);
end;
end; procedure TForm11.mwServerAuthenticate(Sender: TObject;
ClientIdent: TkbmMWClientIdentity; var Perm: TkbmMWAccessPermissions;
var AMessage: string);
var
userdata,
token: string;
begin // no access by default
perm:=[];
//check to see if user already logged in
if clientident.token='' then
begin
memo1.lines.add(clientident.password);
if IsUserLegit(clientident.username,clientident.password,userdata,token) then
begin
clientident.Data:=userdata;
clientident.Token:=token;
perm:=[mwapRead, mwapWrite, mwapDelete, mwapExecute];
try
tbmusers.Lock;
tbmusers.Insert;
tbmusers.FieldByName('id').AsString:=userdata;
tbmusers.FieldByName('token').AsString:=token;
tbmusers.FieldByName('expiry').AsDateTime:=now+(/(*));
tbmusers.Post;
finally
tbmusers.Unlock;
end;
end
else
raise EkbmMWAuthException.Create(,'Username or Password Invalid');
end
else
begin
if IsTokenLegit(clientident.token) then
perm:=[mwapRead, mwapWrite, mwapDelete, mwapExecute]
else
raise EkbmMWAuthException.Create(,'Timeout');
end;
clientident.Username:='';
clientident.Password:='';
end; end.

kbmMW User authentication的更多相关文章

  1. KbmMW 4.5 发布

    We are happy to announce the release of kbmMW v. 4.50.00 Professional, Enterprise and CodeGear Editi ...

  2. KbmMW 4.40.00 正式版发布

    经过快3个月的测试,kbmmw 4.40 正式版终于在圣诞节前发布了. We are happy to announce the availability of a new kbmMW release ...

  3. KbmMW 4.40.00 测试发布

    经过漫长的等待,支持移动开发的kbmmw 4.40.00 终于发布了,这次不但支持各个平台的开发, 而且增加了认证管理器等很多新特性,非常值得升级.具体见下表. 4.40.00 BETA 1 Oct ...

  4. WCF : 修复 Security settings for this service require Windows Authentication but it is not enabled for the IIS application that hosts this service 问题

    摘要 : 最近遇到了一个奇怪的 WCF 安全配置问题, WCF Service 上面配置了Windows Authentication. IIS上也启用了 Windows Authentication ...

  5. Atitit HTTP 认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结

    Atitit HTTP认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结 1.1. 最广泛使用的是基本验证 ( ...

  6. [转]Web APi之认证(Authentication)及授权(Authorization)【一】(十二)

    本文转自:http://www.cnblogs.com/CreateMyself/p/4856133.html 前言 无论是ASP.NET MVC还是Web API框架,在从请求到响应这一过程中对于请 ...

  7. smtplib.SMTPAuthenticationError: (535, b'Error: authentication failed')解决办法

    raise SMTPAuthenticationError(code, resp) smtplib.SMTPAuthenticationError: (535, b'Error: authentica ...

  8. SharePoint Claim base authentication EnsureUser 不带claim(i:0#.w|)user Failed

    环境信息: 带有Form base authentication(FBA).Active Directory Federation Services(ADFS).以及windows Authentic ...

  9. 执行ssh-add时出现Could not open a connection to your authentication agent

    若执行ssh-add /path/to/xxx.pem是出现这个错误:Could not open a connection to your authentication agent,则先执行如下命令 ...

随机推荐

  1. Tempter of the Bone HDU - 1010

    The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it u ...

  2. mysql存储引擎的对比

  3. 动态改变Listview的item背景颜色和item中字体的颜色

    https://blog.csdn.net/qq_14813933/article/details/50417859

  4. 3月22 关于CSS

    CSS(Cascading Style Sheep 叠层样式表,作用是美化HTML网页)/*注释内容*/ 为注释的方法. 样式表的分类: 1.内联样式表 和HTML联合显示,控制精确,但是可重用性差, ...

  5. Oracle 聚合函数

    聚合函数: SQL中提供的聚合函数可以用来统计.求和.求最值等等. 此处采用Oracle 11g中其他用户SCOTT中的EMP表,进行演示. –COUNT:统计行数量 COUNT(*)统计的是结果集的 ...

  6. Opatch使用细则

    Patch 21352635 - Database Patch Set Update 11.2.0.4.8 一.OPatch工具检查及升级 OPatch工具包,在安装目录$ORACLE_HOME下,P ...

  7. win10下使用python访问vmbox中的redis

    了解到redis没有windows的官方支持,所以在vmbox中的ubuntu装了redis#在ubuntu中 #搜索redis相关软件信息 apt-cache search ^redis #不清楚为 ...

  8. oracle 11g完全安装教程(CentOS)

    oracle下载链接:http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html oracle ...

  9. axios API速查表

    原来jq自带ajax方法,但是近期项目用vue,在vue项目中发送ajax请求原来用vue resource,现在更推荐使用axios,因为axios和vue更配! GET 请求 // Make a ...

  10. 运行网站项目时,有时出现Bad Request,该怎么解决?

    有时运行网站项目时,出现Bad Request问题