修改协议工具
This commit is contained in:
64
Hotfix/Gate/System/JWT/GateJWTComponentSystem.cs
Normal file
64
Hotfix/Gate/System/JWT/GateJWTComponentSystem.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Cryptography;
|
||||
using Fantasy.Entitas.Interface;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
|
||||
|
||||
namespace NB.Gate;
|
||||
|
||||
public sealed class GateJWTComponentAwakeSystem : AwakeSystem<GateJWTComponent>
|
||||
{
|
||||
protected override void Awake(GateJWTComponent self)
|
||||
{
|
||||
self.Awake();
|
||||
}
|
||||
}
|
||||
|
||||
public static class GateJWTComponentSystem
|
||||
{
|
||||
public static void Awake(this GateJWTComponent self)
|
||||
{
|
||||
var rsa = RSA.Create();
|
||||
rsa.ImportRSAPublicKey(Convert.FromBase64String(self.PublicKeyPem), out _);
|
||||
self.SigningCredentials = new SigningCredentials(new RsaSecurityKey(rsa), SecurityAlgorithms.RsaSha256);
|
||||
// 创建 TokenValidationParameters 对象,用于配置验证参数
|
||||
self.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateLifetime = false, // 禁止令牌验证时间是否过期
|
||||
ValidateIssuer = true, // 验证发行者
|
||||
ValidateAudience = true, // 验证受众
|
||||
ValidateIssuerSigningKey = true, // 验证签名密钥
|
||||
ValidIssuer = "Fantasy", // 有效的发行者
|
||||
ValidAudience = "Fantasy", // 有效的受众
|
||||
IssuerSigningKey = new RsaSecurityKey(rsa) // RSA公钥作为签名密钥
|
||||
};
|
||||
}
|
||||
|
||||
public static bool ValidateToken(this GateJWTComponent self, string token, out JwtPayload payload)
|
||||
{
|
||||
payload = null;
|
||||
|
||||
try
|
||||
{
|
||||
var jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
|
||||
jwtSecurityTokenHandler.ValidateToken(token, self.TokenValidationParameters, out _);
|
||||
payload = jwtSecurityTokenHandler.ReadJwtToken(token).Payload;
|
||||
return true;
|
||||
}
|
||||
catch (SecurityTokenInvalidAudienceException)
|
||||
{
|
||||
Console.WriteLine("验证受众失败!");
|
||||
return false;
|
||||
}
|
||||
catch (SecurityTokenInvalidIssuerException)
|
||||
{
|
||||
Console.WriteLine("验证发行者失败!");
|
||||
return false;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
35
Hotfix/Gate/System/JWT/GateJWTHelper.cs
Normal file
35
Hotfix/Gate/System/JWT/GateJWTHelper.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using Fantasy;
|
||||
|
||||
namespace NB.Gate;
|
||||
|
||||
public static class GateJWTHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 验证令牌是否合法
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
/// <param name="token">ToKen</param>
|
||||
/// <param name="accountId">如果验证成功会返回正常的AccountId</param>
|
||||
/// <returns>如果是True表示验证成功,False表示验证失败</returns>
|
||||
public static bool ValidateToken(Scene scene, string token, out long accountId)
|
||||
{
|
||||
if (!ValidateToken(scene, token, out JwtPayload payload))
|
||||
{
|
||||
// 如果令牌验证失败,表示当前令牌不合法、那就返回为false,让上层处理。
|
||||
accountId = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
// 如果不等于当前Scene的ConfigId的话,把就表示该连接不应该连接到当前的Gate里。
|
||||
// 所以理应把当前连接关闭掉。
|
||||
var sceneId = Convert.ToInt64(payload["SceneId"]);
|
||||
accountId = Convert.ToInt64(payload["aId"]);
|
||||
return sceneId == scene.SceneConfigId;
|
||||
}
|
||||
|
||||
private static bool ValidateToken(Scene scene, string token, out JwtPayload payload)
|
||||
{
|
||||
return scene.GetComponent<GateJWTComponent>().ValidateToken(token, out payload);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user