fix(#I11GTJ): 前台工程增加访问日志

#Comment
comment #I11GTJ

#Issue
close https://gitee.com/LongbowEnterprise/dashboard/issues?id=I11GTJ
This commit is contained in:
Argo Zhang 2019-09-01 20:06:38 +08:00
parent 44a92f818d
commit 55273e792d
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
9 changed files with 120 additions and 3 deletions

View File

@ -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;
}
}
}

View File

@ -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>

View File

@ -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;
}
}
}
}

View File

@ -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);
}
}
}
}

View File

@ -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);
}
}
}

View File

@ -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" />

View File

@ -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 =>
{

View File

@ -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" />

View File

@ -13,7 +13,6 @@ namespace Bootstrap.DataAccess
/// </summary>
public static class TraceHelper
{
/// <summary>
/// 保存访问历史记录
/// </summary>