Skip to content

Commit

Permalink
Merge pull request #446 from woowacourse-teams/develop-backend
Browse files Browse the repository at this point in the history
백엔드 변경사항을 Develop 브랜치에 반영
  • Loading branch information
Mingyum-Kim authored Aug 22, 2024
2 parents bc7d066 + 2ddd688 commit 11be948
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/backend/out
/frontend/.idea
/backend/htmlReport
*.pem
1 change: 1 addition & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ build/
out
logs
src/main/resources/firebase
src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package mouda.backend.aop.logging;

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

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExceptRequestLogging {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package mouda.backend.aop.logging;

import static java.util.stream.Collectors.*;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import mouda.backend.darakbangmember.domain.DarakbangMember;
import mouda.backend.member.domain.Member;

@Aspect
@Component
@Slf4j
public class RequestLoggingAspect {

@Pointcut("execution(* mouda.backend..controller.*Controller.*(..))")
public void allController() {
}

@Before("allController()")
public void logController(JoinPoint joinPoint) {
HttpServletRequest request = getHttpServletRequest();

MethodSignature signature = (MethodSignature)joinPoint.getSignature();
Method method = signature.getMethod();
if (method.isAnnotationPresent(ExceptRequestLogging.class)) {
return;
}

String uri = request.getRequestURI();
String httpMethod = request.getMethod();
String queryParameters = getQueryParameters(request);
String body = getBody(joinPoint);

String memberInfo = getMemberInfo(joinPoint);

log.info("Request Logging: {} {} member - {} body - {} parameters - {}", httpMethod, uri, memberInfo, body,
queryParameters);
}

private String getMemberInfo(JoinPoint joinPoint) {
for (Object arg : joinPoint.getArgs()) {
if (arg instanceof Member) {
return "Member ID: " + ((Member)arg).getId();
}
if (arg instanceof DarakbangMember) {
return "DarakbangMember ID: " + ((DarakbangMember)arg).getId();
}
}
return "No member";
}

private HttpServletRequest getHttpServletRequest() {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
return requestAttributes.getRequest();
}

private String getQueryParameters(HttpServletRequest request) {
String queryParameters = request.getParameterMap()
.entrySet()
.stream()
.map(entry -> "%s = %s".formatted(entry.getKey(), entry.getValue()[0]))
.collect(joining(", "));

if (queryParameters.isEmpty()) {
return null;
}
return queryParameters;
}

private String getBody(JoinPoint joinPoint) {
MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
Parameter[] parameters = methodSignature.getMethod().getParameters();
Object[] args = joinPoint.getArgs();
for (int i = 0; i < parameters.length; i++) {
Parameter param = parameters[i];
Object arg = args[i];
if (param.isAnnotationPresent(RequestBody.class)) {
return arg.toString();
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import mouda.backend.aop.logging.ExceptRequestLogging;
import mouda.backend.chat.dto.request.ChatCreateRequest;
import mouda.backend.chat.dto.request.DateTimeConfirmRequest;
import mouda.backend.chat.dto.request.LastReadChatRequest;
Expand Down Expand Up @@ -43,6 +44,7 @@ public ResponseEntity<Void> createChat(
}

@Override
@ExceptRequestLogging
@GetMapping
public ResponseEntity<RestResponse<ChatFindUnloadedResponse>> findUnloadedChats(
@PathVariable Long darakbangId,
Expand All @@ -57,6 +59,7 @@ public ResponseEntity<RestResponse<ChatFindUnloadedResponse>> findUnloadedChats(
}

@Override
@ExceptRequestLogging
@GetMapping("/preview")
public ResponseEntity<RestResponse<ChatPreviewResponses>> findChatPreviews(
@PathVariable Long darakbangId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ public void init() {
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.build();
if (FirebaseApp.getApps().isEmpty()) {
FirebaseApp.initializeApp(options);
}
FirebaseApp.initializeApp(options);
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
4 changes: 0 additions & 4 deletions backend/src/main/java/mouda/backend/config/WebMvcConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import mouda.backend.config.argumentresolver.LoginDarakbangMemberArgumentResolver;
import mouda.backend.config.argumentresolver.LoginMemberArgumentResolver;
import mouda.backend.config.converter.FilterTypeConverter;
import mouda.backend.config.interceptor.ApiRequestLoggingInterceptor;
import mouda.backend.config.interceptor.AuthenticationCheckInterceptor;

@Configuration
Expand All @@ -22,13 +21,10 @@ public class WebMvcConfig implements WebMvcConfigurer {
private final LoginMemberArgumentResolver loginMemberArgumentResolver;
private final LoginDarakbangMemberArgumentResolver loginDarakbangMemberArgumentResolver;
private final AuthenticationCheckInterceptor authenticationCheckInterceptor;
private final ApiRequestLoggingInterceptor apiRequestLoggingInterceptor;
private final FilterTypeConverter filterTypeConverter;

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(apiRequestLoggingInterceptor);

registry.addInterceptor(authenticationCheckInterceptor)
.addPathPatterns("/v1/**")
.excludePathPatterns("/v1/auth/kakao/oauth");
Expand Down

This file was deleted.

0 comments on commit 11be948

Please sign in to comment.