Skip to content

Commit

Permalink
Feat(#171-project): 커스텀 어노테이션 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
mingeun0507 committed May 25, 2023
1 parent 5844c69 commit 140a3eb
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/main/java/com/_8attery/seesaw/annotation/ClientIp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com._8attery.seesaw.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface ClientIp {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com._8attery.seesaw.config.resolver;

import com._8attery.seesaw.annotation.ClientIp;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;

@Component
public class ClientIpResolver implements HandlerMethodArgumentResolver {
private static final String[] IP_HEADER_CANDIDATES = {
"X-Forwarded-For",
"Proxy-Client-IP",
"WL-Proxy-Client-IP",
"HTTP_X_FORWARDED_FOR",
"HTTP_X_FORWARDED",
"HTTP_X_CLUSTER_CLIENT_IP",
"HTTP_CLIENT_IP",
"HTTP_FORWARDED_FOR",
"HTTP_FORWARDED",
"HTTP_VIA",
"REMOTE_ADDR"
};

@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.hasParameterAnnotation(ClientIp.class);
}

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();

for (String header : IP_HEADER_CANDIDATES) {
String ip = request.getHeader(header);

if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
return ip;
}
}

return request.getRemoteAddr();
}
}
2 changes: 1 addition & 1 deletion src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<configuration>
<!-- 변수 설정 -->
<property name="LOG_DIR_SERVER" value="/var/log"/>
<property name="LOG_DIR_SERVER" value="./var/log"/>
<property name="LOG_FILE" value="logfile.log"/>
<property name="LOG_FILE_PROD" value="${LOG_DIR_SERVER}/${LOG_FILE}"/>
<property name="LOG_FILE_LOCAL" value="./log/${LOG_FILE}"/>
Expand Down

0 comments on commit 140a3eb

Please sign in to comment.