修复 新增 token失效检查

This commit is contained in:
小肥羊 2025-11-14 18:50:49 +08:00
parent b0d9ed1d6f
commit eae4e36349
4 changed files with 34 additions and 6 deletions

View File

@ -77,7 +77,9 @@ namespace Learn.Archives.API.Controllers
[
new Claim(ClaimEnum.UserCenterRole,"1"),//让所有用户都有用户中心操作权限
new Claim(ClaimEnum.Role,admin.RoleId.ToString()),
new Claim(ClaimEnum.UserId,admin.RoleId.ToString()),
new Claim(ClaimEnum.UserId,admin.Id.ToString()),
new Claim(ClaimEnum.UserId,admin.Id.ToString()),
new Claim(ClaimEnum.Scope,"档案系统"),
new Claim(ClaimEnum.Id, admin.Id.ToString()),
new Claim(ClaimEnum.Name, admin.Name),
])
@ -122,7 +124,7 @@ namespace Learn.Archives.API.Controllers
}
/// <summary>
/// 导入考试信息
/// 导入用户信息
/// </summary>
/// <returns></returns>
[HttpPost, ResultIgnore]

View File

@ -21,7 +21,7 @@ using Learn.Archives.Core.Common;
using Learn.Archives.Core.Model.Dto;
using Learn.Archives.Core.Model;
using SqlSugar.IOC;
using static System.Net.Mime.MediaTypeNames;
using Microsoft.AspNetCore.Authorization;
namespace Learn.Archives.API.Expand
{
@ -204,7 +204,7 @@ namespace Learn.Archives.API.Expand
Url = context.Request.Path + context.Request.QueryString,
Method = context.Request.Method,
Request = request,
IP = context.Connection?.RemoteIpAddress?.ToString(),
IP = $"{userInfo.Scope} {context.Connection?.RemoteIpAddress?.ToString()}",
ResponseCode = result?.Code ?? -1,
Response = (result != null ? JsonSerializer.Serialize(result) : null) ,
Authorization = context.Request.Headers.ContainsKey("Authorization")
@ -220,6 +220,15 @@ namespace Learn.Archives.API.Expand
public override async void OnActionExecuting(ActionExecutingContext context)
{
// 直接返回原始结果,不封装
if (context.HttpContext.GetEndpoint()?
.Metadata.GetMetadata<IAllowAnonymous>() is null
&& string.IsNullOrEmpty(userInfo.Scope))
{
//过期的
context.Result = new UnauthorizedResult();
return;
}
Executing400(context);
@ -241,8 +250,7 @@ namespace Learn.Archives.API.Expand
catch (Exception ex)
{
}
//添加http请求日志
base.OnActionExecuted(context);
}
/// <summary>

View File

@ -58,5 +58,13 @@ namespace Learn.Archives.Core.Common
{
get => _httpContextAccessor.HttpContext?.User.FindFirst(ClaimEnum.Name)?.Value??string.Empty;
}
/// <summary>
/// Scope
/// </summary>
public string Scope
{
get => _httpContextAccessor.HttpContext?.User.FindFirst(ClaimEnum.Scope)?.Value ?? string.Empty;
}
}
}

View File

@ -38,6 +38,16 @@ namespace Learn.Archives.Core.Common
/// <param name="code"></param>
/// <exception cref="OhException"></exception>
public static void ModelError(string message, int code = 400)
{
throw new OhException(message, code);
}
/// <summary>
/// 抛出 模型校验异常
/// </summary>
/// <param name="message"></param>
/// <param name="code"></param>
/// <exception cref="OhException"></exception>
public static void ToeknError(string message, int code = 401)
{
throw new OhException(message, code);
}