Skip to content

Commit 65068db

Browse files
committed
分离webapi,按需配置
1 parent 5aeee30 commit 65068db

9 files changed

Lines changed: 439 additions & 594 deletions

File tree

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@
2020
> 使用物联网卡通过udp下发指令时,存储的那个socket地址端口,有效期非常短,不速度快点下发,那个socket地址端口就可能映射到别的对应卡去了,所以此处采用跟随设备消息下发指令。
2121
2222
## 基于WebApi的消息业务处理程序
23+
默认不提供api,但如果使用webapi项目集成网关,如[JT808.Gateway.SimpleServer](simples/JT808.Gateway.SimpleServer/Program.cs#L45),可通过如下方式添加[自带的api](src/JT808.Gateway/JT808GatewayApiExtensions.cs)
2324

24-
使用webapi项目集成网关,如[simples/JT808.Gateway.SimpleServer/Program.cs](simples/JT808.Gateway.SimpleServer/Program.cs#L41)
25-
26-
**注意**:如果你使用nuget引用的方式,可能会由于控制器扫描规则影响而导致api不生效,因此需要调用`AddApplicationPart`
27-
2825
```c#
29-
builder.Services.AddControllers().AddApplicationPart(typeof(JT808WebApi).Assembly);
26+
app.UseJT808GatewayWebApi();
27+
```
28+
or
29+
```c#
30+
app.UseJT808GatewayWebApi(options=>{
31+
options.RoutePrefix = "jt808api"; //指定路由前缀
32+
options.VerifyAuthorization = true; //启用token鉴权
33+
});
3034
```
3135

3236
[接口文档](api/README.md)

api/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# 基于JT808Gateway WebApi 接口文档
22

3-
基地址:127.0.0.1:828/jt808api/
3+
基地址:127.0.0.1:5000
4+
路由前缀:jt808api(如需自定义请调用`UseJT808GatewayWebApi`时进行修改)
45

56
> 注意url格式
67

simples/JT808.Gateway.SimpleServer/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static void Main(string[] args)
4242
var app = builder.Build();
4343
app.UseRouting();
4444
app.MapControllers();
45+
app.UseJT808GatewayWebApi();
4546
app.Run();
4647
}
4748
}

src/JT808.Gateway/Authorization/JT808TokenAttribute.cs

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Threading.Tasks;
2+
using JT808.Gateway.Abstractions.Configurations;
3+
using JT808.Gateway.Abstractions.Dtos;
4+
using Microsoft.AspNetCore.Http;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Options;
7+
8+
namespace JT808.Gateway.Authorization;
9+
10+
/// <summary>
11+
/// 鉴权过滤器
12+
/// </summary>
13+
internal class JT808TokenFilter : IEndpointFilter
14+
{
15+
const string key = "token";
16+
17+
/// <inheritdoc/>
18+
public async ValueTask<object> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
19+
{
20+
var anticipatedToken = context.HttpContext.RequestServices.GetRequiredService<IOptions<JT808Configuration>>().Value.WebApiToken;
21+
22+
if ((context.HttpContext.Request.Query.TryGetValue(key, out var value) || context.HttpContext.Request.Headers.TryGetValue(key, out value)) && !string.IsNullOrEmpty(value) && value == anticipatedToken)
23+
{
24+
return await next(context);
25+
}
26+
27+
return Results.Ok(new JT808ResultDto<string>
28+
{
29+
Code = 401,
30+
Message = "auth error",
31+
Data = "auth error"
32+
});
33+
}
34+
}

src/JT808.Gateway/JT808.Gateway.xml

Lines changed: 25 additions & 101 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)