feat: 完成登录日志记录功能
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.AspNetCore.Extensions
|
||||
{
|
||||
public static class HttpContextExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取客户端IP
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetClientIp(this HttpContext context)
|
||||
{
|
||||
if (context == null) return "";
|
||||
var result = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
|
||||
if (string.IsNullOrEmpty(result))
|
||||
{
|
||||
result = context.Connection.RemoteIpAddress?.ToString();
|
||||
}
|
||||
if (string.IsNullOrEmpty(result) || result.Contains("::1"))
|
||||
result = "127.0.0.1";
|
||||
|
||||
result = result.Replace("::ffff:", "127.0.0.1");
|
||||
|
||||
//Ip规则效验
|
||||
var regResult = Regex.IsMatch(result, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
|
||||
|
||||
result = regResult ? result : "127.0.0.1";
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取浏览器标识
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetUserAgent(this HttpContext context)
|
||||
{
|
||||
return context.Request.Headers["User-Agent"];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,6 +74,26 @@
|
||||
User服务抽象
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Yi.RBAC.Application.Contracts.Logs.Dtos.LoginLog.LoginLogGetListOutputDto.LoginIp">
|
||||
<summary>
|
||||
登录Ip
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Yi.RBAC.Application.Contracts.Logs.Dtos.LoginLog.LoginLogGetListOutputDto.Browser">
|
||||
<summary>
|
||||
浏览器
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Yi.RBAC.Application.Contracts.Logs.Dtos.LoginLog.LoginLogGetListOutputDto.Os">
|
||||
<summary>
|
||||
操作系统
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Yi.RBAC.Application.Contracts.Logs.Dtos.LoginLog.LoginLogGetListOutputDto.LogMsg">
|
||||
<summary>
|
||||
登录信息
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:Yi.RBAC.Application.Contracts.Setting.Dtos.ConfigCreateInputVo">
|
||||
<summary>
|
||||
Config输入创建对象
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Ddd.Dtos;
|
||||
|
||||
namespace Yi.RBAC.Application.Contracts.Logs.Dtos.LoginLog
|
||||
{
|
||||
public class LoginLogGetListInputVo: PagedAllResultRequestDto
|
||||
{
|
||||
public string? LoginUser { get; set; }
|
||||
|
||||
public string? LoginIp { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Ddd.Dtos;
|
||||
|
||||
namespace Yi.RBAC.Application.Contracts.Logs.Dtos.LoginLog
|
||||
{
|
||||
public class LoginLogGetListOutputDto:EntityDto<long>
|
||||
{
|
||||
public DateTime CreationTime { get; }
|
||||
|
||||
|
||||
public string? LoginUser { get; set; }
|
||||
|
||||
public string? LoginLocation { get; set; }
|
||||
/// <summary>
|
||||
/// 登录Ip
|
||||
///</summary>
|
||||
public string? LoginIp { get; set; }
|
||||
/// <summary>
|
||||
/// 浏览器
|
||||
///</summary>
|
||||
public string? Browser { get; set; }
|
||||
/// <summary>
|
||||
/// 操作系统
|
||||
///</summary>
|
||||
public string? Os { get; set; }
|
||||
/// <summary>
|
||||
/// 登录信息
|
||||
///</summary>
|
||||
public string? LogMsg { get; set; }
|
||||
|
||||
public long? CreatorId { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using NET.AutoWebApi.Setting;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Ddd.Dtos;
|
||||
using Yi.Framework.Ddd.Services;
|
||||
using Yi.RBAC.Application.Contracts.Logs.Dtos.LoginLog;
|
||||
using Yi.RBAC.Application.Contracts.Setting.Dtos;
|
||||
using Yi.RBAC.Domain.Logs.Entities;
|
||||
|
||||
namespace Yi.RBAC.Application.Logs
|
||||
{
|
||||
[AppService]
|
||||
public class LoginLogService : CrudAppService<LoginLogEntity, LoginLogGetListOutputDto, long, LoginLogGetListInputVo>, IAutoApiService
|
||||
{
|
||||
public override async Task<PagedResultDto<LoginLogGetListOutputDto>> GetListAsync(LoginLogGetListInputVo input)
|
||||
{
|
||||
var entity = await MapToEntityAsync(input);
|
||||
|
||||
RefAsync<int> total = 0;
|
||||
|
||||
var entities = await _DbQueryable.WhereIF(!string.IsNullOrEmpty(input.LoginIp), x => x.LoginIp.Contains(input.LoginIp!))
|
||||
.WhereIF(!string.IsNullOrEmpty(input.LoginUser), x => x.LoginUser!.Contains(input.LoginUser!))
|
||||
.WhereIF(input.StartTime is not null && input.EndTime is not null, x => x.CreationTime >= input.StartTime && x.CreationTime <= input.EndTime)
|
||||
.ToPageListAsync(input.PageNum, input.PageSize, total);
|
||||
return new PagedResultDto<LoginLogGetListOutputDto>(total, await MapToGetListOutputDtosAsync(entities));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,5 +11,6 @@ namespace Yi.RBAC.Domain.Shared.Identity.Etos
|
||||
public long UserId { get; set; }
|
||||
public string UserName { get; set; }
|
||||
|
||||
public string LogMsg { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -711,6 +711,36 @@
|
||||
<param name="postIds"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="P:Yi.RBAC.Domain.Logs.Entities.LoginLogEntity.LoginUser">
|
||||
<summary>
|
||||
登录用户
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Yi.RBAC.Domain.Logs.Entities.LoginLogEntity.LoginLocation">
|
||||
<summary>
|
||||
登录地点
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Yi.RBAC.Domain.Logs.Entities.LoginLogEntity.LoginIp">
|
||||
<summary>
|
||||
登录Ip
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Yi.RBAC.Domain.Logs.Entities.LoginLogEntity.Browser">
|
||||
<summary>
|
||||
浏览器
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Yi.RBAC.Domain.Logs.Entities.LoginLogEntity.Os">
|
||||
<summary>
|
||||
操作系统
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Yi.RBAC.Domain.Logs.Entities.LoginLogEntity.LogMsg">
|
||||
<summary>
|
||||
登录信息
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:Yi.RBAC.Domain.Setting.Entities.ConfigEntity">
|
||||
<summary>
|
||||
配置表
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
using Cike.EventBus.EventHandlerAbstracts;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.RBAC.Domain.Shared.Identity.Etos;
|
||||
|
||||
namespace Yi.RBAC.Domain.Identity.Event
|
||||
{
|
||||
public class LoginEventHandler : IDistributedEventHandler<LoginEventArgs>
|
||||
{
|
||||
public Task HandlerAsync(LoginEventArgs eventData)
|
||||
{
|
||||
Console.WriteLine(eventData.UserName+"登录系统");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Data.Auditing;
|
||||
using Yi.Framework.Ddd.Entities;
|
||||
|
||||
namespace Yi.RBAC.Domain.Logs.Entities
|
||||
{
|
||||
[SugarTable("LoginLog")]
|
||||
public class LoginLogEntity : IEntity<long>, ICreationAuditedObject
|
||||
{
|
||||
[SugarColumn(ColumnName = "Id", IsPrimaryKey = true)]
|
||||
public long Id { get; set; }
|
||||
public DateTime CreationTime { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 登录用户
|
||||
///</summary>
|
||||
[SugarColumn(ColumnName = "LoginUser")]
|
||||
public string? LoginUser { get; set; }
|
||||
/// <summary>
|
||||
/// 登录地点
|
||||
///</summary>
|
||||
[SugarColumn(ColumnName = "LoginLocation")]
|
||||
public string? LoginLocation { get; set; }
|
||||
/// <summary>
|
||||
/// 登录Ip
|
||||
///</summary>
|
||||
[SugarColumn(ColumnName = "LoginIp")]
|
||||
public string? LoginIp { get; set; }
|
||||
/// <summary>
|
||||
/// 浏览器
|
||||
///</summary>
|
||||
[SugarColumn(ColumnName = "Browser")]
|
||||
public string? Browser { get; set; }
|
||||
/// <summary>
|
||||
/// 操作系统
|
||||
///</summary>
|
||||
[SugarColumn(ColumnName = "Os")]
|
||||
public string? Os { get; set; }
|
||||
/// <summary>
|
||||
/// 登录信息
|
||||
///</summary>
|
||||
[SugarColumn(ColumnName = "LogMsg")]
|
||||
public string? LogMsg { get; set; }
|
||||
|
||||
public long? CreatorId { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using Cike.EventBus.EventHandlerAbstracts;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Ddd.Repositories;
|
||||
using Yi.RBAC.Domain.Logs.Entities;
|
||||
using Yi.RBAC.Domain.Shared.Identity.Etos;
|
||||
|
||||
namespace Yi.RBAC.Domain.Logs.Event
|
||||
{
|
||||
public class LoginEventHandler : IDistributedEventHandler<LoginEventArgs>
|
||||
{
|
||||
private readonly IRepository<LoginLogEntity> _loginLogRepository;
|
||||
private readonly HttpContext _httpContext;
|
||||
public LoginEventHandler(IRepository<LoginLogEntity> loginLogRepository, IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_loginLogRepository = loginLogRepository;
|
||||
_httpContext = httpContextAccessor.HttpContext;
|
||||
}
|
||||
public Task HandlerAsync(LoginEventArgs eventData)
|
||||
{
|
||||
var loginLogEntity = new LoginLogEntity();
|
||||
loginLogEntity.Id = SnowflakeHelper.NextId;
|
||||
loginLogEntity.LogMsg = eventData.LogMsg;
|
||||
loginLogEntity.LoginUser = eventData.UserName;
|
||||
|
||||
_loginLogRepository.InsertAsync(loginLogEntity);
|
||||
Console.WriteLine(eventData.UserName + "登录系统");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user