-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
interceptor 기반 요청 제한 annotation 기반으로 변경 #17
- Loading branch information
Showing
6 changed files
with
68 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,12 @@ | ||
package com.urlshortener.config.web; | ||
|
||
import com.urlshortener.ratelimit.interceptor.ClientIdInterceptor; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class WebConfig implements WebMvcConfigurer { | ||
private final ClientIdInterceptor clientIdInterceptor; | ||
|
||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(clientIdInterceptor).addPathPatterns("/api/v1/short"); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/urlshortener/ratelimit/annotation/RateLimit.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,13 @@ | ||
package com.urlshortener.ratelimit.annotation; | ||
|
||
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 RateLimit { | ||
int value(); | ||
int durationMinutes(); | ||
} |
56 changes: 37 additions & 19 deletions
56
...imit/interceptor/ClientIdInterceptor.java → ...ner/ratelimit/aspect/RateLimitAspect.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 |
---|---|---|
@@ -1,56 +1,74 @@ | ||
package com.urlshortener.ratelimit.interceptor; | ||
package com.urlshortener.ratelimit.aspect; | ||
|
||
import com.urlshortener.error.dto.ErrorMessage; | ||
import com.urlshortener.error.exception.url.RateLimitExceededException; | ||
import com.urlshortener.ratelimit.annotation.RateLimit; | ||
import com.urlshortener.ratelimit.service.RateLimitService; | ||
import jakarta.servlet.http.Cookie; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import com.urlshortener.error.dto.ErrorMessage; | ||
import com.urlshortener.error.exception.url.RateLimitExceededException; | ||
import com.urlshortener.ratelimit.service.RateLimitService; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Before; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
import java.util.Arrays; | ||
import java.util.UUID; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Aspect | ||
@Component | ||
public class ClientIdInterceptor implements HandlerInterceptor { | ||
@RequiredArgsConstructor | ||
public class RateLimitAspect { | ||
private final RateLimitService rateLimitService; | ||
private final HttpServletRequest request; | ||
private final HttpServletResponse response; | ||
|
||
@Override | ||
public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, Object handler) { | ||
var clientId = getClientIdFromCookie(request, response); | ||
/** | ||
* UUID 사용한 요청 횟수 제한 | ||
* | ||
* @param rateLimit -> 일정 시간 동안 (rateLimit.value()) 특정 횟수까지 요청 제한 (rateLimit.durationMinutes()) | ||
* @throws RateLimitExceededException 일정 시간 요청 횟수 초과시 예외 발생 | ||
*/ | ||
@Before("@annotation(rateLimit)") | ||
public void checkRateLimit(RateLimit rateLimit) { | ||
String clientId = getClientIdFromCookie(request, response); | ||
|
||
if (!rateLimitService.tryConsume(clientId)) { | ||
if (!rateLimitService.tryConsume(clientId, rateLimit)) { | ||
throw new RateLimitExceededException(ErrorMessage.RATE_LIMIT_EXCEEDED); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* 쿠키 조회 및 쿠키가 없을 시 생성 | ||
* | ||
* @param request 쿠키 조회, response 쿠키 등록 | ||
* @return clientCookie UUID | ||
*/ | ||
private String getClientIdFromCookie(HttpServletRequest request, HttpServletResponse response) { | ||
if (request.getCookies() == null) { | ||
return createClientIdFromCookie(response); | ||
} | ||
|
||
return Arrays.stream(request.getCookies()) | ||
.filter(cookie -> "client_id".equals(cookie.getName())) | ||
.filter(cookie -> "client-id".equals(cookie.getName())) | ||
.map(Cookie::getValue) | ||
.findFirst() | ||
.orElseGet(() -> createClientIdFromCookie(response)); | ||
} | ||
|
||
/** | ||
* 쿠키 생성 및 쿠키 저장 | ||
* | ||
* @param response 쿠키 등록 | ||
* @return clientCookie UUID | ||
*/ | ||
private String createClientIdFromCookie(HttpServletResponse response) { | ||
String clientId = UUID.randomUUID().toString(); | ||
Cookie cookie = new Cookie("client_id", clientId); | ||
Cookie cookie = new Cookie("client-id", clientId); | ||
cookie.setPath("/"); | ||
cookie.setMaxAge(60 * 60 * 24 * 183); | ||
response.addCookie(cookie); | ||
|
||
return clientId; | ||
} | ||
} | ||
} |
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
Submodule url-shortener-security
updated
from 76f2a4 to 895c14