Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] CORS 문제 해결 #460

Merged
merged 11 commits into from
Jan 8, 2025
2 changes: 1 addition & 1 deletion backend/scripts/dev/replace-new-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ fi

JAR_FILE=$(ls /home/ubuntu/app/*.jar | head -n 1)

nohup java -Dspring.profiles.active=dev -Duser.timezone=Asia/Seoul -Dserver.port=8080 -jar "$JAR_FILE" &
sudo nohup java -Dspring.profiles.active=dev -Duser.timezone=Asia/Seoul -Dserver.port=8080 -jar "$JAR_FILE" &
6 changes: 5 additions & 1 deletion backend/src/main/java/ddangkong/config/CorsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns(corsOrigin)
.allowedMethods(
HttpMethod.GET.name(), HttpMethod.POST.name(), HttpMethod.PATCH.name(), HttpMethod.DELETE.name()
HttpMethod.GET.name(),
HttpMethod.POST.name(),
HttpMethod.PATCH.name(),
HttpMethod.DELETE.name(),
HttpMethod.OPTIONS.name()
)
.allowCredentials(true)
.allowedHeaders("*");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
Expand All @@ -20,13 +22,22 @@ public AdminAuthorizationInterceptor(@Value("${admin.session-key}") String sessi
}

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
public boolean preHandle(@NotNull HttpServletRequest request,
@NotNull HttpServletResponse response,
@NotNull Object handler) {
if (isPreflightRequest(request)) {
return true;
}
if (hasAdminAuthAnnotation(handler)) {
authorizeAdmin(request);
}
return true;
}

private boolean isPreflightRequest(HttpServletRequest request) {
return HttpMethod.OPTIONS.name().equals(request.getMethod());
}

private boolean hasAdminAuthAnnotation(Object handler) {
if (handler instanceof ResourceHttpRequestHandler) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public enum ClientErrorCode {
BLANK_BALANCE_CONTENT_NAME("컨텐츠 이름이 비어 있습니다."),
LONG_BALANCE_CONTENT_NAME("컨텐츠 이름은 최대 %d자 입니다."),
BLANK_BALANCE_OPTION_NAME("컨텐츠 옵션이 비어 있습니다."),
LONG_BALANCE_OPTION_NAME("컨텐츠 옵션은 최대 % 입니다."),
LONG_BALANCE_OPTION_NAME("컨텐츠 옵션은 최대 %d자 입니다."),
ALREADY_USING_AT_ROOM_BALANCE_CONTENT("현재 게임에서 사용 중인 컨텐츠입니다."),

// Common
Expand Down
55 changes: 29 additions & 26 deletions backend/src/test/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
spring:
profiles:
active: test

---

spring:
config:
activate:
on-profile: test

datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
username: sa

jpa:
hibernate:
ddl-auto: create
properties:
hibernate:
format_sql: true
Expand All @@ -10,20 +25,6 @@ spring:
database-platform: org.hibernate.dialect.H2Dialect
defer-datasource-initialization: true

cors:
origin: "*"

cookie:
rejoin-key: test_cookie

encrypt:
secret-key: 1234567890123456
algorithm: AES

admin:
session-key : admin
password : 1234

logging:
level:
org:
Expand All @@ -35,15 +36,17 @@ logging:
transaction:
interceptor: TRACE

---
spring:
config:
activate:
on-profile: test
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
username: sa
jpa:
hibernate:
ddl-auto: create
cors:
origin: '*'

cookie:
rejoin-key: test_cookie

encrypt:
secret-key: 1234567890123456
algorithm: AES

admin:
session-key: admin
password: 1234

Loading