空箱补货
This commit is contained in:
@@ -24,19 +24,29 @@ namespace DOAN.Admin.WebApi.Filters
|
||||
/// <param name="context"></param>
|
||||
public void OnAuthorization(AuthorizationFilterContext context)
|
||||
{
|
||||
#region 首先检查当前操作是否标记了 AllowAnonymousAttribute 属性。如果有,则跳过授权检查
|
||||
|
||||
var noNeedCheck = false;
|
||||
if (context.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor)
|
||||
{
|
||||
noNeedCheck = controllerActionDescriptor.MethodInfo.GetCustomAttributes(inherit: true)
|
||||
.Any(a => a.GetType().Equals(typeof(AllowAnonymousAttribute)));
|
||||
.Any(a => a.GetType().Equals(typeof(AllowAnonymousAttribute)));
|
||||
}
|
||||
|
||||
if (noNeedCheck) return;
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region 获取客户端IP地址、请求路径、用户是否已认证以及操作系统类型。
|
||||
|
||||
string ip = HttpContextExtension.GetClientUserIp(context.HttpContext);
|
||||
string url = context.HttpContext.Request.Path;
|
||||
var isAuthed = context.HttpContext.User.Identity.IsAuthenticated;
|
||||
string osType = context.HttpContext.Request.Headers["os"];
|
||||
|
||||
#endregion
|
||||
|
||||
#region 刷新JWT令牌验证
|
||||
//使用jwt token校验2020-11-21
|
||||
TokenModel loginUser = JwtUtil.GetLoginUser(context.HttpContext);
|
||||
if (loginUser != null)
|
||||
@@ -44,7 +54,7 @@ namespace DOAN.Admin.WebApi.Filters
|
||||
var nowTime = DateTime.UtcNow;
|
||||
TimeSpan ts = loginUser.ExpireTime - nowTime;
|
||||
|
||||
//Console.WriteLine($"jwt到期剩余:{ts.TotalMinutes}分,{ts.TotalSeconds}秒");
|
||||
Console.WriteLine($"jwt到期剩余:{ts.TotalMinutes}分,{ts.TotalSeconds}秒");
|
||||
|
||||
var CK = "token_" + loginUser.UserId;
|
||||
if (!CacheHelper.Exists(CK) && ts.TotalMinutes < 5)
|
||||
@@ -61,12 +71,21 @@ namespace DOAN.Admin.WebApi.Filters
|
||||
context.HttpContext.Response.Headers.Add("X-Refresh-Token", newToken);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region 处理未认证的情况
|
||||
|
||||
if (loginUser == null || !isAuthed)
|
||||
{
|
||||
string msg = $"请求访问[{url}]失败,无法访问系统资源";
|
||||
//logger.Info(msg);
|
||||
context.Result = new JsonResult(ApiResult.Error(ResultCode.DENY, msg));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user