任何信息系统的一个非常重要的部分是能够对用户进行身份验证。 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. Remove Duplicates From Sorted Array leetcode java

    算法描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...

  2. element中使用button会刷新一遍页面

     会刷新:   <el-form-item> <button @click="register('form')" class="submitBtn&qu ...

  3. 【LeetCode】矩阵操作

    1. 矩阵旋转 将 n × n 矩阵顺时针旋转 90°. 我的思路是 “ 从外到内一层一层旋转 ”. 一个 n × n 矩阵有 (n + 1) / 2 层,每层有 4 部分,将这 4 部分旋转. 顺时 ...

  4. RocketMQ消息存储

    转载:RocketMQ源码学习--消息存储篇 消息中间件—RocketMQ消息存储(一) RocketMQ高性能之底层存储设计 存储架构 RMQ存储架构 上图即为RocketMQ的消息存储整体架构,R ...

  5. matlab global persistent变量

    global变量是全局的,在使用global变量的函数里需要用global声明所使用的变量. persistent类似global,不过仅对当前函数有作用,这样避免了外面的影响.当这个函数被clear ...

  6. 【框架】用excel管理测试用例需要的参数数据(二)

    一.总体思路 以类为excel名,测试方法名为sheet名,建立excel文件.用jxl包里的方法去读取excel文件里的内容,然后用testng里的dataprovider,将数据传递给测试用例 二 ...

  7. 逆袭之旅DAY15.东软实训.Oracle.约束、序列、视图、索引、用户管理、角色

    2018-07-11  08:26:00 有某个学生运动会比赛信息的数据库,保存了如下的表: 运动员sporter表:(运动员编号sporterid,运动员姓名name,运动员性别sex,所属系dep ...

  8. LY.JAVA面向对象编程.形式参数和返回值

    2018-07-09 13:29:16 运动员和教练案例 /* 教练和运动员案例(学生分析然后讲解) 乒乓球运动员和篮球运动员. 乒乓球教练和篮球教练. 为了出国交流,跟乒乓球相关的人员都需要学习英语 ...

  9. 请问微信小程序let和var以及const有什么区别

    在JavaScript中有三种声明变量的方式:var.let.const. var:声明全局变量,换句话理解就是,声明在for循环中的变量,跳出for循环同样可以使用. [JavaScript] 纯文 ...

  10. Python自然语言处理---TF-IDF模型

    一. 信息检索技术简述 信息检索技术是当前比较热门的一项技术,我们通常意义上的论文检索,搜索引擎都属于信息检索的范畴.信息检索的问题可以抽象为:在文档集合D上,对于关键词w[1]…w[k]组成的查询串 ...