✨刷新jwttoken
This commit is contained in:
parent
52240ab6a8
commit
46c9563f33
@ -36,9 +36,24 @@ namespace ZR.Admin.WebApi.Filters
|
|||||||
var isAuthed = context.HttpContext.User.Identity.IsAuthenticated;
|
var isAuthed = context.HttpContext.User.Identity.IsAuthenticated;
|
||||||
|
|
||||||
//使用jwt token校验2020-11-21
|
//使用jwt token校验2020-11-21
|
||||||
LoginUser info = JwtUtil.GetLoginUser(context.HttpContext);
|
LoginUser loginUser = JwtUtil.GetLoginUser(context.HttpContext);
|
||||||
|
if (loginUser != null)
|
||||||
|
{
|
||||||
|
var nowTime = DateTime.UtcNow;
|
||||||
|
TimeSpan ts = loginUser.ExpireTime - nowTime;
|
||||||
|
|
||||||
if (info == null || !isAuthed)
|
//Console.WriteLine($"jwt到期剩余:{ts.TotalMinutes}分,{ts.TotalSeconds}秒");
|
||||||
|
|
||||||
|
var CK = "token_" + loginUser.UserId;
|
||||||
|
if (!CacheHelper.Exists(CK) && ts.TotalMinutes < 5)
|
||||||
|
{
|
||||||
|
var newToken = JwtUtil.GenerateJwtToken(JwtUtil.AddClaims(loginUser));
|
||||||
|
|
||||||
|
CacheHelper.SetCache(CK, CK, 1);
|
||||||
|
context.HttpContext.Response.Headers.Add("X-Refresh-Token", newToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (loginUser == null || !isAuthed)
|
||||||
{
|
{
|
||||||
string msg = $"请求访问[{url}]失败,无法访问系统资源";
|
string msg = $"请求访问[{url}]失败,无法访问系统资源";
|
||||||
logger.Info($"{msg}");
|
logger.Info($"{msg}");
|
||||||
|
|||||||
@ -1,13 +1,8 @@
|
|||||||
using JinianNet.JNTemplate;
|
using Microsoft.IdentityModel.Tokens;
|
||||||
using JinianNet.JNTemplate.Nodes;
|
|
||||||
using Microsoft.AspNetCore.SignalR;
|
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
|
||||||
using Microsoft.IdentityModel.Tokens;
|
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System.IdentityModel.Tokens.Jwt;
|
using System.IdentityModel.Tokens.Jwt;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using ZR.Admin.WebApi.Hubs;
|
|
||||||
using ZR.Model.System.Dto;
|
using ZR.Model.System.Dto;
|
||||||
|
|
||||||
namespace ZR.Admin.WebApi.Framework
|
namespace ZR.Admin.WebApi.Framework
|
||||||
@ -137,21 +132,6 @@ namespace ZR.Admin.WebApi.Framework
|
|||||||
loginUser = JsonConvert.DeserializeObject<LoginUser>(userData);
|
loginUser = JsonConvert.DeserializeObject<LoginUser>(userData);
|
||||||
loginUser.ExpireTime = jwtSecurityToken.ValidTo;
|
loginUser.ExpireTime = jwtSecurityToken.ValidTo;
|
||||||
}
|
}
|
||||||
//var nowTime = DateTime.UtcNow;
|
|
||||||
//TimeSpan ts = loginUser.ExpireTime - nowTime;
|
|
||||||
|
|
||||||
//Console.WriteLine("jwt到期时间:" + loginUser.ExpireTime);
|
|
||||||
//Console.WriteLine("nowTime" + nowTime + ",相隔" + ts.TotalSeconds);
|
|
||||||
|
|
||||||
//if (loginUser != null && ts.TotalSeconds <= 30)
|
|
||||||
//{
|
|
||||||
// var newToken = GenerateJwtToken(AddClaims(loginUser));
|
|
||||||
// var CK = "token_" + loginUser.UserId;
|
|
||||||
// if (!CacheHelper.Exists(CK))
|
|
||||||
// {
|
|
||||||
// CacheHelper.SetCache(CK, newToken);
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
return loginUser;
|
return loginUser;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||||
<NoWarn>1701;1702;1591,8603,8602,8604,8600</NoWarn>
|
<NoWarn>1701;1702;1591,8603,8602,8604,8600,8618</NoWarn>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@ -29,7 +29,8 @@
|
|||||||
"Issuer": "ZRAdmin.NET", //即token的签发者。
|
"Issuer": "ZRAdmin.NET", //即token的签发者。
|
||||||
"Audience": "ZRAdmin.NET", //指该token是服务于哪个群体的(群体范围)
|
"Audience": "ZRAdmin.NET", //指该token是服务于哪个群体的(群体范围)
|
||||||
"SecretKey": "SecretKey-ZRADMIN.NET-20210101",
|
"SecretKey": "SecretKey-ZRADMIN.NET-20210101",
|
||||||
"Expire": 1440 //jwt登录过期时间(分)
|
"Expire": 1440, //jwt登录过期时间(分)
|
||||||
|
"refreshTokenTime": 5
|
||||||
},
|
},
|
||||||
"InjectClass": [ "ZR.Repository", "ZR.Service", "ZR.Tasks" ], //自动注入类
|
"InjectClass": [ "ZR.Repository", "ZR.Service", "ZR.Tasks" ], //自动注入类
|
||||||
"ShowDbLog": true, //是否打印db日志
|
"ShowDbLog": true, //是否打印db日志
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user