Commit 75e2aa23 authored by zhouxudong's avatar zhouxudong

日志记录

parent 52d580aa
......@@ -12,6 +12,8 @@ import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
......@@ -35,10 +37,16 @@ public class IpRule extends AbstractRule {
private static final String RATELIMITER_COUNT_PREFIX = "ratelimiter_request_count";
private static final String RATELIMITER_EXPIRATIONTIME_PREFIX = "ratelimiter_expirationtime";
private static final String RATELIMITER_HIT_CRAWLERSTRATEGY = "ratelimiter_hit_crawlerstrategy";
/**
* @description: 是否触发规则
* @date: 2024/4/10 16:37
* @param: [request, response]
* @return: boolean
**/
@Override
@SuppressWarnings("unchecked")
protected boolean doExecute(HttpServletRequest request, HttpServletResponse response) {
log.info("----------触发------------------");
String ipAddress = getIpAddr(request);
List<String> ignoreIpList = properties.getIpRule().getIgnoreIp();
if (ignoreIpList != null && ignoreIpList.size() > 0) {
......@@ -53,10 +61,11 @@ public class IpRule extends AbstractRule {
}
String requestUrl = request.getRequestURI();
requestUrl = dealUrl(requestUrl);
// 毫秒,默认5000
// 毫秒,默认5000 时间窗口长度(ms)
int expirationTime = properties.getIpRule().getExpirationTime();
// 最高expirationTime时间内请求数
int requestMaxSize = properties.getIpRule().getRequestMaxSize();
//请求url次数
RAtomicLong rRequestCount =
redissonClient.getAtomicLong(RATELIMITER_COUNT_PREFIX.concat(requestUrl).concat(ipAddress));
RAtomicLong rExpirationTime =
......@@ -72,6 +81,7 @@ public class IpRule extends AbstractRule {
// 触发爬虫策略 ,默认10天后可重新访问
long lockExpire = properties.getIpRule().getLockExpire();
rExpirationTime.expire(lockExpire, TimeUnit.SECONDS);
log.info("ipAddress:{},url信息:{}",ipAddress,request.getRequestURI());
// 保存触发来源
rHitMap.put(ipAddress, requestUrl);
LOGGER.info(
......@@ -103,7 +113,9 @@ public class IpRule extends AbstractRule {
*/
@Override
public void reset(HttpServletRequest request, String realRequestUri) {
log.info("-----------重置---------------");
String ipAddress = getIpAddr(request);
log.info("ipAddress:{},url:{}",ipAddress,request.getRequestURI());
String requestUrl = realRequestUri;
requestUrl = dealUrl(requestUrl);
/** 重置计数器 */
......@@ -118,22 +130,10 @@ public class IpRule extends AbstractRule {
rExpirationTime.expire(expirationTime, TimeUnit.MILLISECONDS);
/** 清除记录 */
RMap rHitMap = redissonClient.getMap(RATELIMITER_HIT_CRAWLERSTRATEGY);
rHitMap.remove(ipAddress);
log.info("rHitMap="+rHitMap.get(ipAddress));
Object remove = rHitMap.remove(ipAddress);
}
/* private static String getIpAddr(HttpServletRequest request) {
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;
}*/
@SneakyThrows
public static String getIpAddr(HttpServletRequest request) {
......@@ -167,12 +167,10 @@ public class IpRule extends AbstractRule {
// X-Real-IP:nginx服务代理
ipAddresses = request.getHeader("X-Real-IP");
}
// 有些网络通过多层代理,那么获取到的ip就会有多个,一般都是通过逗号(,)分割开来,并且第一个ip为客户端的真实IP
if (ipAddresses != null && ipAddresses.length() != 0) {
ip = ipAddresses.split(",")[0];
}
// 还是不能获取到,最后再通过request.getRemoteAddr();获取
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
ip = request.getRemoteAddr();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment