You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.4 KiB
78 lines
2.4 KiB
using HuiXin.Gateway.Ocelot.Extensions;
|
|
using Serilog;
|
|
using System.Security.Cryptography;
|
|
|
|
namespace HuiXin.Gateway.Ocelot
|
|
{
|
|
public class Program
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
//BuildJwt();
|
|
|
|
var configuration = new ConfigurationBuilder()
|
|
.AddJsonFile("Configs/ocelot.json", false, true)
|
|
.AddJsonFile("Configs/jwt.json", false, false)
|
|
.AddJsonFile("Configs/serilog.json", false, true)
|
|
.Build();
|
|
|
|
var logger = new LoggerConfiguration()
|
|
.ReadFrom.Configuration(configuration)
|
|
.CreateLogger();
|
|
Log.Logger = logger;
|
|
|
|
try
|
|
{
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
builder.Configuration.AddConfiguration(configuration);
|
|
|
|
var services = builder.Services;
|
|
|
|
services.AddCors();
|
|
services.AddLogging(loggingBuilder => loggingBuilder.ClearProviders().AddSerilog());
|
|
|
|
services.AddJWT(configuration);
|
|
|
|
services.AddMyOcelot(configuration);
|
|
|
|
var app = builder.Build();
|
|
|
|
app.UseCors();
|
|
app.UseJWT();
|
|
|
|
app.UseMyOcelot();
|
|
|
|
app.Run();
|
|
|
|
Log.Information("系统已启动");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Fatal(ex, "系统异常");
|
|
}
|
|
finally
|
|
{
|
|
Log.CloseAndFlush();
|
|
}
|
|
}
|
|
|
|
static void BuildJwt()
|
|
{
|
|
using var aes = Aes.Create();
|
|
aes.KeySize = 256;
|
|
aes.GenerateKey();
|
|
var sign = Convert.ToBase64String(aes.Key);
|
|
var extraHeaders = new Dictionary<string, string> { { "kid", "MyKey" } };
|
|
//过期时间(可以不设置,下面表示签名后 10秒过期)
|
|
double exp = (DateTime.UtcNow.AddDays(10) - new DateTime(1970, 1, 1)).TotalSeconds;
|
|
var payload = new Dictionary<string, object>
|
|
{
|
|
{ "userId", "001" },
|
|
{ "userAccount", "fan" },
|
|
{ "exp",exp }
|
|
};
|
|
Console.WriteLine("Token:" + JWTUtil.CreateJwtToken(payload, sign, extraHeaders));
|
|
}
|
|
}
|
|
}
|
|
|