All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dream.configs.CorsConfig Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
package com.dream.configs;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import com.dream.utils.SecurityHelper;

import java.util.Arrays;
import java.util.List;

@Configuration
public class CorsConfig {

    public static List ALLOWED_ORIGINS;

 // Overcoming Spring's unavailability to set the value to static field
    @Value("#{'${allowed_origins}'.split(',')}") 
 	public void setAllowedOrigins(List key) {
 		CorsConfig.ALLOWED_ORIGINS = key;
 	}
    
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.setAllowedOrigins(ALLOWED_ORIGINS);
        config.addAllowedHeader("*");
        config.addAllowedHeader("Access-Control-Allow-Headers: Authorization, Content-Type");
        config.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy