ip 호출 X-Forwarded-For 확인

This commit is contained in:
2026-01-22 20:18:17 +09:00
parent a46b14db7f
commit 88dbb48129
4 changed files with 32 additions and 14 deletions

View File

@@ -21,11 +21,10 @@ import org.springframework.web.context.request.ServletRequestAttributes;
@RequiredArgsConstructor @RequiredArgsConstructor
public class CustomAuthenticationProvider implements AuthenticationProvider { public class CustomAuthenticationProvider implements AuthenticationProvider {
private final MembersRepository membersRepository;
ServletRequestAttributes attr = ServletRequestAttributes attr =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
private final MembersRepository membersRepository;
@Override @Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException { public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String username = authentication.getName(); String username = authentication.getName();
@@ -61,7 +60,7 @@ public class CustomAuthenticationProvider implements AuthenticationProvider {
// front에서 전달한 사용자 ip 등록 // front에서 전달한 사용자 ip 등록
HttpServletRequest req = (attr != null) ? attr.getRequest() : null; HttpServletRequest req = (attr != null) ? attr.getRequest() : null;
String ip = (req != null) ? HeaderUtil.get(req, "kamco-userIp") : null; String ip = (req != null) ? HeaderUtil.get(req, "kamco-user-ip") : null;
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken token =
new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()); new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());

View File

@@ -35,6 +35,14 @@ public class ApiLogFunction {
return ip; return ip;
} }
public static String getXFowardedForIp(HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
if (ip != null) {
ip = ip.split(",")[0].trim();
}
return ip;
}
// 사용자 ID 추출 예시 (Spring Security 기준) // 사용자 ID 추출 예시 (Spring Security 기준)
public static String getUserId(HttpServletRequest request) { public static String getUserId(HttpServletRequest request) {
try { try {
@@ -53,15 +61,15 @@ public class ApiLogFunction {
return EventType.DOWNLOAD; return EventType.DOWNLOAD;
} }
if (uri.contains("/print")) { if (uri.contains("/print")) {
return EventType.PRINT; return EventType.OTHER;
} }
// 일반 CRUD // 일반 CRUD
return switch (method) { return switch (method) {
case "POST" -> EventType.CREATE; case "POST" -> EventType.ADDED;
case "GET" -> EventType.READ; case "GET" -> EventType.LIST;
case "DELETE" -> EventType.DELETE; case "DELETE" -> EventType.REMOVE;
case "PUT", "PATCH" -> EventType.UPDATE; case "PUT", "PATCH" -> EventType.MODIFIED;
default -> EventType.OTHER; default -> EventType.OTHER;
}; };
} }

View File

@@ -66,7 +66,7 @@ public class ApiResponseAdvice implements ResponseBodyAdvice<Object> {
if (body instanceof ApiResponseDto<?> apiResponse) { if (body instanceof ApiResponseDto<?> apiResponse) {
response.setStatusCode(apiResponse.getHttpStatus()); response.setStatusCode(apiResponse.getHttpStatus());
String ip = ApiLogFunction.getClientIp(servletRequest); String ip = ApiLogFunction.getXFowardedForIp(servletRequest);
Long userid = null; Long userid = null;
if (servletRequest.getUserPrincipal() instanceof UsernamePasswordAuthenticationToken auth if (servletRequest.getUserPrincipal() instanceof UsernamePasswordAuthenticationToken auth

View File

@@ -7,16 +7,27 @@ import lombok.Getter;
@Getter @Getter
@AllArgsConstructor @AllArgsConstructor
public enum EventType implements EnumType { public enum EventType implements EnumType {
CREATE("생성"), LIST("목록"),
READ("조회"), DETAIL("상세"),
UPDATE("수정"), POPUP("팝업"),
DELETE("삭제"), STATUS("상태"),
ADDED("추가"),
MODIFIED("수정"),
REMOVE("삭제"),
DOWNLOAD("다운로드"), DOWNLOAD("다운로드"),
PRINT("출력"), LOGIN("로그인"),
OTHER("기타"); OTHER("기타");
private final String desc; private final String desc;
public static EventType fromName(String name) {
try {
return EventType.valueOf(name.toUpperCase());
} catch (Exception e) {
return OTHER;
}
}
@Override @Override
public String getId() { public String getId() {
return name(); return name();