修改接口
This commit is contained in:
@@ -1,18 +1,20 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Quartz;
|
||||
using FutureMailAPI.Data;
|
||||
using FutureMailAPI.Helpers;
|
||||
using FutureMailAPI.Services;
|
||||
using FutureMailAPI.Middleware;
|
||||
using FutureMailAPI.Extensions;
|
||||
using FutureMailAPI.Filters;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
using System.Text;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// 配置服务器监听所有网络接口的5001端口
|
||||
// 配置服务器监听所有网络接口的5003端口
|
||||
builder.WebHost.ConfigureKestrel(options =>
|
||||
{
|
||||
options.ListenAnyIP(5001);
|
||||
options.ListenAnyIP(5003);
|
||||
});
|
||||
|
||||
// 配置数据库连接
|
||||
@@ -20,9 +22,6 @@ var connectionString = builder.Configuration.GetConnectionString("DefaultConnect
|
||||
builder.Services.AddDbContext<FutureMailDbContext>(options =>
|
||||
options.UseSqlite(connectionString));
|
||||
|
||||
// 配置OAuth 2.0认证
|
||||
// 注意:我们使用自定义中间件实现OAuth 2.0认证,而不是使用内置的JWT认证
|
||||
|
||||
// 配置Swagger/OpenAPI
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen(c =>
|
||||
@@ -40,13 +39,42 @@ builder.Services.AddScoped<IUserService, UserService>();
|
||||
builder.Services.AddScoped<IMailService, MailService>();
|
||||
builder.Services.AddScoped<ITimeCapsuleService, TimeCapsuleService>();
|
||||
builder.Services.AddScoped<IAuthService, AuthService>();
|
||||
builder.Services.AddScoped<IOAuthService, OAuthService>();
|
||||
builder.Services.AddScoped<IAIAssistantService, AIAssistantService>();
|
||||
builder.Services.AddScoped<IPersonalSpaceService, PersonalSpaceService>();
|
||||
builder.Services.AddScoped<IFileUploadService, FileUploadService>();
|
||||
builder.Services.AddScoped<INotificationService, NotificationService>();
|
||||
builder.Services.AddScoped<IOAuthService, OAuthService>();
|
||||
|
||||
builder.Services.AddScoped<IInitializationService, InitializationService>();
|
||||
|
||||
// 添加JWT认证
|
||||
var jwtSettings = builder.Configuration.GetSection("Jwt");
|
||||
var key = Encoding.ASCII.GetBytes(jwtSettings["Key"] ?? throw new InvalidOperationException("JWT密钥未配置"));
|
||||
|
||||
builder.Services.AddAuthentication(options =>
|
||||
{
|
||||
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
})
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
options.RequireHttpsMetadata = false;
|
||||
options.SaveToken = true;
|
||||
options.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(key),
|
||||
ValidateIssuer = true,
|
||||
ValidIssuer = jwtSettings["Issuer"],
|
||||
ValidateAudience = true,
|
||||
ValidAudience = jwtSettings["Audience"],
|
||||
ValidateLifetime = true,
|
||||
ClockSkew = TimeSpan.Zero
|
||||
};
|
||||
});
|
||||
|
||||
builder.Services.AddAuthorization();
|
||||
|
||||
// 配置Quartz任务调度
|
||||
builder.Services.AddQuartz(q =>
|
||||
{
|
||||
@@ -64,8 +92,11 @@ builder.Services.AddQuartz(q =>
|
||||
// 添加Quartz主机服务
|
||||
builder.Services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);
|
||||
|
||||
// 添加控制器
|
||||
builder.Services.AddControllers();
|
||||
// 添加控制器并注册OAuth认证过滤器为全局过滤器
|
||||
builder.Services.AddControllers(options =>
|
||||
{
|
||||
options.Filters.Add<OAuthAuthenticationFilter>();
|
||||
});
|
||||
|
||||
// 添加CORS
|
||||
builder.Services.AddCors(options =>
|
||||
@@ -106,10 +137,7 @@ app.UseStaticFiles(new StaticFileOptions
|
||||
|
||||
app.UseCors("AllowAll");
|
||||
|
||||
// 添加OAuth 2.0认证中间件
|
||||
app.UseMiddleware<OAuthAuthenticationMiddleware>();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user