Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dd38f315d5 | ||
|
|
c448381f23 | ||
|
|
8d5b667c88 |
@@ -7,7 +7,7 @@ namespace SimApi.Communications;
|
||||
/// </summary>
|
||||
public class SimApiLoginItem
|
||||
{
|
||||
public string Id { get; set; } = null!;
|
||||
public required string Id { get; set; }
|
||||
public string[] Type { get; set; } = ["user"];
|
||||
public Dictionary<string, string> Meta { get; set; } = [];
|
||||
public object? Extra { get; set; }
|
||||
|
||||
@@ -4,13 +4,30 @@ public class SimApiJobOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// WebUi地址,设置为null表示不启用
|
||||
/// 默认 /jobs
|
||||
/// </summary>
|
||||
public string? DashboardUrl { get; set; } = "/jobs";
|
||||
|
||||
/// <summary>
|
||||
/// webui 用户
|
||||
/// 默认 admin
|
||||
/// </summary>
|
||||
public string DashboardAuthUser { get; set; } = "admin";
|
||||
|
||||
/// <summary>
|
||||
/// webui 密码
|
||||
/// 默认 Admin@123!
|
||||
/// </summary>
|
||||
public string DashboardAuthPass { get; set; } = "Admin@123!";
|
||||
|
||||
/// <summary>
|
||||
/// 设置为null 使用默认redis配置
|
||||
/// </summary>
|
||||
public string? RedisConfiguration { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 设置为null 使用默认redis配置
|
||||
/// </summary>
|
||||
public int? Database { get; set; } = null;
|
||||
public SimApiJobServerConfig[] Servers { get; set; } = [new()];
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ public class SimApiJobWebAuth(string user, string pass) : IDashboardAuthorizatio
|
||||
public bool Authorize(DashboardContext context)
|
||||
{
|
||||
var httpContext = context.GetHttpContext();
|
||||
var authHeader = httpContext.Request.Headers["Authorization"].FirstOrDefault();
|
||||
var authHeader = httpContext.Request.Headers.Authorization.FirstOrDefault();
|
||||
if (authHeader != null && authHeader.StartsWith("Basic "))
|
||||
{
|
||||
var encodedUsernamePassword = authHeader.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries)[1].Trim();
|
||||
|
||||
@@ -24,6 +24,8 @@ public class SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExcep
|
||||
try
|
||||
{
|
||||
await next(context);
|
||||
if (!context.Response.HasStarted)
|
||||
{
|
||||
switch (context.Response.StatusCode)
|
||||
{
|
||||
case 200:
|
||||
@@ -36,6 +38,7 @@ public class SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExcep
|
||||
throw new SimApiException(context.Response.StatusCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (SimApiException ex)
|
||||
{
|
||||
response = string.IsNullOrEmpty(ex.Message)
|
||||
|
||||
+1
-1
@@ -127,6 +127,7 @@ public static class SimApiExtensions
|
||||
x.OperationFilter<SimApiResponseOperationFilter>();
|
||||
x.OperationFilter<SimApiSignOperationFilter>();
|
||||
x.OperationFilter<AesBodyOperationFilter>();
|
||||
x.SchemaFilter<GlobalDynamicObjectSchemaFilter>();
|
||||
if (simApiOptions.EnableSimApiAuth)
|
||||
{
|
||||
x.OperationFilter<SimApiAuthOperationFilter>();
|
||||
@@ -239,7 +240,6 @@ public static class SimApiExtensions
|
||||
if (simApiOptions.EnableSimApiResponseFilter)
|
||||
{
|
||||
builder.AddControllers(opt => opt.Filters.Add<SimApiResponseFilter>())
|
||||
.AddXmlSerializerFormatters()
|
||||
.AddJsonOptions(opt =>
|
||||
{
|
||||
opt.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using Microsoft.OpenApi.Any;
|
||||
|
||||
namespace SimApi.SwaggerFilters;
|
||||
|
||||
public class GlobalDynamicObjectSchemaFilter : ISchemaFilter
|
||||
{
|
||||
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
|
||||
{
|
||||
if (!IsDynamicObjectType(context.Type)) return;
|
||||
schema.AdditionalPropertiesAllowed = true;
|
||||
schema.AdditionalProperties = new OpenApiSchema
|
||||
{
|
||||
Type = "object", // 表示 value 可以是任意类型(兼容所有类型)
|
||||
Nullable = true
|
||||
};
|
||||
|
||||
// 2. 覆盖默认示例,使用包含多种类型的示例
|
||||
schema.Example = CreateMultiTypeExample();
|
||||
}
|
||||
|
||||
// 判断是否为需要处理的“动态对象”类型
|
||||
private bool IsDynamicObjectType(Type? type)
|
||||
{
|
||||
if (type == null) return false;
|
||||
if (typeof(IDictionary).IsAssignableFrom(type) ||
|
||||
(type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary<,>)))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (type == typeof(object))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return type.Name.Contains("AnonymousType") && type.Namespace == null;
|
||||
}
|
||||
|
||||
// 创建包含多种类型的示例(覆盖默认的 string 示例)
|
||||
private OpenApiObject CreateMultiTypeExample()
|
||||
{
|
||||
return new OpenApiObject
|
||||
{
|
||||
["stringProp"] = new OpenApiString("example string"), // 字符串
|
||||
["numberProp"] = new OpenApiInteger(123), // 数字
|
||||
["boolProp"] = new OpenApiBoolean(true), // 布尔值
|
||||
["objectProp"] = new OpenApiObject // 嵌套对象
|
||||
{
|
||||
["nestedKey"] = new OpenApiString("nested value")
|
||||
},
|
||||
["arrayProp"] = new OpenApiArray // 数组
|
||||
{
|
||||
new OpenApiInteger(1),
|
||||
new OpenApiString("two")
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user