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

feat: #13 cors option 수정 #36

Merged
merged 1 commit into from
Aug 8, 2024
Merged
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
52 changes: 25 additions & 27 deletions src/main/kotlin/com/routebox/routebox/security/SecurityConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.springframework.security.web.SecurityFilterChain
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
import org.springframework.web.cors.CorsConfiguration
import org.springframework.web.cors.CorsConfigurationSource
import org.springframework.web.cors.UrlBasedCorsConfigurationSource

@Configuration
class SecurityConfig {
Expand Down Expand Up @@ -44,33 +45,8 @@ class SecurityConfig {
.httpBasic { it.disable() }
.formLogin { it.disable() }
.sessionManagement { session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS) }
.cors { corsConfigurer ->
val corsConfigSrc = CorsConfigurationSource {
val corsConfig = CorsConfiguration()
corsConfig.allowCredentials = true
corsConfig.allowedOrigins = listOf(
"http://localhost*",
"http://myroutebox.com",
"https://myroutebox.com",
"http://api-dev.myroutebox.com",
"https://api-dev.myroutebox.com",
"http://*.myroutebox.com",
"https://*.myroutebox.com",
)
corsConfig.allowedMethods = listOf(
HttpMethod.GET.name(),
HttpMethod.POST.name(),
HttpMethod.PUT.name(),
HttpMethod.DELETE.name(),
HttpMethod.PATCH.name(),
HttpMethod.OPTIONS.name(),
)
corsConfig.allowedHeaders = listOf("*")
corsConfig.exposedHeaders = listOf("*")
corsConfig.allowCredentials = true
return@CorsConfigurationSource corsConfig
}
corsConfigurer.configurationSource(corsConfigSrc)
.cors {
it.configurationSource(corsConfigurationSource())
}
.authorizeHttpRequests { auth ->
auth.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
Expand All @@ -89,5 +65,27 @@ class SecurityConfig {
}
.build()
}

@Bean
fun corsConfigurationSource(): CorsConfigurationSource {
val corsConfig = CorsConfiguration()
corsConfig.allowedOrigins = listOf("https://api-dev.myroutebox.com", "https://*.myroutebox.com")
corsConfig.allowedMethods = listOf(
HttpMethod.GET.name(),
HttpMethod.POST.name(),
HttpMethod.PUT.name(),
HttpMethod.DELETE.name(),
HttpMethod.PATCH.name(),
HttpMethod.OPTIONS.name(),
)
corsConfig.allowedHeaders = listOf("*")
corsConfig.exposedHeaders = listOf("*")
corsConfig.allowCredentials = true
corsConfig.maxAge = 3600L

val source = UrlBasedCorsConfigurationSource()
source.registerCorsConfiguration("/**", corsConfig)
return source
}
}
}
Loading