Java Code Examples for org.springframework.web.cors.CorsConfiguration#addAllowedOrigin()

The following examples show how to use org.springframework.web.cors.CorsConfiguration#addAllowedOrigin() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CorsConfig.java    From microservices-platform with Apache License 2.0 6 votes vote down vote up
@Order(Ordered.HIGHEST_PRECEDENCE)
@Bean
public CorsWebFilter corsFilter() {
    CorsConfiguration config = new CorsConfiguration();
    // cookie跨域
    config.setAllowCredentials(Boolean.TRUE);
    config.addAllowedMethod(ALL);
    config.addAllowedOrigin(ALL);
    config.addAllowedHeader(ALL);
    // 配置前端js允许访问的自定义响应头
    config.addExposedHeader("setToken");

    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
    source.registerCorsConfiguration("/**", config);

    return new CorsWebFilter(source);
}
 
Example 2
Source File: CORSConfig.java    From entref-spring-boot with MIT License 6 votes vote down vote up
@Bean
public FilterRegistrationBean corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();

    config.addAllowedOrigin(env.getProperty(Constants.ENV_ALLOWED_ORIGIN));
    config.addAllowedHeader("*");
    config.addAllowedMethod(HttpMethod.GET);
    config.addAllowedMethod(HttpMethod.HEAD);
    config.addAllowedMethod(HttpMethod.POST);
    config.addAllowedMethod(HttpMethod.PUT);
    config.addAllowedMethod(HttpMethod.PATCH);
    config.addAllowedMethod(HttpMethod.DELETE);
    config.addAllowedMethod(HttpMethod.OPTIONS);
    config.addAllowedMethod(HttpMethod.TRACE);

    source.registerCorsConfiguration("/**", config);
    final FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
    bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
    return bean;
}
 
Example 3
Source File: CorsConfig.java    From codeway_service with GNU General Public License v3.0 6 votes vote down vote up
private CorsConfiguration buildConfig(){
	CorsConfiguration corsConfiguration = new CorsConfiguration();
	//在生产环境上最好指定域名,以免产生跨域安全问题
	corsConfiguration.addAllowedOrigin("http://localhost:9527");
	corsConfiguration.addAllowedOrigin("http://121.36.158.84:9527");
	// 前台博客
	corsConfiguration.addAllowedOrigin("http://localhost:3000");
	corsConfiguration.addAllowedOrigin("http://202.182.114.223:3000");
	corsConfiguration.addAllowedOrigin("http://codeway.me");
	corsConfiguration.addAllowedOrigin("https://codeway.me");
	corsConfiguration.addAllowedOrigin("http://www.codeway.me");
	corsConfiguration.addAllowedOrigin("https://www.codeway.me");
	corsConfiguration.addAllowedHeader("*");
	corsConfiguration.addAllowedMethod("*");
	return corsConfiguration;
}
 
Example 4
Source File: GatewayApplication.java    From cola-cloud with MIT License 5 votes vote down vote up
@Bean
public CorsFilter corsFilter() {
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    final CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");
    config.setMaxAge(18000L);
    config.addAllowedMethod("*");
    source.registerCorsConfiguration("/**", config);
    return new CorsFilter(source);
}
 
Example 5
Source File: CorsConfig.java    From Refactoring-Bot with MIT License 5 votes vote down vote up
@Bean
public FilterRegistrationBean<CorsFilter> corsFilter() {
	UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
	CorsConfiguration config = new CorsConfiguration();
	config.setAllowCredentials(true);
	config.addAllowedOrigin("*");
	config.addAllowedHeader("*");
	config.addAllowedMethod("*");
	source.registerCorsConfiguration("/**", config);
	FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(source));
	bean.setOrder(0);
	return bean;
}
 
Example 6
Source File: CorsConfig.java    From sophia_scaffolding with Apache License 2.0 5 votes vote down vote up
@Bean
public CorsWebFilter corsFilter() {
    CorsConfiguration config = new CorsConfiguration();
    config.addAllowedMethod("*");
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");

    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
    source.registerCorsConfiguration("/**", config);

    return new CorsWebFilter(source);
}
 
Example 7
Source File: Startup.java    From seata-demo with Apache License 2.0 5 votes vote down vote up
@Bean
public CorsFilter corsFilter() {
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    final CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");
    config.addAllowedMethod("*");
    source.registerCorsConfiguration("/**", config);
    return new CorsFilter(source);
}
 
Example 8
Source File: CorsConfigration.java    From Taroco with Apache License 2.0 5 votes vote down vote up
@Bean
public CorsFilter corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.addAllowedOrigin(CorsConfiguration.ALL);
    config.setAllowCredentials(true);
    config.addAllowedHeader(CorsConfiguration.ALL);
    config.addAllowedMethod(CorsConfiguration.ALL);
    source.registerCorsConfiguration("/**", config);
    return new CorsFilter(source);
}
 
Example 9
Source File: SpringBoot2TwitterCloneApplication.java    From Spring-Boot-2.0-Projects with MIT License 5 votes vote down vote up
@Bean
public FilterRegistrationBean corsFilter() {
	UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
	CorsConfiguration config = new CorsConfiguration();
	config.setAllowCredentials(true);
	config.addAllowedOrigin("*");
	config.addAllowedHeader("*");
	config.addAllowedMethod("*");
	source.registerCorsConfiguration("/**", config);
	FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
	bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
	return bean;
}
 
Example 10
Source File: CorsConfig.java    From xmfcn-spring-cloud with Apache License 2.0 5 votes vote down vote up
@Bean
public CorsFilter corsFilter() {
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    final CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true); // 允许cookies跨域
    config.setAllowCredentials(true); // 跨域session共享
    config.addAllowedOrigin("*");// #允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin
    config.addAllowedHeader("*");// #允许访问的头信息,*表示全部
    config.setMaxAge(18000L);// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了
    config.addAllowedMethod("*");// 允许提交请求的方法,*表示全部允许
    source.registerCorsConfiguration("/**", config);
    return new CorsFilter(source);
}
 
Example 11
Source File: CorsUrlHandlerMappingTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void preFlightRequestWithGlobalCorsConfig() throws Exception {
	CorsConfiguration mappedConfig = new CorsConfiguration();
	mappedConfig.addAllowedOrigin("*");
	this.handlerMapping.setCorsConfigurations(Collections.singletonMap("/welcome.html", mappedConfig));

	String origin = "https://domain2.com";
	ServerWebExchange exchange = createExchange(HttpMethod.OPTIONS, "/welcome.html", origin);
	Object actual = this.handlerMapping.getHandler(exchange).block();

	assertNotNull(actual);
	assertNotSame(this.welcomeController, actual);
	assertEquals("*", exchange.getResponse().getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN));
}
 
Example 12
Source File: SimpleCORSFilter.java    From eb-java-scorekeep with Apache License 2.0 5 votes vote down vote up
private static UrlBasedCorsConfigurationSource configurationSource() {
  CorsConfiguration config = new CorsConfiguration();
  config.setAllowCredentials(true);
  // Modify allowed origins if you run the client at a different endpoint
  config.addAllowedOrigin("http://localhost:8080");
  config.addAllowedHeader("*");
  config.addAllowedMethod("*");
  UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  source.registerCorsConfiguration("/**", config);
  return source;
}
 
Example 13
Source File: CorsConfig.java    From dts-shop with GNU Lesser General Public License v3.0 5 votes vote down vote up
private CorsConfiguration buildConfig() {
	CorsConfiguration corsConfiguration = new CorsConfiguration();
	corsConfiguration.addAllowedOrigin("*"); // 1 设置访问源地址
	corsConfiguration.addAllowedHeader("*"); // 2 设置访问源请求头
	corsConfiguration.addAllowedMethod("*"); // 3 设置访问源请求方法
	return corsConfiguration;
}
 
Example 14
Source File: GateWayCorsConfig.java    From cloud-template with MIT License 5 votes vote down vote up
@Bean
public CorsFilter corsFilter() {
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    final CorsConfiguration corsConfiguration = new CorsConfiguration();
    corsConfiguration.setAllowCredentials(true);
    corsConfiguration.addAllowedHeader("*");
    corsConfiguration.addAllowedOrigin("*");
    corsConfiguration.addAllowedMethod("*");
    source.registerCorsConfiguration("/**", corsConfiguration);
    return new CorsFilter(source);
}
 
Example 15
Source File: CorsConfigration.java    From microservices-platform with Apache License 2.0 5 votes vote down vote up
@Bean
public CorsFilter corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    // #允许向该服务器提交请求的URI,*表示全部允许
    config.addAllowedOrigin(CorsConfiguration.ALL);
    // 允许cookies跨域
    config.setAllowCredentials(true);
    // #允许访问的头信息,*表示全部
    config.addAllowedHeader(CorsConfiguration.ALL);
    // 允许提交请求的方法,*表示全部允许
    config.addAllowedMethod(CorsConfiguration.ALL);
    source.registerCorsConfiguration("/**", config);
    return new CorsFilter(source);
}
 
Example 16
Source File: ApplicationConfig.java    From ZTuoExchange_framework with MIT License 5 votes vote down vote up
@Bean
public FilterRegistrationBean corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.addAllowedOrigin("*");
    config.setAllowCredentials(true);
    config.addAllowedHeader("*");
    config.addAllowedMethod("*");
    source.registerCorsConfiguration("/**", config);
    FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
    bean.setOrder(0);
    return bean;
}
 
Example 17
Source File: CorsConfig.java    From open-capacity-platform with Apache License 2.0 5 votes vote down vote up
/**
 * 跨域支持
 *
 * @return
 */
@Bean
public CorsFilter corsFilter() {
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    final CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true); // 允许cookies跨域
    config.addAllowedOrigin("*");// #允许向该服务器提交请求的URI,*表示全部允许
    config.addAllowedHeader("*");// #允许访问的头信息,*表示全部
    config.setMaxAge(18000L);// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了
    config.addAllowedMethod("*");// 允许提交请求的方法,*表示全部允许
    source.registerCorsConfiguration("/**", config);
    return new CorsFilter(source);
}
 
Example 18
Source File: Application.java    From production-ready-microservices-starter with MIT License 5 votes vote down vote up
@Bean
public FilterRegistrationBean corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");
    config.addAllowedMethod("*");
    source.registerCorsConfiguration("/**", config);

    FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
    bean.setOrder(0);
    return bean;
}
 
Example 19
Source File: Application.java    From servicecomb-toolkit with Apache License 2.0 5 votes vote down vote up
@Bean
public CorsFilter corsFilter() {
    final CorsConfiguration config = new CorsConfiguration();
    //config.setAllowCredentials(true);
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");
    config.addAllowedMethod("*");

    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/api/**", config);

    return new CorsFilter(source);
}
 
Example 20
Source File: SecurityConfigurer.java    From ywh-frame with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 这块是配置跨域请求的
 * @return Cors过滤器
 */
@Bean
public CorsFilter corsFilter() {
    final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
    final CorsConfiguration cors = new CorsConfiguration();
    cors.setAllowCredentials(true);
    cors.addAllowedOrigin("*");
    cors.addAllowedHeader("*");
    cors.addAllowedMethod("*");
    urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", cors);
    return new CorsFilter(urlBasedCorsConfigurationSource);
}