This commit is contained in:
Bob.Song
2026-03-26 18:01:04 +08:00
parent f696d08489
commit a924cd9c01
6 changed files with 256 additions and 7 deletions

View File

@@ -10,6 +10,7 @@
<ItemGroup>
<PackageReference Include="Fantasy-Net" Version="2025.2.1423" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.14" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="8.14.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.14.0" />
</ItemGroup>

View File

@@ -57,13 +57,11 @@
networkProtocol=""
outerPort="0" innerPort="11031" />
<!-- <scene id="1007" -->
<!-- processConfigId="1" -->
<!-- worldConfigId="1" -->
<!-- sceneRuntimeMode="MultiThread" -->
<!-- sceneTypeString="Map" -->
<!-- networkProtocol="" -->
<!-- outerPort="0" innerPort="11061" /> -->
<!-- HTTP场景对外API接口 -->
<scene id="8001" processConfigId="1" worldConfigId="1"
sceneRuntimeMode="MultiThread" sceneTypeString="API"
networkProtocol="HTTP" outerPort="6052" innerPort="11004" />
</scenes>
</server>
</fantasy>

View 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", "Fantasy");
context.Response.Headers.Add("X-Server-Version", "1.0.0");
await next.Invoke();
});
Log.Info($"[HTTP] 游戏应用配置完成: Scene {self.Scene.SceneConfigId}");
await FTask.CompletedTask;
}
}
}

View File

@@ -0,0 +1,89 @@
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;
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;
}
}

View File

@@ -0,0 +1,89 @@
using Fantasy;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace NBF.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class PlayerController : ControllerBase
{
private readonly Scene _scene;
private readonly IPlayerService _playerService;
public PlayerController(Scene scene, IPlayerService playerService)
{
_scene = scene;
_playerService = playerService;
}
/// <summary>
/// 获取玩家信息(需要认证)
/// </summary>
[HttpGet("{playerId}")]
[Authorize(Policy = "Player")]
public async Task<IActionResult> GetPlayer(long playerId)
{
var player = await _playerService.GetPlayerAsync(playerId);
if (player == null)
{
return NotFound(new { error = "Player not found" });
}
return Ok(new
{
playerId = player.Id,
name = player.Name,
level = player.Level,
exp = player.Exp
});
}
/// <summary>
/// 登录接口(无需认证)
/// </summary>
[HttpPost("login")]
[AllowAnonymous]
public async Task<IActionResult> Login([FromBody] LoginRequest request)
{
var (success, token, playerId) = await _playerService.LoginAsync(
request.Username, request.Password);
if (!success)
{
return Unauthorized(new { error = "Invalid credentials" });
}
return Ok(new
{
token = token,
playerId = playerId,
expiresIn = 3600
});
}
/// <summary>
/// 管理员接口(需要 Admin 角色)
/// </summary>
[HttpPost("ban/{playerId}")]
[Authorize(Policy = "Admin")]
public async Task<IActionResult> BanPlayer(long playerId, [FromBody] BanRequest request)
{
await _playerService.BanPlayerAsync(playerId, request.Reason, request.Duration);
return Ok(new { message = "Player banned successfully" });
}
}
public class LoginRequest
{
public string Username { get; set; }
public string Password { get; set; }
}
public class BanRequest
{
public string Reason { get; set; }
public int Duration { get; set; } // 分钟
}
}

View File

@@ -0,0 +1,6 @@
namespace NBF;
public class PlayerService : IPlayerService
{
}