From 992349274d67660b9bdd78aaad464c8463d3b9cb Mon Sep 17 00:00:00 2001 From: Candy Date: Mon, 31 Aug 2020 09:37:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=8E=A5=E5=8F=A3=E5=AE=89?= =?UTF-8?q?=E5=85=A8=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/Cdy.Tag.Consume/IRuntimeSecurity.cs | 5 +++++ Proxy/DBGrpcApi.Client/Client.cs | 16 +++++++++++++--- Proxy/DBGrpcApi.Client/Protos/Security.proto | 3 +++ Proxy/DBGrpcApi/Protos/Security.proto | 3 +++ Proxy/DBGrpcApi/Services/SecurityService.cs | 9 ++++++++- .../Controllers/LoginController.cs | 19 ++++++++++++++----- Proxy/DbInRunWebApi/Model/Requestbase.cs | 13 ++++++++++++- .../DBRuntime.Real/Security/SecurityRunner.cs | 19 ++++++++++++++++--- 8 files changed, 74 insertions(+), 13 deletions(-) diff --git a/Common/Cdy.Tag.Consume/IRuntimeSecurity.cs b/Common/Cdy.Tag.Consume/IRuntimeSecurity.cs index a3f6d99..001c2ed 100644 --- a/Common/Cdy.Tag.Consume/IRuntimeSecurity.cs +++ b/Common/Cdy.Tag.Consume/IRuntimeSecurity.cs @@ -33,6 +33,11 @@ namespace Cdy.Tag #region ... Properties ... + /// + /// + /// + int TimeOut { get; } + #endregion ...Properties... #region ... Methods ... diff --git a/Proxy/DBGrpcApi.Client/Client.cs b/Proxy/DBGrpcApi.Client/Client.cs index e6cfdaf..4abf1e7 100644 --- a/Proxy/DBGrpcApi.Client/Client.cs +++ b/Proxy/DBGrpcApi.Client/Client.cs @@ -70,6 +70,15 @@ namespace DBGrpcApi } } + /// + /// 超时时间 + /// + public int TimeOut { get; set; } + + /// + /// + /// + public DateTime LoginTime { get; set; } #endregion ...Properties... @@ -95,8 +104,6 @@ namespace DBGrpcApi mSecurityClient = new Security.SecurityClient(grpcChannel); - - } catch (Exception ex) { @@ -118,7 +125,10 @@ namespace DBGrpcApi { try { - mLoginId = mSecurityClient.Login(new LoginRequest() { Name = username, Password = password }).Token; + var re = mSecurityClient.Login(new LoginRequest() { Name = username, Password = password }); + TimeOut = re.Timeout; + LoginTime = DateTime.FromBinary(re.Time).ToLocalTime(); + mLoginId = re.Token; } catch { diff --git a/Proxy/DBGrpcApi.Client/Protos/Security.proto b/Proxy/DBGrpcApi.Client/Protos/Security.proto index 0e23f3f..175a632 100644 --- a/Proxy/DBGrpcApi.Client/Protos/Security.proto +++ b/Proxy/DBGrpcApi.Client/Protos/Security.proto @@ -23,10 +23,13 @@ message LoginRequest { // The response message containing the greetings. message LoginReply { string Token = 1; + int64 Time=2; + int32 Timeout=3; } message HartRequest { string Token = 1; + int64 Time=2; } diff --git a/Proxy/DBGrpcApi/Protos/Security.proto b/Proxy/DBGrpcApi/Protos/Security.proto index 0e23f3f..175a632 100644 --- a/Proxy/DBGrpcApi/Protos/Security.proto +++ b/Proxy/DBGrpcApi/Protos/Security.proto @@ -23,10 +23,13 @@ message LoginRequest { // The response message containing the greetings. message LoginReply { string Token = 1; + int64 Time=2; + int32 Timeout=3; } message HartRequest { string Token = 1; + int64 Time=2; } diff --git a/Proxy/DBGrpcApi/Services/SecurityService.cs b/Proxy/DBGrpcApi/Services/SecurityService.cs index 8075ce9..1f592f6 100644 --- a/Proxy/DBGrpcApi/Services/SecurityService.cs +++ b/Proxy/DBGrpcApi/Services/SecurityService.cs @@ -24,7 +24,7 @@ namespace DBGrpcApi public override Task Login(LoginRequest request, ServerCallContext context) { string Token = Cdy.Tag.ServiceLocator.Locator.Resolve().Login(request.Name, request.Password); - return Task.FromResult(new LoginReply() { Token = Token }); + return Task.FromResult(new LoginReply() { Token = Token, Time = DateTime.UtcNow.ToBinary(), Timeout = Cdy.Tag.ServiceLocator.Locator.Resolve().TimeOut }); } @@ -49,6 +49,13 @@ namespace DBGrpcApi /// public override Task Hart(HartRequest request, ServerCallContext context) { + + var dt = DateTime.FromBinary(request.Time); + if ((DateTime.UtcNow - dt).TotalSeconds > Cdy.Tag.ServiceLocator.Locator.Resolve().TimeOut) + { + return Task.FromResult(new HartReply() { Result = false }); + } + Cdy.Tag.ServiceLocator.Locator.Resolve().FreshUserId(request.Token); return Task.FromResult(new HartReply() { Result = true }); } diff --git a/Proxy/DbInRunWebApi/Controllers/LoginController.cs b/Proxy/DbInRunWebApi/Controllers/LoginController.cs index 5b11816..1689209 100644 --- a/Proxy/DbInRunWebApi/Controllers/LoginController.cs +++ b/Proxy/DbInRunWebApi/Controllers/LoginController.cs @@ -16,9 +16,9 @@ namespace DbInRunWebApi.Controllers [HttpPost("TryLogin")] public LoginResponse Login([FromBody] LoginUser user) { - string Token = Cdy.Tag.ServiceLocator.Locator.Resolve().Login(user.UserName, user.Password); - //return null; - return new LoginResponse() { Token = Token,Result = !string.IsNullOrEmpty(Token), Time=DateTime.Now }; + var service = Cdy.Tag.ServiceLocator.Locator.Resolve(); + string Token = service.Login(user.UserName, user.Password); + return new LoginResponse() { Token = Token, Result = !string.IsNullOrEmpty(Token), LoginTime = DateTime.UtcNow.ToBinary(),TimeOut = service.TimeOut }; } /// @@ -29,7 +29,17 @@ namespace DbInRunWebApi.Controllers [HttpPost("Hart")] public bool Hart([FromBody] Requestbase token) { - // return true; + if (string.IsNullOrEmpty(token.Time)) + { + return false; + } + long ltmp = long.Parse(token.Time); + + if ((DateTime.UtcNow - DateTime.FromBinary(ltmp)).TotalSeconds > Cdy.Tag.ServiceLocator.Locator.Resolve().TimeOut) + { + return false; + } + return Cdy.Tag.ServiceLocator.Locator.Resolve().FreshUserId(token.Token); } @@ -41,7 +51,6 @@ namespace DbInRunWebApi.Controllers [HttpPost("Logout")] public bool Logout([FromBody] Requestbase token) { - //return true; return Cdy.Tag.ServiceLocator.Locator.Resolve().Logout(token.Token); } diff --git a/Proxy/DbInRunWebApi/Model/Requestbase.cs b/Proxy/DbInRunWebApi/Model/Requestbase.cs index 4abefd2..586228d 100644 --- a/Proxy/DbInRunWebApi/Model/Requestbase.cs +++ b/Proxy/DbInRunWebApi/Model/Requestbase.cs @@ -53,10 +53,16 @@ namespace DbInRunWebApi.Model #endregion ...Constructor... #region ... Properties ... + /// /// /// - public DateTime Time { get; set; } + public long LoginTime { get; set; } + + /// + /// 超时时间 + /// + public long TimeOut { get; set; } /// /// 结果 @@ -101,6 +107,11 @@ namespace DbInRunWebApi.Model /// public string Token { get; set; } + /// + /// + /// + public string Time { get; set; } + #endregion ...Properties... #region ... Methods ... diff --git a/RunTime/DBRuntime.Real/Security/SecurityRunner.cs b/RunTime/DBRuntime.Real/Security/SecurityRunner.cs index 9181e58..d1ae4b3 100644 --- a/RunTime/DBRuntime.Real/Security/SecurityRunner.cs +++ b/RunTime/DBRuntime.Real/Security/SecurityRunner.cs @@ -31,9 +31,10 @@ namespace Cdy.Tag private Dictionary mLastLogin2 = new Dictionary(); + private Dictionary mUseIdMap2 = new Dictionary(); - public const int Timeout = 10; + public int mTimeout = 10; private bool mIsClosed = false; @@ -67,9 +68,22 @@ namespace Cdy.Tag } } + /// + /// + /// + public int TimeOut + { + get + { + return mTimeout; + } + } + + #endregion ...Properties... #region ... Methods ... + /// /// /// @@ -86,7 +100,6 @@ namespace Cdy.Tag public void Stop() { mIsClosed = true; - } /// @@ -103,7 +116,7 @@ namespace Cdy.Tag { foreach (var vv in mLastLogin) { - if ((dt - vv.Value).TotalMinutes >= Timeout) + if ((dt - vv.Value).TotalMinutes >= mTimeout) { ltmp.Add(vv.Key); } -- GitLab