Skip to content

Commit

Permalink
[feat #110] AOP Logging (#111)
Browse files Browse the repository at this point in the history
* [refactor] : 토큰 로그아웃 여부 검증 리팩토링

* [feat] : Member toString Overrid

* [feat] : Logging AOP 추가

* [rename] : AOP Logging Method 네이밍 변경
  • Loading branch information
dudxo authored Sep 18, 2024
1 parent 3b50802 commit d79a911
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 5 deletions.
79 changes: 79 additions & 0 deletions src/main/java/com/dnd/gongmuin/common/config/LoggingAspect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.dnd.gongmuin.common.config;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Objects;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
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 lombok.extern.slf4j.Slf4j;

@Aspect
@Slf4j
@Component
public class LoggingAspect {

@Pointcut("execution(* com.dnd.gongmuin..controller..*(..))")
public void controllerPointcut() {
}

@Pointcut("execution(* com.dnd.gongmuin..service..*(..))")
public void servicePointcut() {
}

@Around("controllerPointcut() || servicePointcut()")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
long start = System.currentTimeMillis();
log.info("START: {}", joinPoint.toString());
try {
return joinPoint.proceed();
} finally {
long end = System.currentTimeMillis();
long timeMs = end - start;
log.info("END: {} {}ms", joinPoint.toString(), timeMs);
}
}

@Before("controllerPointcut()")
public void logRequestArgs(JoinPoint joinPoint) throws Throwable {
Method method = getMethod(joinPoint);
String methodName = method.getName();
log.info("===== {} Request Detail START =====", methodName);
Arrays.stream(joinPoint.getArgs())
.filter(Objects::nonNull)
.forEach(arg -> {
log.info("type: {}, value: {}", arg.getClass().getSimpleName(), arg);
});
log.info("===== {} Request Detail END =====", methodName);
}

@AfterReturning(value = "servicePointcut()", returning = "returnValue")
public void logResponseDetails(JoinPoint joinPoint, Object returnValue) throws Throwable {
Method method = getMethod(joinPoint);
String methodName = method.getName();

log.info("===== {} Response Detail START =====", methodName);
if (returnValue == null) {
log.info("null 값 반환 오류 발생!!!");
return;
}

log.info("Detail: {}", returnValue.toString());
log.info("===== {} Response Detail END =====", methodName);

}

private Method getMethod(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
return signature.getMethod();
}

}
10 changes: 10 additions & 0 deletions src/main/java/com/dnd/gongmuin/member/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ private Member(String nickname, String socialName, JobGroup jobGroup, JobCategor
this.role = role;
}

@Override
public String toString() {
return "MEMBER INFO{" +
"id=" + id +
", role='" + role + '\'' +
", jobGroup=" + jobGroup +
", socialEmail='" + socialEmail + '\'' +
'}';
}

public static Member of(String socialName, String socialEmail, int credit) {
return Member.builder()
.socialName(socialName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import org.springframework.util.ObjectUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import com.dnd.gongmuin.redis.util.RedisUtil;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
Expand All @@ -25,7 +23,6 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter {

private static final String TOKEN_PREFIX = "Bearer ";
private final TokenProvider tokenProvider;
private final RedisUtil redisUtil;
private final CookieUtil cookieUtil;

@Override
Expand All @@ -36,8 +33,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse

if (tokenProvider.validateToken(accessToken, new Date())) {
// accessToken logout 여부 확인
String isLogout = redisUtil.getValues(accessToken);
if ("false".equals(isLogout)) {
if (tokenProvider.verifyLogout(accessToken)) {
saveAuthentication(accessToken);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import javax.crypto.SecretKey;
Expand Down Expand Up @@ -135,4 +136,9 @@ public Long getExpiration(String token, Date date) {
return (expiration.getTime() - date.getTime());
}

public boolean verifyLogout(String accessToken) {
String value = redisUtil.getValues(accessToken);
return Objects.equals("false", value);
}

}

0 comments on commit d79a911

Please sign in to comment.