dengzedong
2024-12-25 3f225f1c996024b4c4d3c717cd9149455f5e099f
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
package com.iailab.framework.ratelimiter.core.keyresolver.impl;
 
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.iailab.framework.ratelimiter.core.annotation.RateLimiter;
import com.iailab.framework.ratelimiter.core.keyresolver.RateLimiterKeyResolver;
import com.iailab.framework.web.core.util.WebFrameworkUtils;
import org.aspectj.lang.JoinPoint;
 
/**
 * 用户级别的限流 Key 解析器,使用方法名 + 方法参数 + userId + userType,组装成一个 Key
 *
 * 为了避免 Key 过长,使用 MD5 进行“压缩”
 *
 * @author iailab
 */
public class UserRateLimiterKeyResolver implements RateLimiterKeyResolver {
 
    @Override
    public String resolver(JoinPoint joinPoint, RateLimiter rateLimiter) {
        String methodName = joinPoint.getSignature().toString();
        String argsStr = StrUtil.join(",", joinPoint.getArgs());
        Long userId = WebFrameworkUtils.getLoginUserId();
        Integer userType = WebFrameworkUtils.getLoginUserType();
        return SecureUtil.md5(methodName + argsStr + userId + userType);
    }
 
}