mirror of
https://gitee.com/ThingsGateway/ThingsGateway.git
synced 2025-10-26 05:20:16 +08:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d930a9a8eb | ||
|
|
af381fce12 |
@@ -33,83 +33,6 @@ public class AdminOAuthHandler<TOptions>(
|
||||
) : OAuthHandler<TOptions>(options, logger, encoder)
|
||||
where TOptions : AdminOAuthOptions, new()
|
||||
{
|
||||
private async Task<LoginEvent> GetLogin()
|
||||
{
|
||||
var sysUser = await sysUserService.GetUserByIdAsync(RoleConst.SuperAdminId).ConfigureAwait(false);//获取用户信息
|
||||
|
||||
var appConfig = await configService.GetAppConfigAsync().ConfigureAwait(false);
|
||||
|
||||
|
||||
var expire = appConfig.LoginPolicy.VerificatExpireTime;
|
||||
|
||||
var loginEvent = new LoginEvent
|
||||
{
|
||||
Ip = appService.RemoteIpAddress,
|
||||
Device = appService.UserAgent?.Platform,
|
||||
Expire = expire,
|
||||
SysUser = sysUser,
|
||||
VerificatId = CommonUtils.GetSingleId()
|
||||
};
|
||||
|
||||
//获取verificat列表
|
||||
var tokenTimeout = loginEvent.DateTime.AddMinutes(loginEvent.Expire);
|
||||
//生成verificat信息
|
||||
var verificatInfo = new VerificatInfo
|
||||
{
|
||||
Device = loginEvent.Device,
|
||||
Expire = loginEvent.Expire,
|
||||
VerificatTimeout = tokenTimeout,
|
||||
Id = loginEvent.VerificatId,
|
||||
UserId = loginEvent.SysUser.Id,
|
||||
LoginIp = loginEvent.Ip,
|
||||
LoginTime = loginEvent.DateTime
|
||||
};
|
||||
|
||||
|
||||
//添加到verificat列表
|
||||
verificatInfoService.Add(verificatInfo);
|
||||
|
||||
return loginEvent;
|
||||
}
|
||||
/// <summary>
|
||||
/// 登录事件
|
||||
/// </summary>
|
||||
/// <param name="loginEvent"></param>
|
||||
/// <returns></returns>
|
||||
private async Task UpdateUser(LoginEvent loginEvent)
|
||||
{
|
||||
var sysUser = loginEvent.SysUser;
|
||||
|
||||
#region 登录/密码策略
|
||||
|
||||
var key = CacheConst.Cache_LoginErrorCount + sysUser.Account;//获取登录错误次数Key值
|
||||
App.CacheService.Remove(key);//移除登录错误次数
|
||||
|
||||
//获取用户verificat列表
|
||||
var userToken = verificatInfoService.GetOne(loginEvent.VerificatId);
|
||||
|
||||
#endregion 登录/密码策略
|
||||
|
||||
#region 重新赋值属性,设置本次登录信息为最新的信息
|
||||
|
||||
sysUser.LastLoginIp = sysUser.LatestLoginIp;
|
||||
sysUser.LastLoginTime = sysUser.LatestLoginTime;
|
||||
sysUser.LatestLoginIp = loginEvent.Ip;
|
||||
sysUser.LatestLoginTime = loginEvent.DateTime;
|
||||
|
||||
#endregion 重新赋值属性,设置本次登录信息为最新的信息
|
||||
|
||||
using var db = DbContext.Db.GetConnectionScopeWithAttr<SysUser>().CopyNew();
|
||||
//更新用户登录信息
|
||||
if (await db.Updateable(sysUser).UpdateColumns(it => new
|
||||
{
|
||||
it.LastLoginIp,
|
||||
it.LastLoginTime,
|
||||
it.LatestLoginIp,
|
||||
it.LatestLoginTime,
|
||||
}).ExecuteCommandAsync().ConfigureAwait(false) > 0)
|
||||
App.CacheService.HashAdd(CacheConst.Cache_SysUser, sysUser.Id.ToString(), sysUser);//更新Cache信息
|
||||
}
|
||||
|
||||
|
||||
static AdminOAuthHandler()
|
||||
@@ -152,7 +75,6 @@ public class AdminOAuthHandler<TOptions>(
|
||||
|
||||
}
|
||||
|
||||
|
||||
protected override async Task<AuthenticationTicket> CreateTicketAsync(
|
||||
ClaimsIdentity identity,
|
||||
AuthenticationProperties properties,
|
||||
@@ -160,14 +82,17 @@ public class AdminOAuthHandler<TOptions>(
|
||||
{
|
||||
properties.RedirectUri = Options.HomePath;
|
||||
properties.IsPersistent = true;
|
||||
var appConfig = await configService.GetAppConfigAsync().ConfigureAwait(false);
|
||||
|
||||
int expire = appConfig.LoginPolicy.VerificatExpireTime;
|
||||
if (!string.IsNullOrEmpty(tokens.ExpiresIn) && int.TryParse(tokens.ExpiresIn, out var result))
|
||||
{
|
||||
properties.ExpiresUtc = TimeProvider.System.GetUtcNow().AddSeconds(result);
|
||||
expire = (int)(result / 60.0);
|
||||
}
|
||||
var user = await HandleUserInfoAsync(tokens).ConfigureAwait(false);
|
||||
|
||||
var loginEvent = await GetLogin().ConfigureAwait(false);
|
||||
var loginEvent = await GetLogin(expire).ConfigureAwait(false);
|
||||
await UpdateUser(loginEvent).ConfigureAwait(false);
|
||||
identity.AddClaim(new Claim(ClaimConst.VerificatId, loginEvent.VerificatId.ToString()));
|
||||
identity.AddClaim(new Claim(ClaimConst.UserId, RoleConst.SuperAdminId.ToString()));
|
||||
@@ -222,29 +147,6 @@ public class AdminOAuthHandler<TOptions>(
|
||||
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
|
||||
}
|
||||
|
||||
/// <summary>刷新 Token 方法</summary>
|
||||
protected virtual async Task<OAuthTokenResponse> RefreshTokenAsync(OAuthTokenResponse oAuthToken)
|
||||
{
|
||||
var query = new Dictionary<string, string>
|
||||
{
|
||||
{ "refresh_token", oAuthToken.RefreshToken },
|
||||
{ "grant_type", "refresh_token" }
|
||||
};
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, QueryHelpers.AddQueryString(Options.TokenEndpoint, query));
|
||||
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
|
||||
var response = await Backchannel.SendAsync(request, Context.RequestAborted).ConfigureAwait(false);
|
||||
|
||||
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return OAuthTokenResponse.Success(JsonDocument.Parse(content));
|
||||
}
|
||||
|
||||
return OAuthTokenResponse.Failed(new OAuthTokenException($"OAuth token endpoint failure: {await Display(response).ConfigureAwait(false)}"));
|
||||
}
|
||||
|
||||
/// <summary>处理用户信息方法</summary>
|
||||
protected virtual async Task<JsonElement> HandleUserInfoAsync(OAuthTokenResponse tokens)
|
||||
@@ -283,6 +185,79 @@ public class AdminOAuthHandler<TOptions>(
|
||||
|
||||
return output.ToString();
|
||||
}
|
||||
|
||||
private async Task<LoginEvent> GetLogin(int expire)
|
||||
{
|
||||
var sysUser = await sysUserService.GetUserByIdAsync(RoleConst.SuperAdminId).ConfigureAwait(false);//获取用户信息
|
||||
|
||||
var loginEvent = new LoginEvent
|
||||
{
|
||||
Ip = appService.RemoteIpAddress,
|
||||
Device = appService.UserAgent?.Platform,
|
||||
Expire = expire,
|
||||
SysUser = sysUser,
|
||||
VerificatId = CommonUtils.GetSingleId()
|
||||
};
|
||||
|
||||
//获取verificat列表
|
||||
var tokenTimeout = loginEvent.DateTime.AddMinutes(loginEvent.Expire);
|
||||
//生成verificat信息
|
||||
var verificatInfo = new VerificatInfo
|
||||
{
|
||||
Device = loginEvent.Device,
|
||||
Expire = loginEvent.Expire,
|
||||
VerificatTimeout = tokenTimeout,
|
||||
Id = loginEvent.VerificatId,
|
||||
UserId = loginEvent.SysUser.Id,
|
||||
LoginIp = loginEvent.Ip,
|
||||
LoginTime = loginEvent.DateTime
|
||||
};
|
||||
|
||||
|
||||
//添加到verificat列表
|
||||
verificatInfoService.Add(verificatInfo);
|
||||
return loginEvent;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 登录事件
|
||||
/// </summary>
|
||||
/// <param name="loginEvent"></param>
|
||||
/// <returns></returns>
|
||||
private async Task UpdateUser(LoginEvent loginEvent)
|
||||
{
|
||||
var sysUser = loginEvent.SysUser;
|
||||
|
||||
#region 登录/密码策略
|
||||
|
||||
var key = CacheConst.Cache_LoginErrorCount + sysUser.Account;//获取登录错误次数Key值
|
||||
App.CacheService.Remove(key);//移除登录错误次数
|
||||
|
||||
//获取用户verificat列表
|
||||
var userToken = verificatInfoService.GetOne(loginEvent.VerificatId);
|
||||
|
||||
#endregion 登录/密码策略
|
||||
|
||||
#region 重新赋值属性,设置本次登录信息为最新的信息
|
||||
|
||||
sysUser.LastLoginIp = sysUser.LatestLoginIp;
|
||||
sysUser.LastLoginTime = sysUser.LatestLoginTime;
|
||||
sysUser.LatestLoginIp = loginEvent.Ip;
|
||||
sysUser.LatestLoginTime = loginEvent.DateTime;
|
||||
|
||||
#endregion 重新赋值属性,设置本次登录信息为最新的信息
|
||||
|
||||
using var db = DbContext.Db.GetConnectionScopeWithAttr<SysUser>().CopyNew();
|
||||
//更新用户登录信息
|
||||
if (await db.Updateable(sysUser).UpdateColumns(it => new
|
||||
{
|
||||
it.LastLoginIp,
|
||||
it.LastLoginTime,
|
||||
it.LatestLoginIp,
|
||||
it.LatestLoginTime,
|
||||
}).ExecuteCommandAsync().ConfigureAwait(false) > 0)
|
||||
App.CacheService.HashAdd(CacheConst.Cache_SysUser, sysUser.Id.ToString(), sysUser);//更新Cache信息
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>自定义 Token 异常</summary>
|
||||
|
||||
@@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Authentication.OAuth;
|
||||
using Microsoft.AspNetCore.WebUtilities;
|
||||
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ThingsGateway.Admin.Application;
|
||||
@@ -32,6 +33,42 @@ public class GiteeOAuthOptions : AdminOAuthOptions
|
||||
context.Response.Redirect(context.RedirectUri);
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
/// <summary>刷新 Token 方法</summary>
|
||||
protected virtual async Task<OAuthTokenResponse> RefreshTokenAsync(TicketReceivedContext ticketReceivedContext, string refreshToken)
|
||||
{
|
||||
var query = new Dictionary<string, string>
|
||||
{
|
||||
{ "refresh_token", refreshToken },
|
||||
{ "grant_type", "refresh_token" }
|
||||
};
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, QueryHelpers.AddQueryString(TokenEndpoint, query));
|
||||
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
|
||||
var response = await Backchannel.SendAsync(request, ticketReceivedContext.HttpContext.RequestAborted).ConfigureAwait(false);
|
||||
|
||||
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return OAuthTokenResponse.Success(JsonDocument.Parse(content));
|
||||
}
|
||||
|
||||
return OAuthTokenResponse.Failed(new OAuthTokenException($"OAuth token endpoint failure: {await Display(response).ConfigureAwait(false)}"));
|
||||
}
|
||||
|
||||
/// <summary>生成错误信息方法</summary>
|
||||
protected static async Task<string> Display(HttpResponseMessage response)
|
||||
{
|
||||
var output = new StringBuilder();
|
||||
output.Append($"Status: {response.StatusCode}; ");
|
||||
output.Append($"Headers: {response.Headers}; ");
|
||||
output.Append($"Body: {await response.Content.ReadAsStringAsync().ConfigureAwait(false)};");
|
||||
|
||||
return output.ToString();
|
||||
}
|
||||
|
||||
public override string GetName(JsonElement element)
|
||||
|
||||
@@ -39,7 +39,7 @@
|
||||
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
|
||||
<PackageReference Include="Mapster" Version="7.4.0" />
|
||||
<PackageReference Include="MiniProfiler.AspNetCore.Mvc" Version="4.5.4" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="8.1.3" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="8.1.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
<Project>
|
||||
|
||||
<PropertyGroup>
|
||||
<PluginVersion>10.7.20</PluginVersion>
|
||||
<ProPluginVersion>10.7.20</ProPluginVersion>
|
||||
<PluginVersion>10.7.21</PluginVersion>
|
||||
<ProPluginVersion>10.7.21</ProPluginVersion>
|
||||
<AuthenticationVersion>2.4.0</AuthenticationVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
|
||||
@@ -97,13 +97,15 @@ public class DDPTcpSessionClientChannel : TcpSessionClientChannel
|
||||
{
|
||||
if (message.Type == 0x01)
|
||||
{
|
||||
bool log = false;
|
||||
if (id != Id) log = true;
|
||||
//注册ID
|
||||
await ResetIdAsync(id).ConfigureAwait(false);
|
||||
|
||||
//发送成功
|
||||
await DDPAdapter.SendInputAsync(new DDPSend(ReadOnlyMemory<byte>.Empty, id, true, 0x81)).ConfigureAwait(false);
|
||||
|
||||
Logger?.Info(DefaultResource.Localizer["DtuConnected", Id]);
|
||||
if (log)
|
||||
Logger?.Info(DefaultResource.Localizer["DtuConnected", Id]);
|
||||
}
|
||||
else if (message.Type == 0x02)
|
||||
{
|
||||
|
||||
@@ -138,19 +138,30 @@ public class DDPUdpSessionChannel : UdpSessionChannel, IClientChannel, IDtuUdpSe
|
||||
{
|
||||
if (message.Type == 0x01)
|
||||
{
|
||||
bool log = false;
|
||||
|
||||
//注册ID
|
||||
if (!IdDict.TryAdd(endPoint, id))
|
||||
{
|
||||
IdDict[endPoint] = id;
|
||||
}
|
||||
else
|
||||
{
|
||||
log = true;
|
||||
}
|
||||
if (!EndPointDcit.TryAdd(id, endPoint))
|
||||
{
|
||||
EndPointDcit[id] = endPoint;
|
||||
}
|
||||
else
|
||||
{
|
||||
log = true;
|
||||
}
|
||||
|
||||
//发送成功
|
||||
await DDPAdapter.SendInputAsync(endPoint, new DDPSend(ReadOnlyMemory<byte>.Empty, id, false, 0x81)).ConfigureAwait(false);
|
||||
Logger?.Info(DefaultResource.Localizer["DtuConnected", id]);
|
||||
if(log)
|
||||
Logger?.Info(DefaultResource.Localizer["DtuConnected", id]);
|
||||
}
|
||||
else if (message.Type == 0x02)
|
||||
{
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
<Split Basis="20%" ShowBarHandle=false>
|
||||
<FirstPaneTemplate>
|
||||
<ContextMenuZone title="Right click operation">
|
||||
<TreeView TItem="PluginInfo" Items="PluginTreeViewItems" ClickToggleNode IsVirtualize="true" />
|
||||
<TreeView TItem="PluginInfo" Items="PluginTreeViewItems" ClickToggleNode IsVirtualize="true" OnTreeItemClick="OnClick" @ref=treeView />
|
||||
<ContextMenu>
|
||||
<ContextMenuItem Text=@Localizer["New"] OnClick="NewPluginRender"></ContextMenuItem>
|
||||
<ContextMenuItem Text=@Localizer["NewWinbox"] OnClick="NewPluginWinboxRender"></ContextMenuItem>
|
||||
@@ -35,4 +35,5 @@
|
||||
|
||||
@code {
|
||||
Tab tab;
|
||||
TreeView<PluginInfo> treeView;
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
|
||||
using Mapster;
|
||||
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
using ThingsGateway.Gateway.Application;
|
||||
using ThingsGateway.NewLife.Extension;
|
||||
|
||||
@@ -134,4 +136,27 @@ public partial class PluginDebugPage
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private TreeViewItem<PluginInfo> active;
|
||||
private async Task OnClick(TreeViewItem<PluginInfo> item)
|
||||
{
|
||||
if (active == item)
|
||||
{
|
||||
var pluginInfo = item.Value;
|
||||
if (pluginInfo.Children.Count == 0)
|
||||
{
|
||||
var debugRender = await GetDebugUIAsync(pluginInfo);
|
||||
if (debugRender != null)
|
||||
{
|
||||
tab.AddTab(new Dictionary<string, object?>
|
||||
{
|
||||
[nameof(TabItem.Text)] = pluginInfo.Name,
|
||||
[nameof(TabItem.IsActive)] = true,
|
||||
[nameof(TabItem.ChildContent)] = debugRender
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
active = item;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,14 @@
|
||||
<div class="login-form d-flex align-items-center flex-column">
|
||||
<img src=@($"{WebsiteConst.DefaultResourceUrl}images/defaultUser.svg") class="user-avatar" />
|
||||
<h5 class="mt-2 mb-12 ">@Localizer["Welcome"] 👋</h5>
|
||||
<UserLogin Model="loginModel" OnLogin="LoginAsync"></UserLogin>
|
||||
@if(WebsiteOption.Value.Demo)
|
||||
{
|
||||
<Button Class="btn-block mt-5" IsAsync OnClick=GiteeLogin>@Localizer["GiteeLogin"]</Button>
|
||||
}
|
||||
else
|
||||
{
|
||||
<UserLogin Model="loginModel" OnLogin="LoginAsync"></UserLogin>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -64,6 +64,14 @@ public partial class Login
|
||||
_versionString = $"v{VersionService.Version}";
|
||||
return base.OnInitializedAsync();
|
||||
}
|
||||
private void GiteeLogin()
|
||||
{
|
||||
var websiteOptions = App.GetOptions<WebsiteOptions>()!;
|
||||
if (websiteOptions.Demo)
|
||||
{
|
||||
NavigationManager.NavigateTo("/api/auth/oauth-login", forceLoad: true);
|
||||
}
|
||||
}
|
||||
[Inject]
|
||||
NavigationManager NavigationManager { get; set; }
|
||||
private async Task LoginAsync(EditContext context)
|
||||
|
||||
@@ -14,7 +14,8 @@
|
||||
"Remark1": "Edge Collection Gateway",
|
||||
"Remark2": "Data Aggregation, Multi-path Forwarding",
|
||||
"Remark3": "Edge Computing, Efficient Processing",
|
||||
"Welcome": "Welcome"
|
||||
"Welcome": "Welcome",
|
||||
"GiteeLogin": "Gitee OAuth"
|
||||
},
|
||||
|
||||
"ThingsGateway.Server.MainLayout": {
|
||||
|
||||
@@ -15,7 +15,8 @@
|
||||
"Remark1": "边缘采集网关",
|
||||
"Remark2": "数据集中,多路转发",
|
||||
"Remark3": "边缘计算,高效处理",
|
||||
"Welcome": "欢迎使用"
|
||||
"Welcome": "欢迎使用",
|
||||
"GiteeLogin": "Gitee授权登录"
|
||||
},
|
||||
|
||||
"ThingsGateway.Server.MainLayout": {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<Project>
|
||||
<PropertyGroup>
|
||||
<Version>10.7.20</Version>
|
||||
<Version>10.7.21</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user