houzhongyi
2024-07-11 e7c1260db32209a078a962aaa0ad5492c35774fb
提交 | 用户 | 时间
e7c126 1 package com.iailab.framework.ratelimiter.core.keyresolver.impl;
H 2
3 import cn.hutool.core.util.StrUtil;
4 import cn.hutool.crypto.SecureUtil;
5 import com.iailab.framework.ratelimiter.core.annotation.RateLimiter;
6 import com.iailab.framework.ratelimiter.core.keyresolver.RateLimiterKeyResolver;
7 import com.iailab.framework.web.core.util.WebFrameworkUtils;
8 import org.aspectj.lang.JoinPoint;
9
10 /**
11  * 用户级别的限流 Key 解析器,使用方法名 + 方法参数 + userId + userType,组装成一个 Key
12  *
13  * 为了避免 Key 过长,使用 MD5 进行“压缩”
14  *
15  * @author iailab
16  */
17 public class UserRateLimiterKeyResolver implements RateLimiterKeyResolver {
18
19     @Override
20     public String resolver(JoinPoint joinPoint, RateLimiter rateLimiter) {
21         String methodName = joinPoint.getSignature().toString();
22         String argsStr = StrUtil.join(",", joinPoint.getArgs());
23         Long userId = WebFrameworkUtils.getLoginUserId();
24         Integer userType = WebFrameworkUtils.getLoginUserType();
25         return SecureUtil.md5(methodName + argsStr + userId + userType);
26     }
27
28 }