diff --git a/MiddleWare/TokenValidationMiddleware.cs b/MiddleWare/TokenValidationMiddleware.cs new file mode 100644 index 0000000..c25a8c0 --- /dev/null +++ b/MiddleWare/TokenValidationMiddleware.cs @@ -0,0 +1,65 @@ +using LY.App.Common.Redis; +using StackExchange.Redis; +using System.IdentityModel.Tokens.Jwt; + +namespace LY.App.MiddleWare +{ + public class TokenValidationMiddleware : IMiddleware + { + private RedisService _redis; + public TokenValidationMiddleware(RedisService redisHelper) + { + _redis = redisHelper; + } + public async Task InvokeAsync(HttpContext context, RequestDelegate next) + { + // 排除不需要Token验证的API + if (IsExcludedPath(context.Request.Path)) + { + await next(context); + return; + } + // 获取Token + var token = context.Request.Headers["Authorization"].ToString().Replace("Bearer ", ""); + if (!string.IsNullOrEmpty(token)) + { + // 验证Token是否有效 + var handler = new JwtSecurityTokenHandler(); + var jsonToken = handler.ReadToken(token) as JwtSecurityToken; + if (jsonToken != null) + { + // 从Token中获取数据 + var username = jsonToken.Claims.FirstOrDefault(claim => claim.Type == "sub")?.Value; + if (username != null) + { + if (await _redis.ExistsAsync(RedisKeyList.TokenUser(username))) + { + // Token和数据验证通过,继续处理请求 + await next(context); + return; + } + } + } + } + + else + { + // Token不存在,返回未授权 + context.Response.StatusCode = 401; + await context.Response.WriteAsync("Unauthorized: Invalid Token"); + } + } + /// + /// 这是放不需要过滤的api地址 + /// + /// + /// + private bool IsExcludedPath(PathString path) + { + // 根据实际情况定义不需要Token验证的API路径 + string[] arry = { "login" }; + return arry.Any(s => path.Value.Contains(s)); + // return path.StartsWithSegments("/api/public"); + } + } +} diff --git a/Model/LoginModel.cs b/Model/LoginModel.cs index fb782c3..c946d26 100644 --- a/Model/LoginModel.cs +++ b/Model/LoginModel.cs @@ -4,7 +4,5 @@ { public string username { get; set; } public string password { get; set; } - public string type { get; set; } - public string vertifyCode { get; set; } } } diff --git a/Model/PositionInfo.cs b/Model/PositionInfo.cs index 5a50e9b..e1848d2 100644 --- a/Model/PositionInfo.cs +++ b/Model/PositionInfo.cs @@ -6,7 +6,7 @@ namespace LY.App.Model /// 位置信息 /// [SugarTable("ly_position")] - public class PositionInfo: MultPolygonEntity + public class PositionInfo : MultPolygonEntity { /// /// 名称 @@ -30,43 +30,11 @@ namespace LY.App.Model /// [SugarColumn(Length = 31, IsNullable = true, ColumnDescription = "联系人电话")] public string ContactTel { get; set; } - /// - /// 图片文件名 + /// 图片 /// - [SugarColumn(IsNullable = true, ColumnDescription = "图片地址")] - public string ImageName { get; set; } - - /// - /// 图片地址 - /// - [SugarColumn(IsIgnore = true)] - public string ImageUrl { get; set; } - - /// - /// 图片缩略图地址 - /// - [SugarColumn(IsIgnore = true)] - public string ImageBriefUrl { get; set; } - - /// - /// 启用时间 - /// - [SugarColumn(IsNullable = true, ColumnDescription = "启用时间")] - public DateTime? EnableTime { get; set; } - - /// - /// 是否启用 - /// - [SugarColumn(ColumnDescription = "是否启用")] - public bool Enabled { get; set; } - - /// - /// 状态 - /// - [SugarColumn(IsNullable = true, ColumnDescription = "状态")] - public string Status { get; set; } = "离线"; - + [SugarColumn(IsNullable = true, ColumnDescription = "图片地址", ColumnName = "img")] + public string Img { get; set; } /// /// 备注 /// @@ -78,7 +46,6 @@ namespace LY.App.Model /// public class AddPosition { - /// /// 名称 /// @@ -89,21 +56,9 @@ namespace LY.App.Model /// public string RegionJson { get; set; } - - /// - /// 经度 - /// - public double Lon { get; set; } - - /// - /// 纬度 - /// - public double Lat { get; set; } - /// /// 地址 /// - public string Address { get; set; } /// @@ -116,24 +71,9 @@ namespace LY.App.Model public string ContactTel { get; set; } /// - /// 图片文件名 + /// 图片 /// - public string ImageName { get; set; } - - /// - /// 启用时间 - /// - public DateTime? EnableTime { get; set; } - - /// - /// 是否启用 - /// - public bool Enabled { get; set; } - - /// - /// 状态 - /// - public string Status { get; set; } = "离线"; + public string Img { get; set; } /// /// 备注 /// @@ -150,15 +90,4 @@ namespace LY.App.Model /// public long Id { get; set; } } - /// - /// 阵地下所有区域 - /// - public class GeoRegion - { - /// - /// 识别区 - /// - public string Region { get; set; } - - } } diff --git a/Program.cs b/Program.cs index f720b88..6228714 100644 --- a/Program.cs +++ b/Program.cs @@ -43,6 +43,7 @@ string redisConnection = builder.Configuration.GetValue("Redis:Connectio // ע RedisService builder.Services.AddSingleton(new RedisService(redisConnection)); +//builder.Services.AddTransient(); ////עSignalR builder.Services.AddSignalR(); builder.Services.AddHttpClient(); @@ -77,7 +78,7 @@ builder.Services.AddTransient(sp => }; //ݿͱִһ //db.DbMaintenance.CreateDatabase(); - // db.CodeFirst.SetStringDefaultLength(2000).InitTables(typeof(LogEntity)); + //db.CodeFirst.SetStringDefaultLength(2000).InitTables(typeof(UserEntity)); #endif //д // db.QueryFilter.AddTableFilter(it => it.IsDeleted == false); @@ -95,7 +96,7 @@ SnowFlakeSingle.WorkId = Convert.ToInt32(builder.Configuration.GetSection("SnowF var app = builder.Build(); ServiceLocator.Instance = app.Services; var device = app.Services.GetService(); - await device?.Init(); +await device?.Init(); app.UseStaticFiles(new StaticFileOptions() { FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "Img")), @@ -104,7 +105,7 @@ app.UseStaticFiles(new StaticFileOptions() // Configure the HTTP request pipeline. //if (app.Environment.IsDevelopment()) //{ - + //} app.UseSwagger(); app.UseSwaggerUI(); @@ -115,6 +116,8 @@ app.UseCors("CorsPolicy"); //쳣м app.UseMiddleware(); +//token֤м +app.UseMiddleware(); //ִƥĶ˵ app.UseEndpoints(endpoints => { diff --git a/Service/UserService.cs b/Service/UserService.cs index a6863df..99b644e 100644 --- a/Service/UserService.cs +++ b/Service/UserService.cs @@ -1,6 +1,7 @@ using GraphQL; using LY.App.Common.Cypher; using LY.App.Common.Redis; +using LY.App.Extensions.DI; using LY.App.Model; using Mapster; using Microsoft.IdentityModel.Tokens; @@ -12,6 +13,7 @@ using System.Text; namespace LY.App.Service { + [ServiceInjection(InjectionType.Transient)] public class UserService { private readonly SqlSugarClient _db; @@ -121,7 +123,6 @@ namespace LY.App.Service }; } var password = MD5CypherUtil.Hash("ly_" + input.password); - var users = await _db.Queryable().ToListAsync(); var entity = await _db.Queryable() .Where(s => s.Disable == false && s.Name == input.username && s.Password == password).FirstAsync();