-
Notifications
You must be signed in to change notification settings - Fork 7
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
backend/src/main/java/codezap/global/cors/CorsProperties.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,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 | ||
) { | ||
this.allowedOrigins = allowedOrigins; | ||
this.allowedOriginsPatterns = allowedOriginsPatterns; | ||
} | ||
|
||
public String[] getAllowedOrigins() { | ||
return allowedOrigins; | ||
} | ||
|
||
public String[] getAllowedOriginsPatterns() { | ||
return allowedOriginsPatterns; | ||
} | ||
} |
13 changes: 10 additions & 3 deletions
13
backend/src/main/java/codezap/global/cors/WebCorsConfiguration.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
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 |
---|---|---|
|
@@ -2,3 +2,5 @@ spring: | |
output: | ||
ansi: | ||
enabled: always | ||
cors: | ||
allowed-origins: http://localhost:3000 |
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
34 changes: 34 additions & 0 deletions
34
backend/src/test/java/codezap/global/cors/CorsPropertiesTest.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,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[]{""}); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -2,3 +2,5 @@ spring: | |
output: | ||
ansi: | ||
enabled: always | ||
cors: | ||
allowed-origins: http://localhost:3000 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yml 에 값이 없을 경우, NPE 예방을 위해 기본값 설정해놨습니다~