From 16386d20ff8e00a711c0f5e410243309d14f205a Mon Sep 17 00:00:00 2001 From: curry772 Date: Mon, 30 Mar 2026 15:57:04 +0900 Subject: [PATCH] =?UTF-8?q?Block=20IP=20=ED=95=84=ED=84=B0=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../filter/AdapterBlockIpListFilter.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/main/java/com/eactive/eai/adapter/http/dynamic/filter/AdapterBlockIpListFilter.java diff --git a/src/main/java/com/eactive/eai/adapter/http/dynamic/filter/AdapterBlockIpListFilter.java b/src/main/java/com/eactive/eai/adapter/http/dynamic/filter/AdapterBlockIpListFilter.java new file mode 100644 index 0000000..9543ce5 --- /dev/null +++ b/src/main/java/com/eactive/eai/adapter/http/dynamic/filter/AdapterBlockIpListFilter.java @@ -0,0 +1,60 @@ +package com.eactive.eai.adapter.http.dynamic.filter; + +import java.util.Properties; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.http.HttpStatus; + +import com.eactive.eai.adapter.http.HttpStatusException; +import com.eactive.eai.common.util.Logger; +import com.eactive.eai.util.IpUtil; + + +public class AdapterBlockIpListFilter implements HttpAdapterFilter { + static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER); + + @Override + public Object doPreFilter(String adptGrpName, String adptName, Object message, Properties prop, + HttpServletRequest request, HttpServletResponse response) throws Exception { + + String requestIp = getClientIp(request); + try { + String blockIps = prop.getProperty("BLOCK_IP", ""); + if (StringUtils.isBlank(blockIps)) { + return message; + } + + if (IpUtil.isMatchIp(blockIps, requestIp)) { + throw new HttpStatusException("This IP was not allowed-" + requestIp, HttpStatus.FORBIDDEN.value()); + } + } catch (HttpStatusException e) { + logger.debug(e.getMessage()); + throw e; + } catch (Exception e) { + logger.error(e.getMessage()); + throw new JwtAuthException(JwtAuthFilter.ERROR_AUTHORIZATION_FAIL, "Client IP check failed-" + requestIp); + } + + return message; + } + + private String getClientIp(HttpServletRequest request) { + String ipAddress = request.getHeader("X-FORWARDED-FOR"); + if (ipAddress == null) { + ipAddress = request.getRemoteAddr(); + } + if(ipAddress.indexOf(",") >= 0) { + ipAddress = org.springframework.util.StringUtils.tokenizeToStringArray(ipAddress, ",")[0]; + } + return ipAddress; + } + + @Override + public Object doPostFilter(String adptGrpName, String adptName, Object resultMessage, Properties prop, + HttpServletRequest request, HttpServletResponse response) throws Exception { + return resultMessage; + } +}