feat(auth): 添加统一 API 和 PC 认证端点
- 新增 API 端 JWT 登录、refresh token 轮换和退出登录流程 - 新增 refresh token 实体、DbSet 配置和 EF Core 迁移 - 新增 PC 端授权码登录、本地全局 token 刷新、登出和鉴权服务 - 扩展统一端点模型,支持宿主过滤、角色鉴权、OpenAPI 元数据和 DI 服务处理器 - API 启用 JwtBearer 认证、Swagger UI 和认证端点注册 - PC 端注册认证服务,并按宿主过滤桌面拦截端点
This commit is contained in:
@@ -4,10 +4,6 @@ using Avalonia_EFCore.Models;
|
||||
using Avalonia_Services.Core;
|
||||
using Avalonia_Services.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Avalonia_Services.Endpoints
|
||||
{
|
||||
@@ -40,6 +36,7 @@ namespace Avalonia_Services.Endpoints
|
||||
// ---- 业务端点注册 ----
|
||||
// 天气预报(从数据库读取)
|
||||
endpoints.MapGet("api/wData", GetWeatherForecastsAsync)
|
||||
.WithOpenApi("Weather", "获取天气预报信息。")
|
||||
.WithName("GetWeatherForecast");
|
||||
|
||||
// 获取用户(演示从数据库查询)
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
using Avalonia_Services.Core;
|
||||
using Avalonia_Services.Services.AuthService;
|
||||
|
||||
namespace Avalonia_Services.Endpoints
|
||||
{
|
||||
/// <summary>
|
||||
/// 认证端点统一入口。端点定义在这里,宿主项目只提供对应实现。
|
||||
/// </summary>
|
||||
public static class AuthEndpoints
|
||||
{
|
||||
public static void ConfigureApi(ServiceEndpointBuilder builder)
|
||||
{
|
||||
builder.ConfigureEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapPost<IApiAuthEndpointService>("api/auth/login", (service, ctx) => service.LoginAsync(ctx))
|
||||
.WithName("ApiLogin")
|
||||
.WithOpenApi("Auth", "API 登录,返回 access token 和 refresh token。", "", typeof(ApiLoginRequest), typeof(AuthTokenResponse))
|
||||
.ApiOnly();
|
||||
|
||||
endpoints.MapPost<IApiAuthEndpointService>("api/auth/refresh", (service, ctx) => service.RefreshAsync(ctx))
|
||||
.WithName("ApiRefresh")
|
||||
.WithOpenApi("Auth", "API refresh token 轮换。", "", typeof(ApiRefreshTokenRequest), typeof(AuthTokenResponse))
|
||||
.ApiOnly();
|
||||
|
||||
endpoints.MapPost<IApiAuthEndpointService>("api/auth/logout", (service, ctx) => service.LogoutAsync(ctx))
|
||||
.WithName("ApiLogout")
|
||||
.WithOpenApi("Auth", "API 退出登录并吊销 refresh token。", "", typeof(ApiLogoutRequest))
|
||||
.ApiOnly();
|
||||
});
|
||||
}
|
||||
|
||||
public static void ConfigurePc(ServiceEndpointBuilder builder)
|
||||
{
|
||||
builder.ConfigureEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapPost<IPcAuthEndpointService>("api/pc/auth/authorize", (service, ctx) => service.AuthorizeAsync(ctx))
|
||||
.WithName("PcAuthorize")
|
||||
.WithOpenApi("Auth", "PC 授权码登录,生成本地全局 token。", "", typeof(PcAuthorizeRequest), typeof(PcTokenResponse))
|
||||
.PcOnly();
|
||||
|
||||
endpoints.MapPost<IPcAuthEndpointService>("api/pc/auth/refresh", (service, ctx) => service.RefreshAsync(ctx))
|
||||
.WithName("PcRefresh")
|
||||
.WithOpenApi("Auth", "PC 全局 token 刷新。", "", typeof(PcRefreshRequest), typeof(PcTokenResponse))
|
||||
.PcOnly();
|
||||
|
||||
endpoints.MapPost<IPcAuthEndpointService>("api/pc/auth/logout", (service, ctx) => service.LogoutAsync(ctx))
|
||||
.WithName("PcLogout")
|
||||
.WithOpenApi("Auth", "PC 退出登录。", "", typeof(PcLogoutRequest))
|
||||
.PcOnly();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user