https://github.com/stulzq/urlfirewall
UrlFirewall is a lightweight, fast filtering middleware for http request urls.It supports blacklist, whitelist mode.Supports persisting filter rules to any media.You can use it in webapi, gateway, etc.
https://github.com/stulzq/urlfirewall
url-filter url-firewall webapi
Last synced: 3 months ago
JSON representation
UrlFirewall is a lightweight, fast filtering middleware for http request urls.It supports blacklist, whitelist mode.Supports persisting filter rules to any media.You can use it in webapi, gateway, etc.
- Host: GitHub
- URL: https://github.com/stulzq/urlfirewall
- Owner: stulzq
- License: mit
- Created: 2018-05-03T10:02:57.000Z (about 8 years ago)
- Default Branch: dev
- Last Pushed: 2020-11-18T14:06:36.000Z (over 5 years ago)
- Last Synced: 2025-10-25T10:38:03.812Z (8 months ago)
- Topics: url-filter, url-firewall, webapi
- Language: C#
- Homepage:
- Size: 20.5 KB
- Stars: 66
- Watchers: 1
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.nuget.org/packages/UrlFirewall.AspNetCore/)
[](https://github.com/stulzq/UrlFirewall/blob/master/LICENSE)   
# UrlFirewall
UrlFirewall is a lightweight, fast filtering middleware for http request urls.It supports blacklist, whitelist mode.Supports persisting filter rules to any media.You can use it in webapi, gateway, etc.
## Language
English(Current) | [中文](http://www.cnblogs.com/stulzq/p/8987632.html)
## Get start
### 1.Install from Nuget to your Asp.Net Core project:
````shell
Install-Package UrlFirewall.AspNetCore
````
### 2.Configure DI
````csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddUrlFirewall(options =>
{
options.RuleType = UrlFirewallRuleType.Black;
options.SetRuleList(Configuration.GetSection("UrlBlackList"));
options.StatusCode = HttpStatusCode.NotFound;
});
services.AddMvc();
//...
}
````
### 3.Configure url firewall middleware.
>The order of middleware must be at the top most.
````csharp
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//Configure url firewall middleware. Top most.
app.UseUrlFirewall();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
````
### 4.Configure rule
In appsettings.json/appsettings.Devolopment.json create a section.
````json
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"UrlBlackList": [
{
"Url": "/api/cart/add",
"Method": "All"
},
{
"Url": "/api/cart/del",
"Method": "Post"
},
{
"Url": "/api/cart/list",
"Method": "Get"
},
{
"Url": "/api/product/*",
"Method": "All"
}
]
}
````
The url field is the http request path we need to match.It supports wildcard `*` and `?`.The `*` represents an arbitrary number of arbitrary characters. The `?` representative matches any one arbitrary character
### 5.End
Now,you access `/api/cart/add` etc.Will be get 404.Enjoy yourself.
## Extensibility
If you want to implement validation logic yourself, or You want to verify by getting data from the database, redis etc.You can implement the `IUrlFirewallValidator` interface.Then you can replace the default implementation with the `AddUrlFirewallValidator` method.
e.g:
````csharp
services.AddUrlFirewall(options =>
{
options.RuleType = UrlFirewallRuleType.Black;
options.SetRuleList(Configuration.GetSection("UrlBlackList"));
options.StatusCode = HttpStatusCode.NotFound;
}).AddUrlFirewallValidator();
````