클라이언트 IP 체크시 헤더 확인

main
이범준 4 months ago
parent 150130b434
commit e8277af1cc

@ -21,6 +21,7 @@ import cokr.xit.base.user.service.SigunguDepartmentService;
import cokr.xit.base.user.service.UserService;
import cokr.xit.fims.base.FimsUser;
import cokr.xit.fims.base.service.bean.OgdpBean;
import cokr.xit.fims.cmmn.CmmnUtil;
import cokr.xit.fims.task.TaskRequestMappingHandlerMapping;
import cokr.xit.foundation.data.DataObject;
import cokr.xit.foundation.web.RequestHandlerReader;
@ -163,7 +164,9 @@ public class MainController extends cokr.xit.base.web.MainController {
@RequestMapping(name="접속자 IP 확인", value="/ipInfo.do")
public ModelAndView ipInfo(HttpServletRequest hreq) {
String remoteAddr = hreq.getRemoteAddr();
String remoteAddr = CmmnUtil.getClientIpAddr(hreq);
String net = "";
String LOCAL = "127.0.0.1";
String[] PRIVATES = {"10.","172.16.","172.31.","192.168."};

@ -27,6 +27,7 @@ import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FilenameUtils;
import org.springframework.core.io.ClassPathResource;
@ -606,4 +607,33 @@ public class CmmnUtil {
return mm * 2.83465f;
}
/**
* IP .
* @param request 릿
* @return IP
*/
public static String getClientIpAddr(HttpServletRequest request) {
String REGEXP_IPV4_ADDR = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
String[] headerNames = {"X-Forwarded-For","Proxy-Client-IP","WL-Proxy-Client-IP","HTTP_CLIENT_IP","HTTP_X_FORWARDED_FOR"};
String ip = "";
for(String headerName : headerNames) {
ip = request.getHeader(headerName);
if(ip != null && ip.contains(",")) {
ip = ip.split(",",-1)[0];
}
if(ip != null && Pattern.matches(REGEXP_IPV4_ADDR, ip)) {
break;
}
}
if(ip != null && ip.contains(",")) {
ip = ip.split(",",-1)[0];
}
if(ip == null || !Pattern.matches(REGEXP_IPV4_ADDR, ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
Loading…
Cancel
Save