2020 */
2121package eu .openanalytics .containerproxy .ui ;
2222
23- import javax .inject .Inject ;
24-
2523import org .springframework .context .annotation .Bean ;
2624import org .springframework .context .annotation .Configuration ;
2725import org .springframework .core .env .Environment ;
26+ import org .springframework .web .servlet .config .annotation .CorsRegistry ;
2827import org .springframework .web .servlet .config .annotation .ResourceHandlerRegistry ;
2928import org .springframework .web .servlet .config .annotation .WebMvcConfigurer ;
3029import org .thymeleaf .templateresolver .FileTemplateResolver ;
3130
31+ import javax .annotation .Nonnull ;
32+ import javax .inject .Inject ;
33+ import java .util .ArrayList ;
34+ import java .util .List ;
35+
3236@ Configuration
3337public class TemplateResolverConfig implements WebMvcConfigurer {
3438
39+ private static final String PROP_TEMPLATE_PATH = "proxy.template-path" ;
40+
41+ public static final String PROP_CORS_ALLOWED_ORIGINS = "proxy.api-security.cors-allowed-origins" ;
42+
3543 @ Inject
3644 private Environment environment ;
3745
3846 @ Override
3947 public void addResourceHandlers (ResourceHandlerRegistry registry ) {
4048 registry .addResourceHandler ("/assets/**" )
41- .addResourceLocations ("file:" + environment .getProperty ("proxy.template-path" ) + "/assets/" );
49+ .addResourceLocations ("file:" + environment .getProperty (PROP_TEMPLATE_PATH ) + "/assets/" );
4250 }
4351
4452 @ Bean
@@ -52,4 +60,23 @@ public FileTemplateResolver templateResolver() {
5260 resolver .setOrder (1 );
5361 return resolver ;
5462 }
55- }
63+
64+ @ Override
65+ public void addCorsMappings (@ Nonnull CorsRegistry registry ) {
66+ List <String > origins = new ArrayList <>();
67+ int i = 0 ;
68+ String origin = environment .getProperty (String .format (PROP_CORS_ALLOWED_ORIGINS + "[%d]" , i ));
69+ while (origin != null ) {
70+ origins .add (origin );
71+ i ++;
72+ origin = environment .getProperty (String .format (PROP_CORS_ALLOWED_ORIGINS + "[%d]" , i ));
73+ }
74+
75+ if (origins .size () > 0 ) {
76+ registry .addMapping ("/**" )
77+ .allowCredentials (true )
78+ .allowedOrigins (origins .toArray (new String [0 ]));
79+ }
80+ }
81+
82+ }
0 commit comments