How to enable Cors? #902
-
Hello. i tried to enable cors on by following this (spring boot + cors tutorial, https://spring.io/guides/gs/rest-service-cors/) and add the corsConfigurer to my springboot application, but as soon as i add the 'org.springframework.boot:spring-boot-starter-web' to my gradle file. i cant access my playground anymore, i see a login page instead. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
i removed the login page by disabling security with the security.basic.enabled=false on the application.properties. but now im getting errors because it wants me to create the default rest controller (/Error) and the /graphql and /playground routes are disabled |
Beta Was this translation helpful? Give feedback.
-
Hi,
Enabling CORS on webflux based apps should be pretty similar to web mvc one - see https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/web-reactive.html#webflux-cors for details. |
Beta Was this translation helpful? Give feedback.
-
You should be able to create a https://enable-cors.org/server.html @Bean
fun corsWebFilter(): CorsWebFilter {
// Example of some basic config
val corsConfig = CorsConfiguration()
corsConfig.allowedOrigins = mutableListOf("https://example.com")
corsConfig.allowedMethods = mutableListOf("GET", "POST")
corsConfig.allowedHeaders = mutableListOf("Origin", "X-Requested-With", "Content-Type", "Accept")
corsConfig.allowCredentials = true
corsConfig.maxAge = 8000L
// Set the URL matching
val urlSource = UrlBasedCorsConfigurationSource()
urlSource.registerCorsConfiguration("/**", corsConfig)
return CorsWebFilter(urlSource)
} |
Beta Was this translation helpful? Give feedback.
You should be able to create a
CorsWebFilter
bean and that should be picked up by Spring automaticallyhttps://enable-cors.org/server.html