JWT 自动续期

当前为单体架构 ,如果是分布式架构则需用分布式缓存

1.创建一个全局缓存,登录的时候写入

2.关闭jwt自带的时间校验 使用自定义的校验  

(OnTokenValidated 更新最后一次操作时间

OnMessageReceived 判断是否过期,删除与否

)


public static ConcurrentDictionary<string, DateTime> _TokenManager = new ConcurrentDictionary<string, DateTime>();
    public static void Configure(IServiceCollection services, IConfiguration configuration)
        {
            if (bool.Parse(configuration["Authentication:JwtBearer:IsEnabled"]))
            {
                services.AddAuthentication(options =>
                {
                    options.DefaultAuthenticateScheme = "JwtBearer";
                    options.DefaultChallengeScheme = "JwtBearer";
                }).AddJwtBearer("JwtBearer", options =>
                {
                    options.Audience = configuration["Authentication:JwtBearer:Audience"];

                    options.TokenValidationParameters = new TokenValidationParameters
                    {
                        // The signing key must match!
                        ValidateIssuerSigningKey = true,
                        IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(configuration["Authentication:JwtBearer:SecurityKey"])),

                        // Validate the JWT Issuer (iss) claim
                        ValidateIssuer = true,
                        ValidIssuer = configuration["Authentication:JwtBearer:Issuer"],

                        // Validate the JWT Audience (aud) claim
                        ValidateAudience = true,
                        ValidAudience = configuration["Authentication:JwtBearer:Audience"],

                         Validate the token expiry
                        ValidateLifetime = false,
                        // If you want to allow a certain amount of clock drift, set that here
                        ClockSkew = TimeSpan.Zero
                    };

                    options.Events = new JwtBearerEvents
                    {
                        OnTokenValidated = context =>
                        {
                            var _TokenManager = TokenAuthController._TokenManager;
                            var requestToken = getToken(context.HttpContext.Request); //context.HttpContext.Request.Headers.Where(c => c.Key == "Authorization").FirstOrDefault().Value.ToString() ?? "";
                            _TokenManager[requestToken] = DateTime.Now;
                            return Task.CompletedTask;
                        },

                        OnMessageReceived = context =>
                        {
                            if (!context.HttpContext.Request.Headers.Any(c => c.Key == "Authorization"))
                                return Task.CompletedTask;


                            var _TokenManager = TokenAuthController._TokenManager;
                            var requestToken = getToken(context.HttpContext.Request);// context.HttpContext.Request.Headers.Where(c => c.Key == "Authorization").FirstOrDefault().Value.ToString() ?? "";
                            if (!_TokenManager.Where(c => c.Key == requestToken).Any())
                            {
                                context.Response.Headers.Add("Access-Control-Expose-Headers", "Token-Expired");
                                context.Response.Headers.Add("Token-Expired", "true");
                                context.Fail(new Exception("Token有效期已过,已删除"));
                                return Task.CompletedTask;
                            }

                            var currToken = _TokenManager.Where(c => c.Key == requestToken).FirstOrDefault();
                            //距离最后一次操作间隔时长
                            var timeSpan = GetTimeSpan(DateTime.Now, currToken.Value);
                            //Token expired
                            if ((timeSpan > 1))
                            {
                                context.Response.Headers.Add("Access-Control-Expose-Headers", "Token-Expired");
                                context.Response.Headers.Add("Token-Expired", "true");
                                _TokenManager.TryRemove(currToken);
                                context.Fail(new Exception("Token过期"));

                                //清空废弃的数据
                                var removeItems = _TokenManager.Where(c => GetTimeSpan(DateTime.Now, c.Value) > 60).ToList();
                                foreach (var item in removeItems)
                                    _TokenManager.TryRemove(item);
                            }
                            return Task.CompletedTask;
                        },
                    };
                });
            }


        }



        private static string getToken(HttpRequest Request1)
        {
            var requestToken = Request1.Headers.Where(c => c.Key == "Authorization").FirstOrDefault().Value.ToString() ?? "";
            var t = requestToken.Replace("Bearer ", "").Replace("bearer ", "");
            return t;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值