diff --git a/ZR.Admin.WebApi/Filters/VerifyAttribute.cs b/ZR.Admin.WebApi/Filters/VerifyAttribute.cs index 5e54c93..7ea4078 100644 --- a/ZR.Admin.WebApi/Filters/VerifyAttribute.cs +++ b/ZR.Admin.WebApi/Filters/VerifyAttribute.cs @@ -36,9 +36,24 @@ namespace ZR.Admin.WebApi.Filters var isAuthed = context.HttpContext.User.Identity.IsAuthenticated; //使用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}]失败,无法访问系统资源"; logger.Info($"{msg}"); diff --git a/ZR.Admin.WebApi/Framework/JwtUtil.cs b/ZR.Admin.WebApi/Framework/JwtUtil.cs index ad004f2..88423b8 100644 --- a/ZR.Admin.WebApi/Framework/JwtUtil.cs +++ b/ZR.Admin.WebApi/Framework/JwtUtil.cs @@ -1,13 +1,8 @@ -using JinianNet.JNTemplate; -using JinianNet.JNTemplate.Nodes; -using Microsoft.AspNetCore.SignalR; -using Microsoft.Extensions.Caching.Memory; -using Microsoft.IdentityModel.Tokens; +using Microsoft.IdentityModel.Tokens; using Newtonsoft.Json; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using System.Text; -using ZR.Admin.WebApi.Hubs; using ZR.Model.System.Dto; namespace ZR.Admin.WebApi.Framework @@ -137,21 +132,6 @@ namespace ZR.Admin.WebApi.Framework loginUser = JsonConvert.DeserializeObject(userData); 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; } catch (Exception ex) diff --git a/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj b/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj index fc07c89..870be2a 100644 --- a/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj +++ b/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj @@ -6,7 +6,7 @@ true - 1701;1702;1591,8603,8602,8604,8600 + 1701;1702;1591,8603,8602,8604,8600,8618 diff --git a/ZR.Admin.WebApi/appsettings.json b/ZR.Admin.WebApi/appsettings.json index 2e10095..17baa36 100644 --- a/ZR.Admin.WebApi/appsettings.json +++ b/ZR.Admin.WebApi/appsettings.json @@ -29,7 +29,8 @@ "Issuer": "ZRAdmin.NET", //即token的签发者。 "Audience": "ZRAdmin.NET", //指该token是服务于哪个群体的(群体范围) "SecretKey": "SecretKey-ZRADMIN.NET-20210101", - "Expire": 1440 //jwt登录过期时间(分) + "Expire": 1440, //jwt登录过期时间(分) + "refreshTokenTime": 5 }, "InjectClass": [ "ZR.Repository", "ZR.Service", "ZR.Tasks" ], //自动注入类 "ShowDbLog": true, //是否打印db日志