-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from mju-likelion/develop
로깅, HttpMediaTypeNotAcceptableException 핸들러 추가
- Loading branch information
Showing
5 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
FROM openjdk:17-jdk | ||
ARG JAR_FILE=build/libs/*.jar | ||
COPY ${JAR_FILE} app.jar | ||
ENV JAVA_OPTS="-Djava.net.preferIPv4Stack=true -Djava.net.preferIPv4Addresses=true" | ||
ENTRYPOINT ["java", "-Dspring.profiles.active=docker", "-Duser.timezone=Asia/Seoul", "-jar", "app.jar"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.mjulikelion.bagel.config; | ||
|
||
import org.mjulikelion.bagel.interceptor.CustomLoggingInterceptor; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
public class WebConfig implements WebMvcConfigurer { | ||
|
||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(new CustomLoggingInterceptor()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/main/java/org/mjulikelion/bagel/interceptor/CustomLoggingInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package org.mjulikelion.bagel.interceptor; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.net.Inet4Address; | ||
import java.net.UnknownHostException; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
@Slf4j | ||
public class CustomLoggingInterceptor implements HandlerInterceptor { | ||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) | ||
throws Exception { | ||
String ipAddress = getClientIP(request); | ||
String method = request.getMethod(); | ||
String uri = request.getRequestURI(); | ||
|
||
log.info("----- {} {} {} -----", ipAddress, method, uri); | ||
|
||
// 추가적인 로깅 | ||
logUserAgent(request); | ||
logQueryString(request); | ||
logRequestHeaders(request); | ||
|
||
log.info("----- Request end -----"); | ||
|
||
return true; | ||
} | ||
|
||
private String getClientIP(HttpServletRequest request) throws UnknownHostException { | ||
String ip = request.getHeader("X-Forwarded-For"); | ||
|
||
if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { | ||
ip = request.getHeader("Proxy-Client-IP"); | ||
} | ||
if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { | ||
ip = request.getHeader("WL-Proxy-Client-IP"); | ||
} | ||
if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { | ||
ip = request.getHeader("HTTP_CLIENT_IP"); | ||
} | ||
if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { | ||
ip = request.getHeader("HTTP_X_FORWARDED_FOR"); | ||
} | ||
if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { | ||
ip = request.getHeader("X-Real-IP"); | ||
} | ||
if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { | ||
ip = request.getHeader("X-RealIP"); | ||
} | ||
if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { | ||
ip = request.getHeader("REMOTE_ADDR"); | ||
} | ||
if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { | ||
ip = request.getRemoteAddr(); | ||
} | ||
log.info(String.valueOf(Inet4Address.getByName(ip))); | ||
|
||
return ip; | ||
} | ||
|
||
private void logUserAgent(HttpServletRequest request) { | ||
String userAgent = request.getHeader("User-Agent"); | ||
log.info("User-Agent: {}", userAgent); | ||
} | ||
|
||
private void logQueryString(HttpServletRequest request) { | ||
String queryString = request.getQueryString(); | ||
log.info("Query String: {}", queryString); | ||
} | ||
|
||
private void logRequestHeaders(HttpServletRequest request) { | ||
String contentType = request.getHeader("Content-Type"); | ||
String userAgent = request.getHeader("User-Agent"); | ||
log.info("Content-Type: {}", contentType); | ||
log.info("User-Agent: {}", userAgent); | ||
} | ||
} | ||
|