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


Java CorsConfiguration类代码示例

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


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

示例1: buildConfig

import org.springframework.web.cors.CorsConfiguration; //导入依赖的package包/类
/**
 * CORS配置
 * 
 * @return
 */
private CorsConfiguration buildConfig() {
	// 新建CORS配置
	CorsConfiguration corsConfiguration = new CorsConfiguration();
	// 设置允许访问的请求源HTTP
	corsConfiguration.addAllowedOrigin("*");
	// 设置允许访问的请求源方法
	corsConfiguration.addAllowedMethod("*");
	corsConfiguration.setAllowCredentials(true);
	// 添加了新的header,需要配置白名单
	List<String> allowedHeaders = Lists.newArrayList();
	allowedHeaders.add("Content-Type");
	allowedHeaders.add("authorization");
	allowedHeaders.add("x-requested-with");
	corsConfiguration.setAllowedHeaders(allowedHeaders);
	// 返回CORS配置
	return corsConfiguration;
}
 
开发者ID:sdc1234,项目名称:zhihu-spider,代码行数:23,代码来源:CorsConfig.java

示例2: filterRegistrationBean

import org.springframework.web.cors.CorsConfiguration; //导入依赖的package包/类
@Bean
public FilterRegistrationBean filterRegistrationBean() {
    final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();

    final CorsConfiguration corsConfiguration = new CorsConfiguration();
    corsConfiguration.setAllowCredentials(corsSupportProperties.isAllowCredentials());
    corsConfiguration.addAllowedOrigin(corsSupportProperties.getAllowOrigin());
    corsConfiguration.addAllowedHeader(corsSupportProperties.getAllowHeader());
    corsConfiguration.addAllowedMethod(corsSupportProperties.getAllowMethod());

    urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);

    CorsFilter corsFilter = new CorsFilter(urlBasedCorsConfigurationSource);
    FilterRegistrationBean registration = new FilterRegistrationBean(corsFilter);
    registration.addUrlPatterns("/*");
    registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
    return registration;
}
 
开发者ID:melthaw,项目名称:spring-backend-boilerplate,代码行数:19,代码来源:CorsSupportConfiguration.java

示例3: webSocketMapping

import org.springframework.web.cors.CorsConfiguration; //导入依赖的package包/类
@Bean
HandlerMapping webSocketMapping(CommentService commentService) {
	Map<String, WebSocketHandler> urlMap = new HashMap<>();
	urlMap.put("/topic/comments.new", commentService);

	Map<String, CorsConfiguration> corsConfigurationMap =
		new HashMap<>();
	CorsConfiguration corsConfiguration = new CorsConfiguration();
	corsConfiguration.addAllowedOrigin("http://localhost:8080");
	corsConfigurationMap.put(
		"/topic/comments.new", corsConfiguration);

	SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
	mapping.setOrder(10);
	mapping.setUrlMap(urlMap);
	mapping.setCorsConfigurations(corsConfigurationMap);

	return mapping;
}
 
开发者ID:PacktPublishing,项目名称:Learning-Spring-Boot-2.0-Second-Edition,代码行数:20,代码来源:WebSocketConfig.java

示例4: corsFilter

import org.springframework.web.cors.CorsConfiguration; //导入依赖的package包/类
@Bean
FilterRegistrationBean corsFilter() {
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");
    config.addAllowedMethod("*");

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

    FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
    bean.setOrder(Ordered.HIGHEST_PRECEDENCE);

    return bean;
}
 
开发者ID:BakkerTom,项目名称:happy-news,代码行数:17,代码来源:AuthorizationServerConfig.java

示例5: webSocketMapping

import org.springframework.web.cors.CorsConfiguration; //导入依赖的package包/类
@Bean
HandlerMapping webSocketMapping(CommentService commentService,
								InboundChatService inboundChatService,
								OutboundChatService outboundChatService) {
	SimpleUrlHandlerMapping mapping =
		configureUrlMappings(commentService, inboundChatService, outboundChatService);

	Map<String, CorsConfiguration> corsConfigurationMap = new HashMap<>();
	CorsConfiguration corsConfiguration = new CorsConfiguration();
	corsConfiguration.addAllowedOrigin(chatConfigProperties.getOrigin());

	mapping.getUrlMap().keySet().forEach(route ->
		corsConfigurationMap.put(route, corsConfiguration)
	);

	mapping.setCorsConfigurations(corsConfigurationMap);

	return mapping;
}
 
开发者ID:PacktPublishing,项目名称:Learning-Spring-Boot-2.0-Second-Edition,代码行数:20,代码来源:WebSocketConfig.java

示例6: 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

示例7: 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

示例8: corsFilter

import org.springframework.web.cors.CorsConfiguration; //导入依赖的package包/类
@Bean
public FilterRegistrationBean corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");
    config.addAllowedMethod("OPTIONS");
    config.addAllowedMethod("HEAD");
    config.addAllowedMethod("GET");
    config.addAllowedMethod("PUT");
    config.addAllowedMethod("POST");
    config.addAllowedMethod("DELETE");
    config.addAllowedMethod("PATCH");
    source.registerCorsConfiguration("/**", config);
    // return new CorsFilter(source);
    final FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
    bean.setOrder(0);
    return bean;
}
 
开发者ID:lorderikir,项目名称:googlecloud-techtalk,代码行数:21,代码来源:CorsConfig.java

示例9: 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

示例10: corsFilter

import org.springframework.web.cors.CorsConfiguration; //导入依赖的package包/类
@Bean
public CorsFilter corsFilter() {

    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    final CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);

    // TODO - Lock down to specific host in Prod
    config.addAllowedOrigin("*");
    //config.addAllowedOrigin("http://localhost:3000");

    config.addAllowedHeader("*");
    config.addAllowedMethod("OPTIONS");
    config.addAllowedMethod("GET");
    config.addAllowedMethod("POST");
    config.addAllowedMethod("PUT");
    config.addAllowedMethod("DELETE");
    source.registerCorsConfiguration("/**", config);
    return new CorsFilter(source);
}
 
开发者ID:gazbert,项目名称:bxbot-ui-server,代码行数:21,代码来源:RestCorsConfig.java

示例11: 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

示例12: corsFilter

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

    config.setAllowCredentials(true);
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");

    config.addExposedHeader("WWW-Authenticate");
    config.addExposedHeader("Access-Control-Allow-Origin");
    config.addExposedHeader("Access-Control-Allow-Headers");

    config.addAllowedMethod("OPTIONS");
    config.addAllowedMethod("HEAD");
    config.addAllowedMethod("GET");
    config.addAllowedMethod("PUT");
    config.addAllowedMethod("POST");
    config.addAllowedMethod("DELETE");
    config.addAllowedMethod("PATCH");
    source.registerCorsConfiguration("/**", config);

    final FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
    bean.setOrder(0);
    return bean;
}
 
开发者ID:evoila,项目名称:cfsummiteu2017,代码行数:27,代码来源:BaseConfiguration.java

示例13: getCorsFilter

import org.springframework.web.cors.CorsConfiguration; //导入依赖的package包/类
private CorsFilter getCorsFilter() {

    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");
    config.addAllowedMethod("OPTIONS");
    config.addAllowedMethod("HEAD");
    config.addAllowedMethod("GET");
    config.addAllowedMethod("PUT");
    config.addAllowedMethod("POST");
    config.addAllowedMethod("DELETE");
    config.addAllowedMethod("PATCH");
    source.registerCorsConfiguration("/**", config);
    return new CorsFilter(source);
  }
 
开发者ID:oasp,项目名称:oasp-tutorial-sources,代码行数:18,代码来源:BaseWebSecurityConfig.java

示例14: corsFilter

import org.springframework.web.cors.CorsConfiguration; //导入依赖的package包/类
@Bean
public FilterRegistrationBean corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");
    config.addAllowedMethod("OPTIONS");
    config.addAllowedMethod("HEAD");
    config.addAllowedMethod("GET");
    config.addAllowedMethod("PUT");
    config.addAllowedMethod("POST");
    config.addAllowedMethod("DELETE");
    config.addAllowedMethod("PATCH");
    source.registerCorsConfiguration("/**", config);
    final FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
    bean.setOrder(0);
    return bean;
}
 
开发者ID:fku233,项目名称:Plum,代码行数:20,代码来源:CorsConfig.java

示例15: 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


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