优化内容 统一了404的返回,并使其返回内容更可爱😊
This commit is contained in:
		
							parent
							
								
									5b77266b3a
								
							
						
					
					
						commit
						2e1b42de48
					
				@ -1,6 +1,7 @@
 | 
				
			|||||||
using AGSS.Models;
 | 
					using AGSS.Models;
 | 
				
			||||||
using AGSS.Models.Entities;
 | 
					using AGSS.Models.Entities;
 | 
				
			||||||
using AGSS.Models.Template;
 | 
					using AGSS.Models.Template;
 | 
				
			||||||
 | 
					using Microsoft.AspNetCore.Authorization;
 | 
				
			||||||
using Microsoft.AspNetCore.Mvc;
 | 
					using Microsoft.AspNetCore.Mvc;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace AGSS.Controllers.User;
 | 
					namespace AGSS.Controllers.User;
 | 
				
			||||||
@ -17,8 +18,10 @@ public class UserInfoController : ControllerBase
 | 
				
			|||||||
   }
 | 
					   }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   [HttpGet]
 | 
					   [HttpGet]
 | 
				
			||||||
 | 
					   [Authorize]
 | 
				
			||||||
   public async Task<IActionResult> My()
 | 
					   public async Task<IActionResult> My()
 | 
				
			||||||
   {
 | 
					   {
 | 
				
			||||||
 | 
					       
 | 
				
			||||||
       UserModel user = new UserModel
 | 
					       UserModel user = new UserModel
 | 
				
			||||||
       {
 | 
					       {
 | 
				
			||||||
           AuthId = "123"
 | 
					           AuthId = "123"
 | 
				
			||||||
 | 
				
			|||||||
@ -1,4 +1,5 @@
 | 
				
			|||||||
using AGSS.Models;
 | 
					using AGSS.Models;
 | 
				
			||||||
 | 
					using AGSS.Models.Template;
 | 
				
			||||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
 | 
					using Microsoft.AspNetCore.Authentication.JwtBearer;
 | 
				
			||||||
using Microsoft.EntityFrameworkCore;
 | 
					using Microsoft.EntityFrameworkCore;
 | 
				
			||||||
using Microsoft.IdentityModel.Tokens;
 | 
					using Microsoft.IdentityModel.Tokens;
 | 
				
			||||||
@ -21,9 +22,11 @@ var builder = WebApplication.CreateBuilder(args);
 | 
				
			|||||||
var domain = builder.Configuration["Auth0:Domain"];
 | 
					var domain = builder.Configuration["Auth0:Domain"];
 | 
				
			||||||
var audience =builder.Configuration["Auth0:Audience"];
 | 
					var audience =builder.Configuration["Auth0:Audience"];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//数据库配置(PGSQL)
 | 
				
			||||||
builder.Services.AddDbContext<DBContext>(opt =>
 | 
					builder.Services.AddDbContext<DBContext>(opt =>
 | 
				
			||||||
    opt.UseNpgsql(builder.Configuration.GetConnectionString("DBContext")));
 | 
					    opt.UseNpgsql(builder.Configuration.GetConnectionString("DBContext")));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//鉴权配置
 | 
				
			||||||
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
 | 
					builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
 | 
				
			||||||
        .AddJwtBearer(options =>
 | 
					        .AddJwtBearer(options =>
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
@ -37,6 +40,17 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
 | 
				
			|||||||
                ValidIssuer = domain,
 | 
					                ValidIssuer = domain,
 | 
				
			||||||
                ValidAudience = audience
 | 
					                ValidAudience = audience
 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
 | 
					            options.Events = new JwtBearerEvents
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                OnChallenge = context =>
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    context.HandleResponse();
 | 
				
			||||||
 | 
					                    context.Response.StatusCode = 200;
 | 
				
			||||||
 | 
					                    context.Response.ContentType = "application/json";
 | 
				
			||||||
 | 
					                    return context.Response.WriteAsJsonAsync(new ReturnTemplate(401,"你提供了一个错误的Token,所以我们无法验证你的身份,唔......",null));
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            };
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
builder.Services.AddAuthorization();
 | 
					builder.Services.AddAuthorization();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -49,7 +63,7 @@ builder.Services.AddSwaggerGen();
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
var app = builder.Build();
 | 
					var app = builder.Build();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Configure the HTTP request pipeline.
 | 
					// 配置Swagger
 | 
				
			||||||
if (app.Environment.IsDevelopment())
 | 
					if (app.Environment.IsDevelopment())
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    app.UseSwagger();
 | 
					    app.UseSwagger();
 | 
				
			||||||
@ -62,7 +76,23 @@ app.UseAuthentication();
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
app.UseAuthorization();
 | 
					app.UseAuthorization();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//自定义中间件 把404变成200
 | 
				
			||||||
 | 
					app.Use(async (context, next) =>
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    await next(); // 先执行后续中间件
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
 | 
					    // 如果响应是 404 且未修改过
 | 
				
			||||||
 | 
					    if (context.Response.StatusCode == 404 && !context.Response.HasStarted)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        context.Response.StatusCode = 200; // 改为 200
 | 
				
			||||||
 | 
					        context.Response.ContentType = "application/json";
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        // 自定义响应内容
 | 
				
			||||||
 | 
					        await context.Response.WriteAsJsonAsync(new ReturnTemplate(404,"未能找到资源吖!",null));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//控制器路由
 | 
				
			||||||
app.MapControllers();
 | 
					app.MapControllers();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
app.Run();
 | 
					app.Run();
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user