-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLoggingAspect.java
More file actions
129 lines (117 loc) · 5.62 KB
/
LoggingAspect.java
File metadata and controls
129 lines (117 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package clap.server.config.aop;
import clap.server.adapter.inbound.security.SecurityUserDetails;
import clap.server.adapter.outbound.persistense.entity.log.constant.LogStatus;
import clap.server.application.port.inbound.log.CreateAnonymousLogsUsecase;
import clap.server.application.port.inbound.log.CreateMemberLogsUsecase;
import clap.server.config.annotation.LogType;
import clap.server.exception.BaseException;
import clap.server.exception.ErrorContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import java.nio.charset.StandardCharsets;
@Slf4j
@Aspect
@Component
@RequiredArgsConstructor
public class LoggingAspect {
private final ObjectMapper objectMapper;
private final CreateAnonymousLogsUsecase createAnonymousLogsUsecase;
private final CreateMemberLogsUsecase createMemberLogsUsecase;
private final HandlerExceptionResolver handlerExceptionResolver;
@Pointcut("execution(* clap.server.adapter.inbound.web..*Controller.*(..))")
public void controllerMethods() {
}
@Around("controllerMethods()")
public Object logApiRequests(ProceedingJoinPoint joinPoint) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
HttpServletRequest request = attributes.getRequest();
HttpServletResponse response = attributes.getResponse();
Object result = null;
Exception capturedException = null;
try {
result = joinPoint.proceed();
} catch (Exception ex) {
capturedException = ex;
throw ex;
} finally {
LogStatus logStatus = getLogType((MethodSignature) joinPoint.getSignature());
int statusCode;
String customCode = null;
if (capturedException != null) {
if (capturedException instanceof BaseException e) {
statusCode = e.getCode().getHttpStatus().value();
customCode = e.getCode().getCustomCode();
} else {
ModelAndView modelAndView = handlerExceptionResolver.resolveException(request, response, null, capturedException);
statusCode = modelAndView.getStatus().value();
}
} else {
statusCode = response.getStatus();
}
if (logStatus != null) {
if (LogStatus.LOGIN.equals(logStatus)) {
createAnonymousLogsUsecase.createAnonymousLog(request, statusCode, customCode, logStatus, result, getRequestBody(request), getNicknameFromRequestBody(request));
} else {
if (!isUserAuthenticated()) {
log.error("로그인 시도 로그를 기록할 수 없음");
} else {
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (principal instanceof SecurityUserDetails userDetails) {
createMemberLogsUsecase.createMemberLog(request, statusCode, customCode, logStatus, result, getRequestBody(request), userDetails.getUserId());
}
}
}
}
}
return result;
}
private LogStatus getLogType(MethodSignature methodSignature) {
if (methodSignature.getMethod().isAnnotationPresent(LogType.class)) {
return methodSignature.getMethod().getAnnotation(LogType.class).value();
} else {
return null;
}
}
private String getNicknameFromRequestBody(HttpServletRequest request) {
try {
String requestBody = getRequestBody(request);
JsonNode jsonNode = objectMapper.readTree(requestBody);
return jsonNode.has("nickname") ? jsonNode.get("nickname").asText() : null;
} catch (Exception e) {
return null;
}
}
private String getRequestBody(HttpServletRequest request) {
try {
ContentCachingRequestWrapper cachingRequest = (ContentCachingRequestWrapper) request;
byte[] content = cachingRequest.getContentAsByteArray();
return new String(content, StandardCharsets.UTF_8);
} catch (Exception e) {
return "요청 바디의 내용을 읽을 수 없음";
}
}
private boolean isUserAuthenticated() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return authentication != null && authentication.isAuthenticated()
&& !"anonymousUser".equals(authentication.getPrincipal());
}
}