当前位置: 首页>>代码示例>>Java>>正文


Java CorsConfiguration.setAllowedOrigins方法代码示例

本文整理汇总了Java中org.springframework.web.cors.CorsConfiguration.setAllowedOrigins方法的典型用法代码示例。如果您正苦于以下问题:Java CorsConfiguration.setAllowedOrigins方法的具体用法?Java CorsConfiguration.setAllowedOrigins怎么用?Java CorsConfiguration.setAllowedOrigins使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.springframework.web.cors.CorsConfiguration的用法示例。


在下文中一共展示了CorsConfiguration.setAllowedOrigins方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: casCorsFilter

import org.springframework.web.cors.CorsConfiguration; //导入方法依赖的package包/类
@ConditionalOnProperty(prefix = "cas.httpWebRequest.cors", name = "enabled", havingValue = "true")
@Bean
@RefreshScope
public FilterRegistrationBean casCorsFilter() {
    final HttpWebRequestProperties.Cors cors = casProperties.getHttpWebRequest().getCors();
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    final CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(cors.isEnabled());
    config.setAllowedOrigins(cors.getAllowOrigins());
    config.setAllowedMethods(cors.getAllowMethods());
    config.setAllowedHeaders(cors.getAllowHeaders());
    config.setMaxAge(cors.getMaxAge());
    config.setExposedHeaders(cors.getExposedHeaders());
    source.registerCorsConfiguration("/**", config);
    final FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
    bean.setName("casCorsFilter");
    bean.setAsyncSupported(true);
    bean.setOrder(0);
    return bean;
}
 
开发者ID:mrluo735,项目名称:cas-5.1.0,代码行数:21,代码来源:CasFiltersConfiguration.java

示例2: corsFilterBean

import org.springframework.web.cors.CorsConfiguration; //导入方法依赖的package包/类
/**
 * <a href="https://github.com/spring-projects/spring-boot/issues/5834#issuecomment-296370088">See explanation here</a>
 */
@Bean
public FilterRegistrationBean corsFilterBean() {
	final CorsConfiguration configuration = new CorsConfiguration();
	configuration.setAllowedOrigins(Collections.singletonList("*"));
	configuration.setAllowedMethods(Arrays.asList("HEAD", "GET", "POST", "PUT", "DELETE", "PATCH"));
	// setAllowCredentials(true) is important, otherwise:
	// The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.
	configuration.setAllowCredentials(true);
	// setAllowedHeaders is important! Without it, OPTIONS preflight request
	// will fail with 403 Invalid CORS request
	configuration.setAllowedHeaders(Arrays.asList("Authorization", "Cache-Control", "Content-Type"));
	final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
	source.registerCorsConfiguration("/**", configuration);
	FilterRegistrationBean corsFilter = new FilterRegistrationBean(new CorsFilter(source));
	corsFilter.setOrder(Ordered.HIGHEST_PRECEDENCE);
	return corsFilter;
}
 
开发者ID:codenergic,项目名称:theskeleton,代码行数:21,代码来源:WebSecurityConfig.java

示例3: corsFilter

import org.springframework.web.cors.CorsConfiguration; //导入方法依赖的package包/类
@Bean
public CorsFilter corsFilter() {
    return new CorsFilter(request -> {
        String pathInfo = request.getPathInfo();
        if (pathInfo != null &&
            (pathInfo.endsWith("/swagger.json") ||
             pathInfo.endsWith("/swagger.yaml"))) {
            return new CorsConfiguration().applyPermitDefaultValues();
        }

        CorsConfiguration config = new CorsConfiguration();
        config.setAllowedOrigins(allowedOrigins);
        config.setAllowedMethods(Arrays.asList("HEAD", "GET", "POST", "PUT", "DELETE", "PATCH"));
        config.applyPermitDefaultValues();
        return config;
    });
}
 
开发者ID:syndesisio,项目名称:syndesis,代码行数:18,代码来源:SyndesisCorsConfiguration.java

示例4: corsConfigurationSource

import org.springframework.web.cors.CorsConfiguration; //导入方法依赖的package包/类
@Bean
public CorsConfigurationSource corsConfigurationSource() {
	CorsConfiguration configuration = new CorsConfiguration();
	configuration.setAllowedOrigins(ImmutableList.of("*"));
	configuration.setAllowedMethods(ImmutableList.of("*"));
	configuration.setAllowCredentials(true);

	configuration.setAllowedHeaders(ImmutableList.of("*"));
	UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
	source.registerCorsConfiguration("/**", configuration);

	return source;
}
 
开发者ID:peavers,项目名称:swordfish-service,代码行数:14,代码来源:SecurityConfig.java

示例5: initCorsFilter

import org.springframework.web.cors.CorsConfiguration; //导入方法依赖的package包/类
/**
 * CORS:
 * <p>
 * Do not do any of below, which are the wrong way to attempt solving the ajax problem:
 * - http.authorizeRequests().antMatchers(HttpMethod.OPTIONS, "/**").permitAll();
 * - web.ignoring().antMatchers(HttpMethod.OPTIONS)
 * <p>
 * Global CORS configuration
 * https://spring.io/blog/2015/06/08/cors-support-in-spring-framework
 * https://docs.spring.io/spring-security/site/docs/current/reference/html/cors.html
 * <p>
 * Solution 1
 * add CrossOrigin annotation to Controller class or methods
 * <p>
 * Solution 2
 * override addCorsMappings(CorsRegistry registry) method of WebMvcConfigurerAdapter class
 * <p>
 * <p>
 * The follow method will override CORS Configuration provided by Spring MVC.
 */
@Bean
public FilterRegistrationBean initCorsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();

    // setAllowCredentials(true) is important, otherwise:
    // The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.
    config.setAllowCredentials(true);

    // setAllowedHeaders is important! Without it, OPTIONS preflight request
    // will fail with 403 Invalid CORS request
    config.setAllowedHeaders(ImmutableList.of("Authorization", "Cache-Control", "Content-Type"));

    config.addAllowedMethod("*");

    String origins = this.applicationConfig.getAllowedOrigins();
    if (origins != null && !"".equals(origins)) {
        config.setAllowedOrigins(Arrays.asList(StringHelper.splitWithoutWhitespace(origins, ",")));
    }
    source.registerCorsConfiguration("/**", config);
    FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
    bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
    return bean;
}
 
开发者ID:bndynet,项目名称:web-framework-for-java,代码行数:45,代码来源:WebSecurityConfig.java

示例6: getCorsConfiguration

import org.springframework.web.cors.CorsConfiguration; //导入方法依赖的package包/类
private CorsConfiguration getCorsConfiguration(EndpointCorsProperties properties) {
	if (CollectionUtils.isEmpty(properties.getAllowedOrigins())) {
		return null;
	}
	CorsConfiguration configuration = new CorsConfiguration();
	configuration.setAllowedOrigins(properties.getAllowedOrigins());
	if (!CollectionUtils.isEmpty(properties.getAllowedHeaders())) {
		configuration.setAllowedHeaders(properties.getAllowedHeaders());
	}
	if (!CollectionUtils.isEmpty(properties.getAllowedMethods())) {
		configuration.setAllowedMethods(properties.getAllowedMethods());
	}
	if (!CollectionUtils.isEmpty(properties.getExposedHeaders())) {
		configuration.setExposedHeaders(properties.getExposedHeaders());
	}
	if (properties.getMaxAge() != null) {
		configuration.setMaxAge(properties.getMaxAge());
	}
	if (properties.getAllowCredentials() != null) {
		configuration.setAllowCredentials(properties.getAllowCredentials());
	}
	return configuration;
}
 
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:24,代码来源:EndpointWebMvcManagementContextConfiguration.java

示例7: corsFilter

import org.springframework.web.cors.CorsConfiguration; //导入方法依赖的package包/类
@Bean
public CorsFilter corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration corsConfiguration = new CorsConfiguration();

    corsConfiguration.setAllowedOrigins(Arrays.asList("http://localhost:4200"));
    corsConfiguration.setAllowedMethods(Arrays.asList(CorsConfiguration.ALL));
    corsConfiguration.setAllowedHeaders(Arrays.asList(CorsConfiguration.ALL));
    corsConfiguration.addExposedHeader(SecurityConstant.HEADER_SECURITY_TOKEN);
    corsConfiguration.setAllowCredentials(true);

    source.registerCorsConfiguration("/**", corsConfiguration);
    return new CorsFilter(source);
}
 
开发者ID:yuexine,项目名称:loafer,代码行数:15,代码来源:WebConfiguration.java

示例8: corsConfigurationSource

import org.springframework.web.cors.CorsConfiguration; //导入方法依赖的package包/类
protected CorsConfigurationSource corsConfigurationSource() {
    final CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowCredentials(true);
    configuration.setAllowedOrigins(Arrays.asList("*"));
    configuration.setAllowedMethods(Arrays.asList("GET", "POST", "DELETE", "PUT"));
    configuration.setAllowedHeaders(Arrays.asList("X-Requested-With", "Authorization"));
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", configuration);
    return source;
}
 
开发者ID:panga,项目名称:unibave-backend,代码行数:11,代码来源:WebSecurityConfig.java

示例9: initCorsConfiguration

import org.springframework.web.cors.CorsConfiguration; //导入方法依赖的package包/类
@Override
protected CorsConfiguration initCorsConfiguration(Object handler, Method method, RequestMappingInfo mappingInfo) {
	HandlerMethod handlerMethod = createHandlerMethod(handler, method);
	CrossOrigin typeAnnotation = AnnotatedElementUtils.findMergedAnnotation(handlerMethod.getBeanType(), CrossOrigin.class);
	CrossOrigin methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, CrossOrigin.class);

	if (typeAnnotation == null && methodAnnotation == null) {
		return null;
	}

	CorsConfiguration config = new CorsConfiguration();
	updateCorsConfig(config, typeAnnotation);
	updateCorsConfig(config, methodAnnotation);

	if (CollectionUtils.isEmpty(config.getAllowedOrigins())) {
		config.setAllowedOrigins(Arrays.asList(CrossOrigin.DEFAULT_ORIGINS));
	}
	if (CollectionUtils.isEmpty(config.getAllowedMethods())) {
		for (RequestMethod allowedMethod : mappingInfo.getMethodsCondition().getMethods()) {
			config.addAllowedMethod(allowedMethod.name());
		}
	}
	if (CollectionUtils.isEmpty(config.getAllowedHeaders())) {
		config.setAllowedHeaders(Arrays.asList(CrossOrigin.DEFAULT_ALLOWED_HEADERS));
	}
	if (config.getAllowCredentials() == null) {
		config.setAllowCredentials(CrossOrigin.DEFAULT_ALLOW_CREDENTIALS);
	}
	if (config.getMaxAge() == null) {
		config.setMaxAge(CrossOrigin.DEFAULT_MAX_AGE);
	}
	return config;
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:34,代码来源:RequestMappingHandlerMapping.java

示例10: corsConfigurationSource

import org.springframework.web.cors.CorsConfiguration; //导入方法依赖的package包/类
@Bean
public CorsConfigurationSource corsConfigurationSource() {
    final CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowedOrigins(ImmutableList.of("http://localhost:4200"));
    configuration.setAllowedMethods(Arrays.asList(CORS_ALLOWED_METHODS));
    configuration.setAllowCredentials(false);
    configuration.setAllowedHeaders(Arrays.asList(CORS_ALLOW_HEADERS));

    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", configuration);
    return source;
}
 
开发者ID:vlsidlyarevich,项目名称:unity,代码行数:13,代码来源:SecurityConfig.java

示例11: corsConfigurationSource

import org.springframework.web.cors.CorsConfiguration; //导入方法依赖的package包/类
/**
 * CorsConfigurationSource bean initializer.
 * @return cors configuration
 */
@Bean
public CorsConfigurationSource corsConfigurationSource() {
  UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  if (allowedOrigins.length > 0) {
    CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowedOrigins(Arrays.asList(allowedOrigins));
    configuration.setAllowedMethods(Arrays.asList(allowedMethods));
    source.registerCorsConfiguration("/**", configuration);
  }
  return source;
}
 
开发者ID:OpenLMIS,项目名称:openlmis-stockmanagement,代码行数:16,代码来源:ResourceServerSecurityConfiguration.java

示例12: initCorsConfiguration

import org.springframework.web.cors.CorsConfiguration; //导入方法依赖的package包/类
@Override
protected CorsConfiguration initCorsConfiguration(Object handler, Method method, String mapping) {
	CorsConfiguration corsConfig = new CorsConfiguration();
	corsConfig.setAllowedOrigins(Collections.singletonList("http://" + handler.hashCode() + method.getName()));
	return corsConfig;
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:7,代码来源:HandlerMethodMappingTests.java


注:本文中的org.springframework.web.cors.CorsConfiguration.setAllowedOrigins方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。