移动系统liao
2025-05-01 a247547df86f0fad8f03aebb91de68d3f2bc7918
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
 
using EzCoreNet.Redis;
using Furion;
using Furion.FriendlyException;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
 
namespace Cylsg.Filter
{
 
    public enum Limttype
    {
        /// <summary>
        ///共用限流
        /// </summary>
        All,
        /// <summary>
        /// 用户级限流
        /// </summary>
       User
    }
 
    /// <summary>
    /// 限流
    /// </summary>
    
    public class LimitFilterAttribute : Attribute,IAsyncActionFilter
    {
 
        /// <summary>
        /// redis限流文件夹名称
        /// </summary>
        private const string DirKey = "UserRateLimit:";
        /// <summary>
        /// 限流发生时 返回的提示
        /// </summary>
        public string ResponseMeg { get; set; } = "请不要在短时间内重复访问该资源";
 
        /// <summary>
        ///  限流时长 单位秒  默认为1秒
        /// </summary>
        public int  timespan { get; set; } = 1;
        /// <summary>
        /// 限流次数 默认为1次
        /// </summary>
        public int InCount { get; set; } = 1;
 
        /// <summary>
        /// 默认为用户级
        /// </summary>
        public Limttype LimiType { get; set; }= Limttype.All;
 
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
         var redisHelper =    App.GetService<IEzCoreNetRedisService>();
            if(redisHelper == null)
            {
                throw Oops.Oh("内存服务异常,请求终止");
            }
 
            var path = context.HttpContext?.Request?.Path.Value;
            if (string.IsNullOrEmpty(path))
            {
                //非API控制器,直接放行  没有找到路径
                await next.Invoke();
                return;
            }
            string key = "";
            if (LimiType == Limttype.User)
            {
                if (App.User?.FindFirstValue("UserID") == null)
                {
                    //没有用户直接放行
                    await next.Invoke();
                    return;
                }
 
                 key = $"{DirKey}:{path}:{App.User?.FindFirstValue("UserID")}";
            }
            else
                if(LimiType == Limttype.All)
            {
                key = $"{DirKey}:{path}:All";
            }
            else
            {
                await next.Invoke();
                return;
            }
 
 
            long? count = redisHelper.Get<int>(key);
            if (count == null || count == 0)
            {
                redisHelper.Incrby(key);
                redisHelper.SetTtl(key, timespan);
            }
            else
            if (count >= (InCount))
            {
                throw Oops.Oh(ResponseMeg);
            
            }
            else
            {
                redisHelper.Incrby(key);
            }
 
            await next.Invoke();
 
           
        }
    }
}