This commit is contained in:
qianhao.xu 2024-07-22 15:12:00 +08:00
parent af29b35e83
commit 8421ee49a6
8 changed files with 23 additions and 13 deletions

View File

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

View File

@ -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 = 3PgSql = 4
"ConfigId": "0", //
"IsAutoCloseConnection": true

View File

@ -12,7 +12,7 @@ using DOAN.Model;
namespace DOAN.Repository
{
/// <summary>
/// 数据仓库类
/// 数据仓库类 TODO 配置 DbScoped.SugarScope 是 SqlSugar ORM 中的一个特性,主要用于管理数据库连接和事务。
/// </summary>
/// <typeparam name="T"></typeparam>
public class BaseRepository<T> : SimpleClient<T> 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<TenantAttribute>()?.configId;
if (configId != null)
{

View File

@ -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<DeviceRouteInspectionPlan> InitDataList = Context
List<DeviceRouteInspectionPlan> InitDataList = DbScoped.SugarScope.CopyNew()
.Queryable<DeviceRouteInspectionPlan>()
.Where(it => it.Status == 1)
.Where(it => it.LifeCycleStart <= DateTime.Now)

View File

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

View File

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

View File

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