提交
This commit is contained in:
66
Hotfix/Api/Handler/GameHttpApplicationHandler.cs
Normal file
66
Hotfix/Api/Handler/GameHttpApplicationHandler.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using Fantasy.Async;
|
||||
using Fantasy.Event;
|
||||
using Fantasy.Network.HTTP;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using System;
|
||||
using Fantasy;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public class GameHttpApplicationHandler : AsyncEventSystem<OnConfigureHttpApplication>
|
||||
{
|
||||
protected override async FTask Handler(OnConfigureHttpApplication self)
|
||||
{
|
||||
var app = self.Application;
|
||||
|
||||
// 1. CORS(必须在认证之前)
|
||||
app.UseCors("GameClient");
|
||||
|
||||
// 2. 请求日志中间件
|
||||
app.Use(async (context, next) =>
|
||||
{
|
||||
var requestId = Guid.NewGuid().ToString("N");
|
||||
context.Items["RequestId"] = requestId;
|
||||
|
||||
var start = DateTime.UtcNow;
|
||||
var method = context.Request.Method;
|
||||
var path = context.Request.Path;
|
||||
var ip = context.Connection.RemoteIpAddress?.ToString();
|
||||
|
||||
Log.Info($"[HTTP-{requestId}] {method} {path} - IP: {ip}");
|
||||
|
||||
try
|
||||
{
|
||||
await next.Invoke();
|
||||
|
||||
var duration = (DateTime.UtcNow - start).TotalMilliseconds;
|
||||
var status = context.Response.StatusCode;
|
||||
Log.Info($"[HTTP-{requestId}] {status} - {duration}ms");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error($"[HTTP-{requestId}] 异常: {e.Message}");
|
||||
throw;
|
||||
}
|
||||
});
|
||||
|
||||
// 3. 认证
|
||||
app.UseAuthentication();
|
||||
|
||||
// 4. 授权
|
||||
app.UseAuthorization();
|
||||
|
||||
// 5. 自定义响应头
|
||||
app.Use(async (context, next) =>
|
||||
{
|
||||
context.Response.Headers.Add("X-Game-Server", "NBF");
|
||||
context.Response.Headers.Add("X-Server-Version", "1.0.0");
|
||||
await next.Invoke();
|
||||
});
|
||||
|
||||
Log.Info($"[HTTP] 游戏应用配置完成: Scene {self.Scene.SceneConfigId}");
|
||||
|
||||
await FTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
90
Hotfix/Api/Handler/GameHttpServicesHandler.cs
Normal file
90
Hotfix/Api/Handler/GameHttpServicesHandler.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using Fantasy.Async;
|
||||
using Fantasy.Event;
|
||||
using Fantasy.Network.HTTP;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Fantasy;
|
||||
|
||||
namespace NBF;
|
||||
|
||||
public class GameHttpServicesHandler : AsyncEventSystem<OnConfigureHttpServices>
|
||||
{
|
||||
protected override async FTask Handler(OnConfigureHttpServices self)
|
||||
{
|
||||
// 1. 配置 JSON 序列化
|
||||
self.MvcBuilder.AddJsonOptions(options =>
|
||||
{
|
||||
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
|
||||
options.JsonSerializerOptions.WriteIndented = true;
|
||||
options.JsonSerializerOptions.DefaultIgnoreCondition =
|
||||
JsonIgnoreCondition.WhenWritingNull;
|
||||
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
|
||||
});
|
||||
|
||||
// 2. 添加全局过滤器
|
||||
self.MvcBuilder.AddMvcOptions(options =>
|
||||
{
|
||||
// options.Filters.Add<GameExceptionFilter>();
|
||||
// options.Filters.Add<ModelValidationFilter>();
|
||||
});
|
||||
|
||||
// 3. 配置 JWT 认证
|
||||
var jwtSecret = "YourSuperSecretKeyForJwtTokenGeneration123!";
|
||||
self.Builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
options.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = true,
|
||||
ValidateAudience = true,
|
||||
ValidateLifetime = true,
|
||||
ValidateIssuerSigningKey = true,
|
||||
ValidIssuer = "GameServer",
|
||||
ValidAudience = "GameClient",
|
||||
IssuerSigningKey = new SymmetricSecurityKey(
|
||||
Encoding.UTF8.GetBytes(jwtSecret)),
|
||||
ClockSkew = TimeSpan.Zero
|
||||
};
|
||||
});
|
||||
|
||||
// 4. 配置授权策略
|
||||
self.Builder.Services.AddAuthorization(options =>
|
||||
{
|
||||
options.AddPolicy("Player", policy =>
|
||||
policy.RequireRole("Player", "Admin"));
|
||||
options.AddPolicy("Admin", policy =>
|
||||
policy.RequireRole("Admin"));
|
||||
options.AddPolicy("VIP", policy =>
|
||||
policy.RequireClaim("VIPLevel"));
|
||||
});
|
||||
|
||||
// 5. 配置 CORS
|
||||
self.Builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy("GameClient", builder =>
|
||||
{
|
||||
builder.WithOrigins(
|
||||
"https://game.example.com",
|
||||
"https://cdn.game.example.com"
|
||||
)
|
||||
.AllowAnyMethod()
|
||||
.AllowAnyHeader()
|
||||
.AllowCredentials();
|
||||
});
|
||||
});
|
||||
|
||||
// // 6. 注册游戏服务
|
||||
// self.Builder.Services.AddSingleton<IPlayerService, PlayerService>();
|
||||
// self.Builder.Services.AddSingleton<IGuildService, GuildService>();
|
||||
// self.Builder.Services.AddScoped<IAuthService, AuthService>();
|
||||
// self.Builder.Services.AddScoped<IGameRepository, GameRepository>();
|
||||
|
||||
Log.Info($"[HTTP] 游戏服务配置完成: Scene {self.Scene.SceneConfigId}");
|
||||
|
||||
await FTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user