- 新增 API 端 JWT 登录、refresh token 轮换和退出登录流程 - 新增 refresh token 实体、DbSet 配置和 EF Core 迁移 - 新增 PC 端授权码登录、本地全局 token 刷新、登出和鉴权服务 - 扩展统一端点模型,支持宿主过滤、角色鉴权、OpenAPI 元数据和 DI 服务处理器 - API 启用 JwtBearer 认证、Swagger UI 和认证端点注册 - PC 端注册认证服务,并按宿主过滤桌面拦截端点
85 lines
3.0 KiB
C#
85 lines
3.0 KiB
C#
using Authentication;
|
||
using Avalonia;
|
||
using Avalonia_Common.Infrastructure;
|
||
using Avalonia_EFCore.Database;
|
||
using Avalonia_PC.Authentication;
|
||
using Avalonia_PC.Views;
|
||
using Avalonia_Services.Core;
|
||
using Avalonia_Services.Endpoints;
|
||
using Avalonia_Services.Services;
|
||
using Avalonia_Services.Services.AuthService;
|
||
using Microsoft.Extensions.DependencyInjection;
|
||
using Serilog;
|
||
using System;
|
||
|
||
namespace Avalonia_PC
|
||
{
|
||
internal sealed class Program
|
||
{
|
||
public static IServiceProvider Services { get; private set; } = null!;
|
||
|
||
[STAThread]
|
||
public static void Main(string[] args)
|
||
{
|
||
// 初始化日志系统
|
||
AppLog.Initialize(LoggingConfiguration.CreateDefaultLogger(logDir: "logs"));
|
||
|
||
AppLog.Information("Avalonia-PC 正在启动...");
|
||
|
||
ConfigureServices();
|
||
|
||
// 初始化数据库(自动迁移 + 种子数据)
|
||
Services.InitializeDatabase<AppDataContext>();
|
||
|
||
#if DEBUG
|
||
// 开启 WebView2 远程调试,启动后在 Edge 中访问 edge://inspect 调试网页
|
||
Environment.SetEnvironmentVariable(
|
||
"WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS",
|
||
"--remote-debugging-port=9222 --auto-open-devtools-for-tabs");
|
||
#endif
|
||
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
|
||
}
|
||
|
||
private static void ConfigureServices()
|
||
{
|
||
var services = new ServiceCollection();
|
||
|
||
// ---- 数据库 ----
|
||
// 注册默认数据库提供程序(SQLite / MySQL / PostgreSQL / SqlServer)
|
||
DatabaseProviderRegistry.RegisterDefaults();
|
||
|
||
// 桌面端固定使用 SQLite 本地数据库
|
||
services.AddAppDatabase<AppDataContext>(DatabaseConfiguration.ForSQLite("app.db"));
|
||
|
||
// ---- 业务服务 ----
|
||
services.AddSingleton<WeatherForecastService>();
|
||
services.AddSingleton<IPcThirdPartyAuthorizationClient, DefaultPcThirdPartyAuthorizationClient>();
|
||
services.AddSingleton<PcGlobalTokenService>();
|
||
services.AddSingleton<IAuthService, PcAuthService>();
|
||
services.AddSingleton<IPcAuthEndpointService, PcAuthEndpointService>();
|
||
|
||
// ---- 端点注册 ----
|
||
var endpointBuilder = new ServiceEndpointBuilder();
|
||
AppEndpoints.Configure(endpointBuilder);
|
||
AuthEndpoints.ConfigurePc(endpointBuilder);
|
||
var endpoints = endpointBuilder.Build();
|
||
services.AddSingleton(endpoints);
|
||
|
||
// 注册 Window
|
||
services.AddTransient<MainWindow>(sp => new MainWindow(sp));
|
||
|
||
Services = services.BuildServiceProvider();
|
||
}
|
||
|
||
// Avalonia configuration, don't remove; also used by visual designer.
|
||
public static AppBuilder BuildAvaloniaApp()
|
||
=> AppBuilder.Configure<App>()
|
||
.UsePlatformDetect()
|
||
#if DEBUG
|
||
.WithDeveloperTools()
|
||
#endif
|
||
.WithInterFont()
|
||
.LogToTrace();
|
||
}
|
||
}
|