Java Code Examples for io.vertx.ext.web.handler.CorsHandler#allowedMethod()

The following examples show how to use io.vertx.ext.web.handler.CorsHandler#allowedMethod() . 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: RestRouter.java    From rest.vertx with Apache License 2.0 6 votes vote down vote up
/**
 * @param router               to add handler to
 * @param allowedOriginPattern origin pattern
 * @param allowCredentials     allowed credentials
 * @param maxAge               in seconds
 * @param allowedHeaders       set of headers or null for none
 * @param methods              list of methods or empty for all
 */
public static void enableCors(Router router,
                       String allowedOriginPattern,
                       boolean allowCredentials,
                       int maxAge,
                       Set<String> allowedHeaders,
                       HttpMethod... methods) {

	CorsHandler handler = CorsHandler.create(allowedOriginPattern)
	                                 .allowCredentials(allowCredentials)
	                                 .maxAgeSeconds(maxAge);

	if (methods == null || methods.length == 0) { // if not given than all
		methods = HttpMethod.values();
	}

	for (HttpMethod method : methods) {
		handler.allowedMethod(method);
	}

	handler.allowedHeaders(allowedHeaders);
	router.route().order(ORDER_CORS_HANDLER).handler(handler);
}
 
Example 2
Source File: RestServerVerticle.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
/**
 * Support CORS
 */
void mountCorsHandler(Router mainRouter) {
  if (!TransportConfig.isCorsEnabled()) {
    return;
  }

  CorsHandler corsHandler = getCorsHandler(TransportConfig.getCorsAllowedOrigin());
  // Access-Control-Allow-Credentials
  corsHandler.allowCredentials(TransportConfig.isCorsAllowCredentials());
  // Access-Control-Allow-Headers
  corsHandler.allowedHeaders(TransportConfig.getCorsAllowedHeaders());
  // Access-Control-Allow-Methods
  Set<String> allowedMethods = TransportConfig.getCorsAllowedMethods();
  for (String method : allowedMethods) {
    corsHandler.allowedMethod(HttpMethod.valueOf(method));
  }
  // Access-Control-Expose-Headers
  corsHandler.exposedHeaders(TransportConfig.getCorsExposedHeaders());
  // Access-Control-Max-Age
  int maxAge = TransportConfig.getCorsMaxAge();
  if (maxAge >= 0) {
    corsHandler.maxAgeSeconds(maxAge);
  }

  LOGGER.info("mount CorsHandler");
  mainRouter.route().handler(corsHandler);
}