🍒 refactor(sysAuth): 优化 token 黑名单处理逻辑
This commit is contained in:
parent
22f299bb1e
commit
2d8e8a62e7
@ -355,13 +355,14 @@ public class SysAuthService : IDynamicApiController, ITransient
|
||||
{
|
||||
var httpContext = _httpContextAccessor.HttpContext ?? throw Oops.Oh(ErrorCodeEnum.D1016);
|
||||
|
||||
var accessToken = httpContext.Request.Headers.Authorization.ToString();
|
||||
if (string.IsNullOrWhiteSpace(accessToken) || string.IsNullOrWhiteSpace(_userManager.Account))
|
||||
var userId = httpContext.User.FindFirst(ClaimConst.UserId)?.Value;
|
||||
var version = httpContext.User.FindFirst(ClaimConst.TokenVersion)?.Value;
|
||||
if (string.IsNullOrWhiteSpace(userId) || string.IsNullOrWhiteSpace(version) || string.IsNullOrWhiteSpace(_userManager.Account))
|
||||
throw Oops.Oh(ErrorCodeEnum.D1011);
|
||||
|
||||
// 写入Token黑名单
|
||||
var tokenExpire = await _sysConfigService.GetTokenExpire();
|
||||
_sysCacheService.Set($"{CacheConst.KeyTokenBlacklist}:{accessToken}", _userManager.Account, TimeSpan.FromMinutes(tokenExpire));
|
||||
_sysCacheService.Set($"{CacheConst.KeyTokenBlacklist}:{userId}:{version}", _userManager.Account, TimeSpan.FromMinutes(tokenExpire));
|
||||
|
||||
// 发布系统退出事件
|
||||
await _eventPublisher.PublishAsync(UserEventTypeEnum.Logout, _userManager);
|
||||
|
||||
@ -60,9 +60,9 @@ namespace Admin.NET.Web.Core
|
||||
}
|
||||
|
||||
// 验证Token黑名单
|
||||
var accessToken = httpContext.Request.Headers.Authorization.ToString();
|
||||
var userId = httpContext.User.FindFirst(ClaimConst.UserId)?.Value;
|
||||
if (sysCacheService.ExistKey($"{CacheConst.KeyTokenBlacklist}:{accessToken}") || !sysCacheService.ExistKey($"{CacheConst.KeyUserSession}{userId}"))
|
||||
var version = httpContext.User.FindFirst(ClaimConst.TokenVersion)?.Value;
|
||||
if (sysCacheService.ExistKey($"{CacheConst.KeyTokenBlacklist}:{userId}:{version}") || !sysCacheService.ExistKey($"{CacheConst.KeyUserSession}{userId}"))
|
||||
{
|
||||
context.Fail(new AuthorizationFailureReason(this, "令牌已失效,请重新登录。"));
|
||||
context.StatusCode(StatusCodes.Status401Unauthorized);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user