Skip to content

Instantly share code, notes, and snippets.

@tombasche
Created August 11, 2021 15:14
Show Gist options
  • Save tombasche/5905ee126486e5114d83ffef045ca89d to your computer and use it in GitHub Desktop.
Save tombasche/5905ee126486e5114d83ffef045ca89d to your computer and use it in GitHub Desktop.
Spring boot (kotlin) Web Server Config for CORS
package com.myapplication
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.servlet.config.annotation.CorsRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
@Configuration
class WebServerConfiguration {
@Value("\${cors.originPatterns:default}")
private val corsOriginPatterns: String = ""
@Bean
fun addCorsConfig(): WebMvcConfigurer {
return object : WebMvcConfigurer {
override fun addCorsMappings(registry: CorsRegistry) {
val allowedOrigins = corsOriginPatterns.split(",").toTypedArray()
registry.addMapping("/**")
.allowedMethods("*")
.allowedOriginPatterns(*allowedOrigins)
.allowCredentials(true)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment