集成方式授权和自带的授权只能二选一
如你的项目已存在了JWT这个时候你才用集成方式授权(比如用了Furion 等框架有了授权了)
如果你项目中没有JWT你可以用 自带的授权 (比如新项目什么授权没有)
适用于项目已存在JWT授权
//注册ReZero.Api builder.Services.AddReZeroServices(api => { var apiObj = new SuperAPIOptions(); ....省略.... //只看这一行 apiObj.InterfaceOptions = new InterfaceOptions() { SuperApiAop=new MyAop() }; api.EnableSuperApi(apiObj); }); //新建一个AOP类 public class MyAop : DefaultSuperApiAop { public override Task OnExecutingAsync(InterfaceContext context) { //可以拿httpContext对象 var httpConetxt = context.HttpContext; //可以自个修改,只要有HttpContext就能拿到授权信息 var claims = DependencyResolver.GetClaims();//获取Claims if (claims == null) { httpConetxt!.Response.StatusCode = 401;//设置401状态 throw new Exception("未授权"); } //循环将上面的claims通过下面方法到加上下文 foreach (var item in claims.Claims) { context.AttachClaimToHttpContext(item.Type, item.Value); } return base.OnExecutingAsync(context); } }
只要请求的接口带上token就行了
所有请求都会进 AOP,打个断点就行了
2016 © donet5.comApache Licence 2.0