Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Enable CORS in Spark Java to allow origins *
options("/*",
(request, response) -> {
String accessControlRequestHeaders = request
.headers("Access-Control-Request-Headers");
if (accessControlRequestHeaders != null) {
response.header("Access-Control-Allow-Headers",
accessControlRequestHeaders);
}
String accessControlRequestMethod = request
.headers("Access-Control-Request-Method");
if (accessControlRequestMethod != null) {
response.header("Access-Control-Allow-Methods",
accessControlRequestMethod);
}
return "OK";
});
before((request, response) -> response.header("Access-Control-Allow-Origin", "*"));
@pkjc

This comment has been minimized.

Copy link

@pkjc pkjc commented Mar 1, 2018

Thanks 👍

@Myolisi

This comment has been minimized.

Copy link

@Myolisi Myolisi commented Mar 8, 2018

Thanks!

@ajesk

This comment has been minimized.

Copy link

@ajesk ajesk commented Mar 14, 2018

thank you!!!!!

@mustafakibar

This comment has been minimized.

Copy link

@mustafakibar mustafakibar commented Mar 21, 2018

thanks

@estebgonza

This comment has been minimized.

Copy link

@estebgonza estebgonza commented Jul 13, 2018

Thank's !

@samritbk

This comment has been minimized.

Copy link

@samritbk samritbk commented Jul 21, 2018

Thanks.

@JavaSparker

This comment has been minimized.

Copy link

@JavaSparker JavaSparker commented Jul 27, 2018

Hello,
I try to execute Spark-submit from Angular post http and given problem CROS:

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://185.28.5.2:4200' is therefore not allowed access.

Any help plaise

@arterzatij

This comment has been minimized.

Copy link

@arterzatij arterzatij commented Sep 12, 2018

@JavaSparker no problem with the following configuration

            BasicConfigurator.configure();
	
	Spark.staticFiles.location("/assets");
	Spark.staticFiles.header("Access-Control-Allow-Origin", "*");
	
	options("/*", (req, res) -> {
		String accessControlRequestHeaders = req.headers("Access-Control-Request-Headers");
		if (accessControlRequestHeaders != null) {
			res.header("Access-Control-Allow-Headers", accessControlRequestHeaders);
		}

		String accessControlRequestMethod = req.headers("Access-Control-Request-Method");
		if (accessControlRequestMethod != null) {
			res.header("Access-Control-Allow-Methods", accessControlRequestMethod);
		}

        return "OK";
	});

	before((req, res) -> {
		res.header("Access-Control-Allow-Origin", "*");
		res.header("Access-Control-Allow-Headers", "*");
		res.type("application/json");
	});
@liamweldon

This comment has been minimized.

Copy link

@liamweldon liamweldon commented Apr 7, 2019

ilysm

@vaghi

This comment has been minimized.

Copy link

@vaghi vaghi commented May 29, 2019

Love you! haha

@activated

This comment has been minimized.

Copy link

@activated activated commented Jan 6, 2020

thanks!

@chiquito83

This comment has been minimized.

Copy link

@chiquito83 chiquito83 commented Jun 28, 2020

Thank you :)

@saravanasksk

This comment has been minimized.

Copy link

@saravanasksk saravanasksk commented Sep 2, 2021

thanks it's working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment