feat: 优化预算控制器排序逻辑,修复除零错误;增强程序启动时的JWT认证配置
All checks were successful
Docker Build & Deploy / Build Docker Image (push) Successful in 26s
Docker Build & Deploy / Deploy to Production (push) Successful in 19s
Docker Build & Deploy / Cleanup Dangling Images (push) Successful in 1s
Docker Build & Deploy / WeChat Notification (push) Successful in 1s
All checks were successful
Docker Build & Deploy / Build Docker Image (push) Successful in 26s
Docker Build & Deploy / Deploy to Production (push) Successful in 19s
Docker Build & Deploy / Cleanup Dangling Images (push) Successful in 1s
Docker Build & Deploy / WeChat Notification (push) Successful in 1s
This commit is contained in:
@@ -19,7 +19,7 @@ public class BudgetController(
|
|||||||
.OrderBy(b => b.IsStopped)
|
.OrderBy(b => b.IsStopped)
|
||||||
.OrderBy(b => b.Category)
|
.OrderBy(b => b.Category)
|
||||||
.ThenBy(b => b.Type)
|
.ThenBy(b => b.Type)
|
||||||
.ThenByDescending(b => b.Current / b.Limit)
|
.ThenByDescending(b => b.Limit > 0 ? b.Current / b.Limit : 0)
|
||||||
.ThenBy(b => b.Name)
|
.ThenBy(b => b.Name)
|
||||||
.ToList()
|
.ToList()
|
||||||
.Ok();
|
.Ok();
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System.Text.Json.Serialization;
|
|
||||||
using FreeSql;
|
using FreeSql;
|
||||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Authorization;
|
||||||
using Microsoft.IdentityModel.Tokens;
|
using Microsoft.IdentityModel.Tokens;
|
||||||
using Scalar.AspNetCore;
|
using Scalar.AspNetCore;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
@@ -21,7 +22,14 @@ builder.Host.UseSerilog((context, loggerConfig) =>
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers(options =>
|
||||||
|
{
|
||||||
|
var policy = new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme)
|
||||||
|
.RequireAuthenticatedUser()
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
options.Filters.Add(new AuthorizeFilter(policy));
|
||||||
|
});
|
||||||
builder.Services.AddOpenApi();
|
builder.Services.AddOpenApi();
|
||||||
builder.Services.AddHttpClient();
|
builder.Services.AddHttpClient();
|
||||||
|
|
||||||
@@ -65,6 +73,21 @@ builder.Services.AddAuthentication(options =>
|
|||||||
IssuerSigningKey = new SymmetricSecurityKey(key),
|
IssuerSigningKey = new SymmetricSecurityKey(key),
|
||||||
ClockSkew = TimeSpan.Zero
|
ClockSkew = TimeSpan.Zero
|
||||||
};
|
};
|
||||||
|
options.Events = new JwtBearerEvents
|
||||||
|
{
|
||||||
|
OnChallenge = async context =>
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
|
||||||
|
context.Response.ContentType = "application/json";
|
||||||
|
await context.Response.WriteAsJsonAsync(BaseResponse.Fail("未登录"));
|
||||||
|
},
|
||||||
|
OnForbidden = async context =>
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = StatusCodes.Status403Forbidden;
|
||||||
|
context.Response.ContentType = "application/json";
|
||||||
|
await context.Response.WriteAsJsonAsync(BaseResponse.Fail("权限不足"));
|
||||||
|
}
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
builder.Services.AddAuthorization();
|
builder.Services.AddAuthorization();
|
||||||
|
|||||||
Reference in New Issue
Block a user