任何信息系统的一个非常重要的部分是能够对用户进行身份验证。 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. CentOS6启动流程

    CentOS6启动流程 1.加载BIOS的硬件信息,获取第一个启动设备 在通电之后,CentOS6会进行加电自检(Power On Self Test),这个过程主要是由BIOS完成的.在自检完毕,会 ...

  2. Vue自动化工具(Vue-CLI)的安装

    安装VUM 前面学习了普通组件以后,接下来我们继续学习单文件组件则需要提前先安装准备一些组件开发工具.否则无法使用和学习单文件组件. 一般情况下,单文件组件,我们运行在 自动化工具vue-CLI中,可 ...

  3. GIL锁,线程池

    内容梗概: 1.线程队列 2.线程池 3.GIL锁 1.线程队列 1.1先进先出队列(FIFO)import queueq = queue.Queue(3)q.put(1)q.put(2)q.put( ...

  4. 使用SetInterval时函数不能传参问题

    无论是window.setTimeout还是window.setInterval,在使用函数名作为调用句柄时都不能带参数,而在许多场合必须要带参数,这就需要想方法解决.经网上查询后整理如下:例如对于函 ...

  5. virtualbox不能安装64位操作系统

    现在virtualbox 还是比较好用的虚拟机.新建立一个不同的操作系统还是非常方便. virtualbox下载地址  https://www.virtualbox.org/wiki/Download ...

  6. GetMapping 和 PostMapping最大的差别(转)

    原文地址:GetMapping 和 PostMapping  Spring4.3中引进了{@GetMapping.@PostMapping.@PutMapping.@DeleteMapping.@Pa ...

  7. [NOIP 2015TG D1T3] 斗地主

    题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...

  8. Maven 99.0-does-not-exist构建空包,排查依赖

    空包作用 作用:强制排除所有对该包的依赖: 空包制作 构建一个空包pom.xml,如下图所示: <?xml version="1.0" encoding="UTF- ...

  9. Swagger 路径过滤 -PreSerializeFilters

    Swagger 默认显示所有api, 如果要做路径过滤,可以这样做. //过滤,只显示部分api app.UseSwagger(c=> { c.PreSerializeFilters.Add(( ...

  10. 开发环境转Mac FAQ

    vs2017 for mac, 默认的源代码管理工具是git, 不是svn, 安装source tree,注册bitbucket(免费1G私有空间),整合的比较好(国内的码云也能支持,不过是用账号密码 ...