
entry-point.rest-mvc.cors-config.mustache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scaffold-clean-architecture Show documentation
Show all versions of scaffold-clean-architecture Show documentation
Gradle plugin to create a clean application in Java that already works, It follows our best practices!
package {{package}}.api.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import java.util.Arrays;
import java.util.List;
@Configuration
public class CorsConfig {
@Bean
public FilterRegistrationBean corsFilter(@Value("${cors.allowed-origins}") String origins) {
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.setAllowedOrigins(List.of(origins.split(",")));
config.setAllowedMethods(Arrays.asList("POST", "GET")); // TODO: Check others required methods
config.setAllowedHeaders(List.of(CorsConfiguration.ALL));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
final FilterRegistrationBean corsFilter = new FilterRegistrationBean<>(new CorsFilter(source));
corsFilter.setOrder(Ordered.HIGHEST_PRECEDENCE);
return corsFilter;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy