-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: added aop for duplicated request
- Loading branch information
Showing
5 changed files
with
64 additions
and
3 deletions.
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
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
57 changes: 57 additions & 0 deletions
57
smeem-input-http/src/main/java/com/smeem/http/aspect/DuplicateRequestAspect.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,57 @@ | ||
package com.smeem.http.aspect; | ||
|
||
import com.smeem.common.exception.ExceptionCode; | ||
import com.smeem.http.controller.dto.ExceptionResponse; | ||
import lombok.val; | ||
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.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
@Aspect | ||
@Component | ||
public class DuplicateRequestAspect { | ||
private final Set<String> requestSet = Collections.synchronizedSet(new HashSet<>()); | ||
|
||
@Pointcut("within(*..*Api)") | ||
public void onRequest() { | ||
} | ||
|
||
@Around("onRequest()") | ||
public Object duplicateRequestCheck(ProceedingJoinPoint joinPoint) throws Throwable { | ||
val request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); | ||
val httpMethod = request.getMethod(); | ||
|
||
// GET 요청일 경우 중복 체크 생략 | ||
if ("GET".equalsIgnoreCase(httpMethod)) { | ||
return joinPoint.proceed(); | ||
} | ||
|
||
val requestId = joinPoint.getSignature().toLongString(); | ||
if (requestSet.contains(requestId)) { | ||
// 중복 요청인 경우 | ||
return handleDuplicateRequest(); | ||
} | ||
requestSet.add(requestId); | ||
|
||
try { | ||
return joinPoint.proceed(); | ||
} finally { // 요청 후 요청값 삭제 | ||
requestSet.remove(requestId); | ||
} | ||
} | ||
|
||
private ResponseEntity<ExceptionResponse> handleDuplicateRequest() { | ||
return ResponseEntity | ||
.status(ExceptionCode.TOO_MANY_REQUESTS.getStatusCode()) | ||
.body(ExceptionResponse.of(ExceptionCode.TOO_MANY_REQUESTS.getMessage() + ": 중복된 요청")); | ||
} | ||
} |
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