我似乎无法让CORS与我在a中添加的一些自定义中间件一起工作。net core 3.1 web api。
我发现问题是我已经实现的中间件,如果我注释掉中间件行,那么它可以工作,但当我把它们放进去时,它失败了:
app.UseMiddleware<ApiKeyMiddleware>(); // All request will need an ApiKey
app.UseMiddleware<UserTokenMiddleware>(); // All request but the /login and /swagger urls will need a UserToken
我需要做什么才能让它工作?
我的代码如下所示。cs文件
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ReminderContext>(options =>
{
options.UseSqlServer(this.Configuration.GetConnectionString("Reminder"));
});
services.AddCors();
services.AddControllers().AddJsonOptions(opt =>
{
// Added this to support enum strings coming into the api, without it they won't work
opt.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
services.AddSwaggerGen();
services.AddMediatR(typeof(Startup), typeof(LoginHandler));
// Using AddScoped means that the instance will be the same object for the whole request
// This is good and means the unit of work will exist for the full requests wherever it is used
// Unit of work
services.AddScoped<IUnitOfWork, UnitOfWork>();
// Add Repositories here
services.AddScoped<IUserRepository, UserRepository>();
// Add Business here
services.AddScoped<ILoginBusiness, LoginBusiness>();
services.AddScoped<IUserBusiness, UserBusiness>();
services.AddScoped<IBirthdayBusiness, BirthdayBusiness>();
services.AddSingleton<IPasswordUtility, PasswordUtility>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// Middleware to intercept requests and check the ApiKey and UserToken
app.UseMiddleware<ApiKeyMiddleware>(); // All request will need an ApiKey
app.UseMiddleware<UserTokenMiddleware>(); // All request but the /login and /swagger urls will need a UserTokena
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
//c.RoutePrefix = string.Empty;
});
app.UseHttpsRedirection();
app.UseRouting();
//var corsUrls = Configuration.GetSection("Cors").Get<string[]>();
app.UseCors(builder =>
{
builder.WithOrigins("http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
//app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
ApiKeyMiddleware如下所示:
public class ApiKeyMiddleware
{
private readonly RequestDelegate _next;
public ApiKeyMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context, IConfiguration configuration)
{
// Check headers that a header had been passed and it is value
if (!context.Request.Headers.Keys.Contains("ApiKey") ||
context.Request.Headers["ApiKey"] != configuration["Security:ApiKey"])
{
context.Response.StatusCode = 400; //Bad Request
await context.Response.WriteAsync("Invalid request");
return;
}
await _next.Invoke(context); // call next middleware
}
}
我将中间件行移到应用程序下方。UseCors(…)这条线现在似乎起作用了!
app.UseMiddleware<ApiKeyMiddleware>(); // All request will need an ApiKey
app.UseMiddleware<UserTokenMiddleware>(); // All request but the /login and /swagger urls will need a UserToken
实际上,我现在可以进入“招摇过市”页面,而在此之前,它也无法进入“招摇过市”页面。排序似乎总是令人困惑。
中间件是 WebHook 的一种变种模式,不同的是它早于 WebHook 执行,你可以做一些有用的前置拦截,比如 token 校验、日志记录等等。 实现一个中间件可以参考一个例子: public class BasicAuthMiddleware implements WebHook { private static final int AUTH_LENGTH = 6; priv
我有一个示例 anjularjs 示例,它拉取 sku 详细信息数组,在选择下拉列表中显示价格和名称。现在我在下拉列表中选择了任何选项,它显示SKU名称和价格。 下面是我的选择标签,现在正在显示,sku名称和价格。 以下是我的skulist数据: 所以在这里,我的要求是下拉列表应该包含sku名称和价格,但一旦选择了值,然后只显示sku名称。
我有一份CSV档案 1577,真,假,假,假,真 我试图用自定义模式加载csv文件, 但模式的可正确为null并没有按预期更改。
如何在不同的springboot控制器方法中使用不同的json序列化?假设我有 在一些控制器方法中,我只想以JSON的形式返回一个家伙的名字。在某些方法中,我希望以JSON的形式返回名称和年龄。在某些方法中,我只想以JSON的形式返回列表。注释POJO中的属性是没有意义的,因为每个方法都有不同的返回规范······
我想做这样的事情: 但不知道怎么做。有什么解决方案可以修复我下面的代码吗?
我修改了LoginController中的登录函数,这就是我所写的 这似乎工作得很好,而且正在工作,但是当我添加新的中间件时,它是并且我选中了