taptap/ratelimiter-spring-boot-starter

使用过程中的问题和经验,供大家参考

Opened this issue · 0 comments

  1. redis如果是无密码,配置文件必须将密码字段不能使用空字符串字段,会导致redis无法连接。

  2. 想要使用ip作为key,就会比较麻烦。customKeyFunction只能获取当前实例的方法且必须方法名称与传入参数一致,对于多种不同参数请求,就比较麻烦了。最后使用了keys = "T(hy.jtt808.ratelimit.RequestUtils).getClientIP()"
    `
    public class RequestUtils {

    public static String getClientIP() {
    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    if (attributes == null) {
    return "UNKNOWN";
    }

     HttpServletRequest request = attributes.getRequest();
     String ip = request.getHeader("X-Forwarded-For");
     if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
         ip = request.getHeader("Proxy-Client-IP");
     }
     if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
         ip = request.getHeader("WL-Proxy-Client-IP");
     }
     if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
         ip = request.getRemoteAddr();
     }
     return ip;
    

    }
    }
    `