1
+ package no.nav.klage.document.config
2
+
3
+ import org.springframework.beans.factory.annotation.Value
4
+ import org.springframework.boot.web.servlet.FilterRegistrationBean
5
+ import org.springframework.context.annotation.Bean
6
+ import org.springframework.context.annotation.Configuration
7
+ import org.springframework.core.Ordered
8
+ import org.springframework.web.cors.CorsConfiguration
9
+ import org.springframework.web.cors.CorsConfigurationSource
10
+ import org.springframework.web.cors.UrlBasedCorsConfigurationSource
11
+ import org.springframework.web.filter.CorsFilter
12
+
13
+ @Configuration
14
+ class CorsGlobalConfiguration {
15
+
16
+ @Value(" \$ {allowed.origins}" )
17
+ private lateinit var allowedOrigins: List <String >
18
+
19
+ @Bean
20
+ fun corsServletFilterRegistration (): FilterRegistrationBean <CorsFilter > {
21
+ val config = corsConfiguration()
22
+ val source = corsConfigurationSource(config)
23
+ val corsFilter = CorsFilter (source)
24
+ val bean = FilterRegistrationBean <CorsFilter >()
25
+ bean.filter = corsFilter
26
+ bean.order = Ordered .HIGHEST_PRECEDENCE
27
+ bean.urlPatterns = setOf (" /*" )
28
+ bean.isEnabled = true
29
+ return bean
30
+ }
31
+
32
+ private fun corsConfigurationSource (config : CorsConfiguration ): CorsConfigurationSource {
33
+ val source = UrlBasedCorsConfigurationSource ()
34
+ source.registerCorsConfiguration(" /**" , config)
35
+ return source
36
+ }
37
+
38
+ private fun corsConfiguration (): CorsConfiguration {
39
+ val config = CorsConfiguration ()
40
+ config.allowedOrigins = allowedOrigins
41
+ config.addAllowedMethod(" *" )
42
+ config.allowCredentials = true
43
+ config.maxAge = 3600L
44
+ return config
45
+ }
46
+ }
0 commit comments