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

cors allow origin 을 yml에서 읽어오도록 변경 #619

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions backend/src/main/java/codezap/global/cors/CorsProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package codezap.global.cors;


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.bind.DefaultValue;

@ConfigurationProperties(prefix = "cors")
public class CorsProperties {
private final String[] allowedOrigins;
private final String[] allowedOriginsPatterns;

public CorsProperties(
@DefaultValue(value = "") String[] allowedOrigins,
@DefaultValue(value = "") String[] allowedOriginsPatterns
Comment on lines +13 to +14
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yml 에 값이 없을 경우, NPE 예방을 위해 기본값 설정해놨습니다~

) {
this.allowedOrigins = allowedOrigins;
this.allowedOriginsPatterns = allowedOriginsPatterns;
}

public String[] getAllowedOrigins() {
return allowedOrigins;
}

public String[] getAllowedOriginsPatterns() {
return allowedOriginsPatterns;
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package codezap.global.cors;

import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ConfigurationPropertiesScan
public class WebCorsConfiguration implements WebMvcConfigurer {

private final CorsProperties corsProperties;

public WebCorsConfiguration(CorsProperties corsProperties) {
this.corsProperties = corsProperties;
}

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowCredentials(true)
.allowedOriginPatterns("https://*.code-zap.com")
.allowedOrigins("https://code-zap.com")
.allowedOrigins("chrome-extension://bmlonhfgleihfabinjbhgefojkfpmlaf")
.allowedOriginPatterns(corsProperties.getAllowedOriginsPatterns())
.allowedOrigins(corsProperties.getAllowedOrigins())
.allowedMethods("*")
.exposedHeaders("*");
}
Expand Down
2 changes: 2 additions & 0 deletions backend/src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ spring:
output:
ansi:
enabled: always
cors:
allowed-origins: http://localhost:3000
4 changes: 4 additions & 0 deletions backend/src/test/java/codezap/global/MockMvcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
Expand All @@ -19,11 +21,13 @@
import codezap.auth.provider.CredentialProvider;
import codezap.category.service.facade.MemberCategoryApplicationService;
import codezap.category.service.facade.MemberCategoryTemplateApplicationService;
import codezap.global.cors.CorsProperties;
import codezap.member.fixture.MemberFixture;
import codezap.member.service.MemberService;
import codezap.template.service.TemplateService;

@WebMvcTest(SpringExtension.class)
@EnableConfigurationProperties(CorsProperties.class)
public abstract class MockMvcTest {

@Autowired
Expand Down
34 changes: 34 additions & 0 deletions backend/src/test/java/codezap/global/cors/CorsPropertiesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package codezap.global.cors;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@SpringBootTest
class CorsPropertiesTest {

@Autowired
private CorsProperties corsProperties;

@Test
@DisplayName("yml 파일로부터 allowed-origins 값을 가져오는지 확인")
void getAllowedOrigins() {
assertThat(corsProperties.getAllowedOrigins()).isEqualTo(new String[]{"http://localhost:3000"});
}

@Test
@DisplayName("yml 파일로부터 allowed-origins-patterns 값을 가져오는지 확인")
void getAllowedOriginsPatterns() {
assertThat(corsProperties.getAllowedOriginsPatterns()).isEqualTo(new String[]{""});
}
}
2 changes: 2 additions & 0 deletions backend/src/test/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ spring:
output:
ansi:
enabled: always
cors:
allowed-origins: http://localhost:3000