token 验证
This commit is contained in:
parent
4922ae31b1
commit
947edb0409
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 这是放不需要过滤的api地址
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
private bool IsExcludedPath(PathString path)
|
||||
{
|
||||
// 根据实际情况定义不需要Token验证的API路径
|
||||
string[] arry = { "login" };
|
||||
return arry.Any(s => path.Value.Contains(s));
|
||||
// return path.StartsWithSegments("/api/public");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -4,7 +4,5 @@
|
|||
{
|
||||
public string username { get; set; }
|
||||
public string password { get; set; }
|
||||
public string type { get; set; }
|
||||
public string vertifyCode { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ namespace LY.App.Model
|
|||
/// 位置信息
|
||||
/// </summary>
|
||||
[SugarTable("ly_position")]
|
||||
public class PositionInfo: MultPolygonEntity
|
||||
public class PositionInfo : MultPolygonEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// 名称
|
||||
|
|
@ -30,43 +30,11 @@ namespace LY.App.Model
|
|||
/// </summary>
|
||||
[SugarColumn(Length = 31, IsNullable = true, ColumnDescription = "联系人电话")]
|
||||
public string ContactTel { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 图片文件名
|
||||
/// 图片
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = true, ColumnDescription = "图片地址")]
|
||||
public string ImageName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 图片地址
|
||||
/// </summary>
|
||||
[SugarColumn(IsIgnore = true)]
|
||||
public string ImageUrl { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 图片缩略图地址
|
||||
/// </summary>
|
||||
[SugarColumn(IsIgnore = true)]
|
||||
public string ImageBriefUrl { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 启用时间
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = true, ColumnDescription = "启用时间")]
|
||||
public DateTime? EnableTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否启用
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "是否启用")]
|
||||
public bool Enabled { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 状态
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = true, ColumnDescription = "状态")]
|
||||
public string Status { get; set; } = "离线";
|
||||
|
||||
[SugarColumn(IsNullable = true, ColumnDescription = "图片地址", ColumnName = "img")]
|
||||
public string Img { get; set; }
|
||||
/// <summary>
|
||||
/// 备注
|
||||
/// </summary>
|
||||
|
|
@ -78,7 +46,6 @@ namespace LY.App.Model
|
|||
/// </summary>
|
||||
public class AddPosition
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 名称
|
||||
/// </summary>
|
||||
|
|
@ -89,21 +56,9 @@ namespace LY.App.Model
|
|||
/// </summary>
|
||||
|
||||
public string RegionJson { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 经度
|
||||
/// </summary>
|
||||
public double Lon { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 纬度
|
||||
/// </summary>
|
||||
public double Lat { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 地址
|
||||
/// </summary>
|
||||
|
||||
public string Address { get; set; }
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -116,24 +71,9 @@ namespace LY.App.Model
|
|||
public string ContactTel { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 图片文件名
|
||||
/// 图片
|
||||
/// </summary>
|
||||
public string ImageName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 启用时间
|
||||
/// </summary>
|
||||
public DateTime? EnableTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否启用
|
||||
/// </summary>
|
||||
public bool Enabled { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 状态
|
||||
/// </summary>
|
||||
public string Status { get; set; } = "离线";
|
||||
public string Img { get; set; }
|
||||
/// <summary>
|
||||
/// 备注
|
||||
/// </summary>
|
||||
|
|
@ -150,15 +90,4 @@ namespace LY.App.Model
|
|||
/// </summary>
|
||||
public long Id { get; set; }
|
||||
}
|
||||
/// <summary>
|
||||
/// 阵地下所有区域
|
||||
/// </summary>
|
||||
public class GeoRegion
|
||||
{
|
||||
/// <summary>
|
||||
/// 识别区
|
||||
/// </summary>
|
||||
public string Region { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,6 +43,7 @@ string redisConnection = builder.Configuration.GetValue<string>("Redis:Connectio
|
|||
|
||||
// 注册 RedisService
|
||||
builder.Services.AddSingleton(new RedisService(redisConnection));
|
||||
//builder.Services.AddTransient<TokenValidationMiddleware>();
|
||||
////注册SignalR
|
||||
builder.Services.AddSignalR();
|
||||
builder.Services.AddHttpClient();
|
||||
|
|
@ -77,7 +78,7 @@ builder.Services.AddTransient<SqlSugarClient>(sp =>
|
|||
};
|
||||
//创建数据库和表的语句仅执行一次
|
||||
//db.DbMaintenance.CreateDatabase();
|
||||
// db.CodeFirst.SetStringDefaultLength(2000).InitTables(typeof(LogEntity));
|
||||
//db.CodeFirst.SetStringDefaultLength(2000).InitTables(typeof(UserEntity));
|
||||
#endif
|
||||
//过滤器写在这儿就行了
|
||||
// db.QueryFilter.AddTableFilter<IDeleted>(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<DeviceService>();
|
||||
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<CustomErrorMiddleware>();
|
||||
//token验证中间件
|
||||
app.UseMiddleware<TokenValidationMiddleware>();
|
||||
//执行匹配的端点
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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<UserEntity>().ToListAsync();
|
||||
var entity = await _db.Queryable<UserEntity>()
|
||||
.Where(s => s.Disable == false &&
|
||||
s.Name == input.username && s.Password == password).FirstAsync();
|
||||
|
|
|
|||
Loading…
Reference in New Issue