刷新jwttoken

This commit is contained in:
不做码农 2023-08-29 07:40:56 +08:00
parent 52240ab6a8
commit 46c9563f33
4 changed files with 21 additions and 25 deletions

View File

@ -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}");

View File

@ -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)

View File

@ -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>

View File

@ -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