Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Getter
public enum Restaurant {

DODAM("DODAM", 7000),
DODAM("DODAM", 6000),
DORMITORY("DORMITORY", 5500),
FOOD_COURT("FOOD_COURT", null),
SNACK_CORNER("SNACK_CORNER", null),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static String sendServerError(Throwable ex, String method, String uri, St
- HTTP Method: {4}
- URI: {5}
- User ID: {6}
- 요청 파라미터: {7}
- 로그 내용 (파라미터 정보 포함): {7}
===================
""";
MessageFormat messageFormat = new MessageFormat(messageTemplate);
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/ssu/eatssu/global/log/ControllerLogAspect.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
Expand Down Expand Up @@ -51,15 +53,7 @@ public Object logApi(ProceedingJoinPoint joinPoint) throws Throwable {
String[] paramNames = methodSignature.getParameterNames();
Object[] args = joinPoint.getArgs();

// 요청자
String userId = IntStream.range(0, args.length)
.filter(i -> args[i] instanceof CustomUserDetails)
.mapToObj(i -> {
CustomUserDetails user = (CustomUserDetails) args[i];
return String.valueOf(user.getId());
})
.findFirst()
.orElse("anonymous");
String userId = getUserIdFromSecurityContext();

String userIdLog = "userId=" + userId;

Expand Down Expand Up @@ -127,6 +121,15 @@ private String getCauseMessage(Throwable e) {
return message != null ? message : e.getClass().getSimpleName();
}

private String getUserIdFromSecurityContext() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && authentication.isAuthenticated() && authentication.getPrincipal() instanceof CustomUserDetails) {
CustomUserDetails userDetails = (CustomUserDetails) authentication.getPrincipal();
return String.valueOf(userDetails.getId());
}
Comment on lines +126 to +129

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Java 16 이상을 사용하고 있다면, instanceof 패턴 매칭을 활용하여 코드를 더 간결하게 만들 수 있습니다. 이렇게 하면 타입 확인과 변수 할당을 한 줄로 처리할 수 있어 가독성이 향상됩니다.

        if (authentication != null && authentication.isAuthenticated() && authentication.getPrincipal() instanceof CustomUserDetails userDetails) {
            return String.valueOf(userDetails.getId());
        }

return "anonymous";
}

private Map<String, Object> toSafeMap(Object arg) {
Map<String, Object> result = new HashMap<>();
for (Field field : arg.getClass().getDeclaredFields()) {
Expand Down