Skip to content

Instantly share code, notes, and snippets.

@alexaugustobr
Forked from thiagofa/CorsConfig.java
Created August 23, 2022 17:41
Show Gist options
  • Save alexaugustobr/3222a12dfa34c22cd7bc7b6d089e3ced to your computer and use it in GitHub Desktop.
Save alexaugustobr/3222a12dfa34c22cd7bc7b6d089e3ced to your computer and use it in GitHub Desktop.
Configuração de CORS no Authorization Server com CorsFilter
// Fonte: https://spring.io/blog/2015/06/08/cors-support-in-spring-framework#filter-based-cors-support
import java.util.Collections;
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;
@Configuration
public class CorsConfig {
@Bean
public FilterRegistrationBean<CorsFilter> corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(false);
config.setAllowedOrigins(Collections.singletonList("*"));
config.setAllowedMethods(Collections.singletonList("*"));
config.setAllowedHeaders(Collections.singletonList("*"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/oauth/token", config);
FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>();
bean.setFilter(new CorsFilter(source));
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return bean;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment