Skip to content

Commit f174d22

Browse files
authored
Merge pull request #525 from TaskFlow-CLAP/CLAP-406
CLAP-406 DevelopOnlyApi 운영 환경에 적용되지 않도록 설정
2 parents e4f3f6f + 962a555 commit f174d22

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package clap.server.common.utils;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import org.springframework.core.env.Environment;
5+
import org.springframework.stereotype.Component;
6+
import org.springframework.util.CollectionUtils;
7+
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
@Component
12+
@RequiredArgsConstructor
13+
public class SpringEnvironmentHelper {
14+
15+
private final Environment environment;
16+
17+
private final String PROD = "prod";
18+
private final String DEV = "dev";
19+
private final String LOCAL = "local";
20+
21+
private final List<String> LOCAL_AND_DEV = List.of("local", "dev");
22+
23+
public Boolean isProdProfile() {
24+
String[] activeProfiles = environment.getActiveProfiles();
25+
List<String> currentProfile = Arrays.stream(activeProfiles).toList();
26+
return currentProfile.contains(PROD);
27+
}
28+
29+
public Boolean isLocalProfile() {
30+
String[] activeProfiles = environment.getActiveProfiles();
31+
List<String> currentProfile = Arrays.stream(activeProfiles).toList();
32+
return currentProfile.contains(LOCAL);
33+
}
34+
35+
public Boolean isDevProfile() {
36+
String[] activeProfiles = environment.getActiveProfiles();
37+
List<String> currentProfile = Arrays.stream(activeProfiles).toList();
38+
return currentProfile.contains(DEV);
39+
}
40+
41+
public Boolean isLocalAndDevProfile() {
42+
String[] activeProfiles = environment.getActiveProfiles();
43+
List<String> currentProfile = Arrays.stream(activeProfiles).toList();
44+
return CollectionUtils.containsAny(LOCAL_AND_DEV, currentProfile);
45+
}
46+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package clap.server.config.aop;
2+
3+
import clap.server.common.utils.SpringEnvironmentHelper;
4+
import clap.server.exception.ApplicationException;
5+
import lombok.RequiredArgsConstructor;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.aspectj.lang.ProceedingJoinPoint;
8+
import org.aspectj.lang.annotation.Around;
9+
import org.aspectj.lang.annotation.Aspect;
10+
import org.springframework.stereotype.Component;
11+
12+
import static clap.server.exception.code.GlobalErrorCode.BLOCKED_API;
13+
14+
@Aspect
15+
@Component
16+
@Slf4j
17+
@RequiredArgsConstructor
18+
public class ApiBlockingAspect {
19+
20+
private final SpringEnvironmentHelper springEnvironmentHelper;
21+
22+
@Around("@annotation(clap.server.common.annotation.swagger.DevelopOnlyApi)")
23+
public Object checkApiAcceptingCondition(ProceedingJoinPoint joinPoint) throws Throwable {
24+
if (springEnvironmentHelper.isProdProfile()) {
25+
throw new ApplicationException(BLOCKED_API) ;
26+
}
27+
return joinPoint.proceed();
28+
}
29+
}

src/main/java/clap/server/exception/code/GlobalErrorCode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public enum GlobalErrorCode implements BaseErrorCode {
1515
METHOD_ARGUMENT_NOT_VALID(HttpStatus.BAD_REQUEST, "COMMON_002", "올바르지 않은 요청입니다."),
1616
METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, "COMMON_005", "지원하지 않은 Http Method 입니다."),
1717
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "COMMON_006", "서버 에러가 발생했습니다."),
18+
BLOCKED_API(HttpStatus.METHOD_NOT_ALLOWED, "COMMON_007", "운영 환경에서 사용할 수 없는 API 입니다."),
1819
;
1920

2021
private final HttpStatus httpStatus;

src/main/resources/swagger.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ springdoc:
3535
path: /swagger/v3/api-docs
3636

3737
---
38-
spring.config.activate.on-profile: prod
38+
spring.config.activate.on-profile: prod
39+
swagger.server.url: ${SWAGGER_SERVER_URL:http://localhost:8080}

0 commit comments

Comments
 (0)