cn.flood.cloud.gateway.service.impl.SafeRuleServiceImpl Maven / Gradle / Ivy
package cn.flood.cloud.gateway.service.impl;
import cn.flood.Func;
import cn.flood.cloud.gateway.service.SafeRuleService;
import cn.flood.cloud.rule.constant.RuleConstant;
import cn.flood.cloud.rule.entity.BlackList;
import cn.flood.cloud.rule.service.RuleCacheService;
import cn.flood.date.DateTimeUtils;
import cn.flood.http.IPUtils;
import com.google.common.base.Stopwatch;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Service;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.net.URI;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* 安全规则业务实现类
*
* @author mmdai
*/
@Service
@RequiredArgsConstructor
public class SafeRuleServiceImpl implements SafeRuleService {
private final Logger log = LoggerFactory.getLogger(this.getClass());
private final AntPathMatcher antPathMatcher = new AntPathMatcher();
@Autowired
private RuleCacheService ruleCacheService;
@Override
public Mono filterBlackList(ServerWebExchange exchange) {
Stopwatch stopwatch = Stopwatch.createStarted();
ServerHttpRequest request = exchange.getRequest();
ServerHttpResponse response = exchange.getResponse();
try {
URI originUri = getOriginRequestUri(exchange);
String requestIp = IPUtils.getServerHttpRequestIpAddress(request);
String requestMethod = request.getMethodValue();
AtomicBoolean forbid = new AtomicBoolean(false);
// 从缓存中获取黑名单信息
Set