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

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

  1. unit Unit11;
  2.  
  3. interface
  4.  
  5. uses
  6. Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  7. Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Data.DB,
  8. kbmMWServer, kbmMemTable, kbmMWSecurity, kbmMWExceptions,kbmMWuniDAC,
  9. DBAccess, Uni, kbmMWCustomConnectionPool;
  10.  
  11. type
  12. TForm11 = class(TForm)
  13. mwServer: TkbmMWServer;
  14. Memo1: TMemo;
  15. tbmusers: TkbmMemTable;
  16. mwcpool: TkbmMWUNIDACConnectionPool;
  17. UniConnection1: TUniConnection;
  18. procedure mwServerAuthenticate(Sender: TObject;
  19. ClientIdent: TkbmMWClientIdentity; var Perm: TkbmMWAccessPermissions;
  20. var AMessage: string);
  21. private
  22. function IsUserLegit(UserName,password:string;var customdata,token:string):Boolean;
  23. function IsTokenLegit(token:string):Boolean;
  24. public
  25. { Public declarations }
  26. end;
  27.  
  28. var
  29. Form11: TForm11;
  30.  
  31. implementation
  32.  
  33. {$R *.dfm}
  34.  
  35. function TForm11.IsTokenLegit(token: string): Boolean;
  36. begin
  37. tbmusers.Lock;
  38. try
  39. if tbmusers.Locate('token',token,[]) then
  40. begin
  41. Result:=true;
  42. tbmusers.Edit;
  43. tbmusers.FieldByName('exppiry').AsDateTime:=Now+(/(*));//当前时间+15分钟
  44. tbmusers.Post;
  45. end
  46. else
  47. Result:=false;
  48. finally
  49. tbmusers.Unlock;
  50. end;
  51. end;
  52.  
  53. function TForm11.IsUserLegit(UserName, password: string; var customdata,
  54. token: string): Boolean;
  55. var
  56. conn:TkbmMWUNIDACConnection;
  57. qry:TUniQuery;
  58. begin
  59. Result := False;
  60. qry := TUniQuery.Create(nil);
  61. conn := TkbmMWUNIDACConnection(mwcpool.GetBestConnection(true, -, nil, ));
  62. try
  63. qry.Connection := conn.Database;
  64. qry.SQL.Add('select id from employees where name=:username and password=:password');
  65. qry.ParamByName('username').AsString := UserName;
  66. qry.ParamByName('password').AsString := password;
  67. qry.Open;
  68. if qry.Eof then
  69. begin
  70. Result := False;//用户名或密码错误
  71. end
  72. else
  73. begin
  74. //用户与密码正确,建立token并返回customdata.
  75. customdata := qry.Fields[].AsString;
  76. //建立简单的token
  77. token := customdata + IntToStr(GetTickCount);
  78. Result := True;
  79. end;
  80. finally
  81. FreeAndNil(qry);
  82. FreeAndNil(conn);
  83. end;
  84. end;
  85.  
  86. procedure TForm11.mwServerAuthenticate(Sender: TObject;
  87. ClientIdent: TkbmMWClientIdentity; var Perm: TkbmMWAccessPermissions;
  88. var AMessage: string);
  89. var
  90. userdata,
  91. token: string;
  92. begin // no access by default
  93. perm:=[];
  94. //check to see if user already logged in
  95. if clientident.token='' then
  96. begin
  97. memo1.lines.add(clientident.password);
  98. if IsUserLegit(clientident.username,clientident.password,userdata,token) then
  99. begin
  100. clientident.Data:=userdata;
  101. clientident.Token:=token;
  102. perm:=[mwapRead, mwapWrite, mwapDelete, mwapExecute];
  103. try
  104. tbmusers.Lock;
  105. tbmusers.Insert;
  106. tbmusers.FieldByName('id').AsString:=userdata;
  107. tbmusers.FieldByName('token').AsString:=token;
  108. tbmusers.FieldByName('expiry').AsDateTime:=now+(/(*));
  109. tbmusers.Post;
  110. finally
  111. tbmusers.Unlock;
  112. end;
  113. end
  114. else
  115. raise EkbmMWAuthException.Create(,'Username or Password Invalid');
  116. end
  117. else
  118. begin
  119. if IsTokenLegit(clientident.token) then
  120. perm:=[mwapRead, mwapWrite, mwapDelete, mwapExecute]
  121. else
  122. raise EkbmMWAuthException.Create(,'Timeout');
  123. end;
  124. clientident.Username:='';
  125. clientident.Password:='';
  126. end;
  127.  
  128. 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. 移动端自动化测试-Mac-IOS-Appium环境搭建

    第一步 安装JDK,本机如果带有1.7及以上版本的,则可忽略此安装步骤. 百度下载JDK,并配置环境变量 vim ~/.bash_profile 检查是否安装成功 java -version 第二步 ...

  2. oracle查询视图归属于哪个用户

    select OWNER from ALL_VIEWS where VIEW_NAME='视图名';

  3. 【Linux】bash shell学习

    Bash Shell Linux系统的合法shell都写入/etc/shells这个文件,默认使用的shell版本称为“Bourne Again Shell(简称bash)” 用户登录时系统会分配一个 ...

  4. Jmeter的使用简介及实例

    一.安装及配置环境1.安装   java环境   该软件需要java环境,安装jdk,在百度自行查找安装   环境变量配置:变量名JAVA_HOME 值:jdk的安装路径                ...

  5. Vue 项目骨架屏注入与实践

    作为与用户联系最为密切的前端开发者,用户体验是最值得关注的问题.关于页面loading状态的展示,主流的主要有loading图和进度条两种.除此之外,越来越多的APP采用了“骨架屏”的方式去展示未加载 ...

  6. 前端基础之html常用标签

    前言: 1.在B-S模式下,server服务端和客户端之间 使用http协议(规定 客户端应该怎么请求服务端,服务端应该怎么响应)通信: 2.传输过程 浏览器 向服务端发起 post/get请求 服务 ...

  7. ActiveMQ 处理不同类型的消息

    ActiveMQ 中的消息都继承自 org.apache.activemq.command.BaseCommand 类. broker 处理消息的调用栈如下: TransportConnection ...

  8. 把旧系统迁移到.Net Core 2.0 日记(9) -- T4 Template

    想着用T4 Template 自动生成代码,省了功夫. 发现T4 Template 挺笨的. 我开始这样写是会报错的  <#  var modualName = "CRM"  ...

  9. C# [IPA]IOS In App Purchase(内购)验证(asp.net 版本)

    之前没有做过IOS 内购服务器验证这块,所以找了不少参考资料,网上大多php和java版本,然后自己搞了一个C#版本,希望能给大家一些参考,下面步入正题 在客户端向苹果购买成功之后,我们需要进行二次验 ...

  10. JSP页面间的传值方法总结

    JSP 页面间传递参数是项目中经常需要的,这应该算是 web 基本功吧.试着将各种方式总结下来,需要时可以进行权衡利弊选择最合适的方式.下面来一起看看详细的介绍: 1. URL 链接后追加参数 ? 1 ...