diff --git a/DOAN.Admin.WebApi/Program.cs b/DOAN.Admin.WebApi/Program.cs index dde3ee4..e45488e 100644 --- a/DOAN.Admin.WebApi/Program.cs +++ b/DOAN.Admin.WebApi/Program.cs @@ -10,10 +10,10 @@ using DOAN.Common.Cache; using DOAN.Infrastructure.WebExtensions; using DOAN.ServiceCore.Signalr; using DOAN.ServiceCore.SqlSugar; +using Infrastructure; var builder = WebApplication.CreateBuilder(args); -// NLog: Setup NLog for Dependency injection -//builder.Logging.ClearProviders(); + builder.Host.UseNLog(); // Add services to the container. @@ -100,7 +100,7 @@ var app = builder.Build(); InternalApp.ServiceProvider = app.Services; InternalApp.Configuration = builder.Configuration; InternalApp.WebHostEnvironment = app.Environment; -//初始化db +//初始化db sqlsugar builder.Services.AddDb(app.Environment); //使用全局异常中间件 @@ -147,9 +147,10 @@ app.UseDirectoryBrowser(new DirectoryBrowserOptions app.UseRouting(); app.UseCors("Policy");//要放在app.UseEndpoints前。 //app.UseHttpsRedirection(); - +// 认证 这意味着对于每一个进入应用的HTTP请求,框架都会尝试从请求中提取身份验证信息(如JWT token、cookies或其他身份验证机制提供的信息),并根据这些信息来创建或更新当前用户的身份信息(ClaimsPrincipal)。如果请求中包含了有效的身份验证凭据,那么用户将被认为是已认证的。 app.UseAuthentication(); -app.UseAuthorization(); +//授权 在app.UseAuthentication(); 之后调用的 app.UseAuthorization(); 则是启用授权功能。当授权中间件被调用时,它会检查请求上的[Authorize] 属性或其他策略,以确定用户是否有权限访问特定的资源或执行特定的操作。 +app.UseAuthorization(); //授权 //开启缓存 app.UseResponseCaching(); diff --git a/DOAN.Admin.WebApi/appsettings.Development.json b/DOAN.Admin.WebApi/appsettings.Development.json index 3769487..f762fe7 100644 --- a/DOAN.Admin.WebApi/appsettings.Development.json +++ b/DOAN.Admin.WebApi/appsettings.Development.json @@ -10,7 +10,7 @@ { //"Conn": "Data Source=139.224.232.211;User ID=root;Password=doantech123;Initial Catalog=GXAssembly;Port=3308", // "Conn": "Data Source=127.0.0.1;User ID=root;Password=123456;Initial Catalog=GXAssembly;Port=3306", - "Conn": "Data Source=192.168.0.58;User ID=root;Password=123456;Initial Catalog=GXAssembly;Port=3306", + "Conn": "Data Source=192.168.50.163;User ID=root;Password=123456;Initial Catalog=GXAssembly;Port=3306", "DbType": 0, //数据库类型 MySql = 0, SqlServer = 1, Oracle = 3,PgSql = 4 "ConfigId": "0", //多租户唯一标识 "IsAutoCloseConnection": true diff --git a/DOAN.Admin.WebApi/wwwroot/export/工单列表07-22-142245.xlsx b/DOAN.Admin.WebApi/wwwroot/export/工单列表07-22-142245.xlsx new file mode 100644 index 0000000..ed48f6b Binary files /dev/null and b/DOAN.Admin.WebApi/wwwroot/export/工单列表07-22-142245.xlsx differ diff --git a/DOAN.Repository/BaseRepository.cs b/DOAN.Repository/BaseRepository.cs index b30211a..5b2ac6f 100644 --- a/DOAN.Repository/BaseRepository.cs +++ b/DOAN.Repository/BaseRepository.cs @@ -12,7 +12,7 @@ using DOAN.Model; namespace DOAN.Repository { /// - /// 数据仓库类 + /// 数据仓库类 TODO 配置 DbScoped.SugarScope 是 SqlSugar ORM 中的一个特性,主要用于管理数据库连接和事务。 /// /// public class BaseRepository : SimpleClient where T : class, new() @@ -30,6 +30,7 @@ namespace DOAN.Repository public BaseRepository(ISqlSugarClient context = null) : base(context) { //通过特性拿到ConfigId + // C# 中使用反射来获取一个类型 (T) 上定义的自定义属性 (TenantAttribute) 的值。 var configId = typeof(T).GetCustomAttribute()?.configId; if (configId != null) { diff --git a/DOAN.Service/MES/dev/DeviceTaskExecuteService.cs b/DOAN.Service/MES/dev/DeviceTaskExecuteService.cs index b28d515..af0a17c 100644 --- a/DOAN.Service/MES/dev/DeviceTaskExecuteService.cs +++ b/DOAN.Service/MES/dev/DeviceTaskExecuteService.cs @@ -13,6 +13,7 @@ using Mapster; using Microsoft.AspNetCore.Authentication; using Newtonsoft.Json.Linq; using SqlSugar; +using SqlSugar.IOC; namespace DOAN.Service.MES.dev { @@ -187,7 +188,7 @@ namespace DOAN.Service.MES.dev public int ScanEveryTask() { int result = 0; - List InitDataList = Context + List InitDataList = DbScoped.SugarScope.CopyNew() .Queryable() .Where(it => it.Status == 1) .Where(it => it.LifeCycleStart <= DateTime.Now) diff --git a/DOAN.ServiceCore/Filters/VerifyAttribute.cs b/DOAN.ServiceCore/Filters/VerifyAttribute.cs index 70a0caa..27cb062 100644 --- a/DOAN.ServiceCore/Filters/VerifyAttribute.cs +++ b/DOAN.ServiceCore/Filters/VerifyAttribute.cs @@ -29,12 +29,16 @@ namespace DOAN.Admin.WebApi.Filters noNeedCheck = controllerActionDescriptor.MethodInfo.GetCustomAttributes(inherit: true) .Any(a => a.GetType().Equals(typeof(AllowAnonymousAttribute))); } - + // 不需要校验token if (noNeedCheck) return; string ip = HttpContextExtension.GetClientUserIp(context.HttpContext); string url = context.HttpContext.Request.Path; + //是否有认证成功 + //这行代码是用来检查当前HTTP上下文中的用户是否已经被认证。这里是详细的解释 var isAuthed = context.HttpContext.User.Identity.IsAuthenticated; + + string osType = context.HttpContext.Request.Headers["os"]; //使用jwt token校验2020-11-21 TokenModel loginUser = JwtUtil.GetLoginUser(context.HttpContext); @@ -48,6 +52,7 @@ namespace DOAN.Admin.WebApi.Filters var CK = "token_" + loginUser.UserId; if (!CacheHelper.Exists(CK) && ts.TotalMinutes < 5) { + // 刷新token var newToken = JwtUtil.GenerateJwtToken(JwtUtil.AddClaims(loginUser)); CacheHelper.SetCache(CK, CK, 1); diff --git a/DOAN.Tasks/TaskScheduler/Job_Device_Execute.cs b/DOAN.Tasks/TaskScheduler/Job_Device_Execute.cs index 5b21f39..e2b3794 100644 --- a/DOAN.Tasks/TaskScheduler/Job_Device_Execute.cs +++ b/DOAN.Tasks/TaskScheduler/Job_Device_Execute.cs @@ -38,6 +38,7 @@ namespace DOAN.Tasks.TaskScheduler { AbstractTrigger trigger = (context as JobExecutionContextImpl).Trigger as AbstractTrigger; //var info = await tasksQzService.CopyNew().GetByIdAsync(trigger.JobName); + // CopyNew 在多线程环境中,为每个线程或任务创建新的 SugarScope 实例以避免线程安全问题 var info = await DbScoped.SugarScope.CopyNew().Queryable().FirstAsync(f => f.ID == trigger.JobName); if (info == null) { @@ -45,7 +46,7 @@ namespace DOAN.Tasks.TaskScheduler } int result = deviceTaskExecute.ScanEveryTask(); - logger.Info($"任务【{info.Name}】设备管理调度请求执行结果=" + result); + logger.Info($"(job)任务【{info.Name}】设备管理调度请求执行结果=" + result); } } diff --git a/Infrastructure/WebExtensions/JwtExtension.cs b/Infrastructure/WebExtensions/JwtExtension.cs index e9c58de..82a224c 100644 --- a/Infrastructure/WebExtensions/JwtExtension.cs +++ b/Infrastructure/WebExtensions/JwtExtension.cs @@ -11,14 +11,15 @@ namespace DOAN.Infrastructure.WebExtensions { public static void AddJwt(this IServiceCollection services) { + // 添加依赖 默认的身份验证方案为JWT承载 services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; - }).AddCookie() - .AddJwtBearer(o => + }).AddCookie() // 虽然主要使用的是JWT,.AddCookie()的调用可以为混合场景提供支持 + .AddJwtBearer(o => //具体配置JWT承载身份验证 { - o.TokenValidationParameters = JwtUtil.ValidParameters(); + o.TokenValidationParameters = JwtUtil.ValidParameters(); //通常是一个包含了用于验证JWT的公钥、颁发者、接收者等信息的TokenValidationParameters对象。 o.Events = new JwtBearerEvents { OnAuthenticationFailed = context =>