진이의 Developer Story
스프링에서 클라이언트IP 가져오기 본문
포워드IP의 경우 제대로 걸러지지 않는 경우가 있다.
헤더에서 구분하여 IP값을 받아오자.
public static String getClientIP() {
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).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.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
// ipv6 (ipv4 127.0.0.1) localhost로 접속했을시
if("0:0:0:0:0:0:0:1".equals(ip)){
ip = "127.0.0.1";
}
return ip;
}
'Java > Spring' 카테고리의 다른 글
STS + Spring boot 환경에서 Hot swapping 적용하기 (1) | 2019.07.01 |
---|---|
아프리카 tv 별풍선 수집기 (0) | 2018.02.12 |
Spring Quartz Autowired (0) | 2017.10.25 |
예전에 만들어두었던 롤 전적검색사이트 (2) | 2017.01.03 |
@ControllerAdvice를 이용한 커스텀 에러 처리 (0) | 2016.12.04 |
Comments