fix(#I11GTJ): 前台工程增加访问日志
#Comment comment #I11GTJ #Issue close https://gitee.com/LongbowEnterprise/dashboard/issues?id=I11GTJ
This commit is contained in:
parent
44a92f818d
commit
55273e792d
|
@ -1,5 +1,6 @@
|
|||
using Bootstrap.Admin.Query;
|
||||
using Bootstrap.DataAccess;
|
||||
using Longbow.Web;
|
||||
using Longbow.Web.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
|
@ -22,5 +23,17 @@ namespace Bootstrap.Admin.Controllers.Api
|
|||
{
|
||||
return value.RetrieveData();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在线用户访问保存方法,前台系统调用
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public bool Post([FromBody]OnlineUser user)
|
||||
{
|
||||
TraceHelper.Save(HttpContext, user);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
<ItemGroup>
|
||||
<PackageReference Include="Longbow.Data" Version="2.3.6" />
|
||||
<PackageReference Include="Longbow.Logging" Version="2.2.12" />
|
||||
<PackageReference Include="Longbow.Web" Version="2.2.16" />
|
||||
<PackageReference Include="Bootstrap.Security.Mvc" Version="2.2.13" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
using Bootstrap.Security.DataAccess;
|
||||
using Longbow.Cache;
|
||||
using Longbow.Data;
|
||||
using Longbow.Web;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Bootstrap.Client.DataAccess
|
||||
|
@ -106,5 +108,21 @@ namespace Bootstrap.Client.DataAccess
|
|||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string RetrieveNotisUrl() => DbContextManager.Create<Dict>().RetrieveNotisUrl();
|
||||
|
||||
/// <summary>
|
||||
/// 配置 IP 地理位置查询配置项 注入方法调用此方法
|
||||
/// </summary>
|
||||
/// <param name="op"></param>
|
||||
public static void ConfigIPLocator(IPLocatorOption op)
|
||||
{
|
||||
var name = RetrieveLocaleIPSvr();
|
||||
if (!string.IsNullOrEmpty(name) && !name.Equals("None", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var url = RetrieveLocaleIPSvrUrl(name);
|
||||
op.Locator = string.IsNullOrEmpty(url) ? null : DefaultIPLocatorProvider.CreateLocator(name);
|
||||
op.Url = string.IsNullOrEmpty(url) ? string.Empty : $"{url}{op.IP}";
|
||||
if (int.TryParse(RetrieveLocaleIPSvrCachePeriod(), out var period) && period > 0) op.Period = period * 60 * 1000;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
using Longbow.Configuration;
|
||||
using Longbow.Web;
|
||||
using Microsoft.AspNetCore;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace Bootstrap.Client.DataAccess
|
||||
{
|
||||
/// <summary>
|
||||
/// 访问网页跟踪帮助类
|
||||
/// </summary>
|
||||
public static class TraceHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 保存访问历史记录
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="user"></param>
|
||||
public static void Save(HttpContext context, OnlineUser user)
|
||||
{
|
||||
if (context.User.Identity.IsAuthenticated)
|
||||
{
|
||||
var client = context.RequestServices.GetRequiredService<TraceHttpClient>();
|
||||
client.Post(context, user);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
using Longbow.Configuration;
|
||||
using Longbow.Web;
|
||||
using Microsoft.AspNetCore;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace Bootstrap.Client.DataAccess
|
||||
{
|
||||
/// <summary>
|
||||
/// Bootstrap TraceHttpClient 操作类
|
||||
/// </summary>
|
||||
public class TraceHttpClient
|
||||
{
|
||||
HttpClient _client;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="client"></param>
|
||||
/// <param name="httpContextAccessor"></param>
|
||||
public TraceHttpClient(HttpClient client, IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
client.Timeout = TimeSpan.FromSeconds(10);
|
||||
client.DefaultRequestHeaders.Connection.Add("keep-alive");
|
||||
|
||||
// set auth
|
||||
var cookieValues = httpContextAccessor.HttpContext.Request.Cookies.Select(cookie => $"{cookie.Key}={cookie.Value}");
|
||||
client.DefaultRequestHeaders.Add("Cookie", cookieValues);
|
||||
|
||||
var authHost = ConfigurationManager.Get<BootstrapAdminAuthenticationOptions>().AuthHost.TrimEnd(new char[] { '/' });
|
||||
var url = $"{authHost}/api/Traces";
|
||||
client.BaseAddress = new Uri(url);
|
||||
|
||||
_client = client;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 提交数据到后台访问网页接口
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="user"></param>
|
||||
public async void Post(HttpContext context, OnlineUser user)
|
||||
{
|
||||
// 调用 后台跟踪接口
|
||||
// http://localhost:50852/api/Traces
|
||||
user.RequestUrl = context.Request.AbsoluteUrl();
|
||||
|
||||
await _client.PostAsJsonAsync("", user, context.RequestAborted);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -9,7 +9,6 @@
|
|||
<PackageReference Include="Bootstrap.Security.Mvc" Version="2.2.13" />
|
||||
<PackageReference Include="Longbow.Configuration" Version="2.2.7" />
|
||||
<PackageReference Include="Longbow.Logging" Version="2.2.12" />
|
||||
<PackageReference Include="Longbow.Web" Version="2.2.15" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.App" />
|
||||
<PackageReference Include="Microsoft.Data.Sqlite" Version="2.2.6" />
|
||||
<PackageReference Include="Longbow.Cache" Version="2.2.14" />
|
||||
|
|
|
@ -50,6 +50,10 @@ namespace Bootstrap.Client
|
|||
services.AddConfigurationManager();
|
||||
services.AddCacheManager();
|
||||
services.AddDbAdapter();
|
||||
services.AddHttpClient("BootstrapAdmin", client => client.DefaultRequestHeaders.Connection.Add("keep-alive"));
|
||||
services.AddIPLocator(DictHelper.ConfigIPLocator);
|
||||
services.AddOnlineUsers();
|
||||
services.AddHttpClient<TraceHttpClient>();
|
||||
services.AddSignalR().AddJsonProtocalDefault();
|
||||
services.AddResponseCompression();
|
||||
services.AddBootstrapAdminAuthentication();
|
||||
|
@ -91,6 +95,7 @@ namespace Bootstrap.Client
|
|||
app.UseCookiePolicy();
|
||||
app.UseBootstrapAdminAuthentication(RoleHelper.RetrievesByUserName, RoleHelper.RetrievesByUrl, AppHelper.RetrievesByUserName);
|
||||
app.UseCacheManager();
|
||||
app.UseOnlineUsers(callback: TraceHelper.Save);
|
||||
app.UseSignalR(routes => { routes.MapHub<SignalRHub>("/NotiHub"); });
|
||||
app.UseMvc(routes =>
|
||||
{
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
<PackageReference Include="Longbow.Logging" Version="2.2.12" />
|
||||
<PackageReference Include="Longbow.PetaPoco" Version="1.0.2" />
|
||||
<PackageReference Include="Longbow.Security.Cryptography" Version="1.3.0" />
|
||||
<PackageReference Include="Longbow.Web" Version="2.2.15" />
|
||||
<PackageReference Include="Longbow.Web" Version="2.2.16" />
|
||||
<PackageReference Include="Longbow.Cache" Version="2.2.14" />
|
||||
<PackageReference Include="Microsoft.Data.Sqlite" Version="2.2.6" />
|
||||
<PackageReference Include="PetaPoco.Extensions" Version="1.0.9" />
|
||||
|
|
|
@ -13,7 +13,6 @@ namespace Bootstrap.DataAccess
|
|||
/// </summary>
|
||||
public static class TraceHelper
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 保存访问历史记录
|
||||
/// </summary>
|
||||
|
|
Loading…
Reference in New Issue