CrossOriginFilter with Jersey and embedded JettyHow to handle CORS using JAX-RS with JerseyCross Origin Filter with embedded JettyServing static files with embedded JettyHow to configure embedded jetty to access Jersey resources?Using Jersey in Embedded JettyJetty Embedded, Jersey 2, WeldEmbedded Jetty+Jersey injectionBest practice for REST token-based authentication with JAX-RS and JerseyJersey with embedded Jetty serverJetty server configurationJersey SecurityContext.isUserInRole always false when running on Jetty EmbeddedStrange exception using Jersey with embedded Jetty
How to write a sincerely religious protagonist without preaching or affirming or judging their worldview?
Why are off grid solar setups only 12, 24, 48 VDC?
What is the lowest-speed bogey a jet fighter can intercept/escort?
How do professional electronic musicians/sound engineers combat listening fatigue?
On the strategic interest of giving long lasting stock orders
How were the LM astronauts supported during the moon landing and ascent? What were the max G's on them during these phases?
What was the rationale behind 36 bit computer architectures?
Airplanes in static display at Whiteman AFB
Character is called by their first initial. How do I write it?
Is it legal to use cash pulled from a credit card to pay the monthly payment on that credit card?
Does the Intel 8086 CPU have user mode and kernel mode?
Why is chess failing to attract big name sponsors?
What causes long-running disputes over sovereignty?
Which Roman general was killed by his own soldiers for not letting them to loot a newly conquered city?
What exactly makes a General Products hull nearly indestructible?
Creating Darkness
How to copy a file transactionally?
Trapped in an ocean Temple in Minecraft?
Is it better to memorize verb's 1st person perfect tense?
Why was Sauron not trying to find the Ring, and instead of preparing for war?
Strange Cron Job takes up 100% of CPU Ubuntu 18 LTS Server
powerhouse of ideas
Reduce column width of table while also aligning values at decimal point
Where to place an artificial gland in the human body?
CrossOriginFilter with Jersey and embedded Jetty
How to handle CORS using JAX-RS with JerseyCross Origin Filter with embedded JettyServing static files with embedded JettyHow to configure embedded jetty to access Jersey resources?Using Jersey in Embedded JettyJetty Embedded, Jersey 2, WeldEmbedded Jetty+Jersey injectionBest practice for REST token-based authentication with JAX-RS and JerseyJersey with embedded Jetty serverJetty server configurationJersey SecurityContext.isUserInRole always false when running on Jetty EmbeddedStrange exception using Jersey with embedded Jetty
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I know this question and that on however they were not answered and asked 4 years ago. Further, non of the answers worked for me.
I am unable to add a crossOriginFilter to my embedded jetty server.
My pom
<!-- Jetty -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.2.11.v20150529</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.2.11.v20150529</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>9.2.11.v20150529</version>
</dependency>
My code - unfortunatly I do not get any Header field in the responses!
ServletContextHandler dynamicResourceContext = new ServletContextHandler();
dynamicResourceContext.setContextPath("/rest");
FilterHolder holder = new FilterHolder(CrossOriginFilter.class);
holder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*");
holder.setInitParameter(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*");
holder.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET,POST,HEAD");
holder.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "X-Requested-With,Content-Type,Accept,Origin");
dynamicResourceContext.addFilter(holder, "/*", EnumSet.of(DispatcherType.REQUEST));
ServletContextHandler staticResourceContext = new ServletContextHandler();
staticResourceContext.setContextPath("/resources");
DefaultServlet defaultServlet = new DefaultServlet();
ServletHolder holderPwd = new ServletHolder("default", defaultServlet);
holderPwd.setInitParameter("resourceBase", "./src/webapp/");
staticResourceContext.addServlet(holderPwd, "/*");
HandlerList handlers = new HandlerList();
handlers.addHandler(dynamicResourceContext);
handlers.addHandler(staticResourceContext);
server = new Server(port);
server.setHandler(handlers);
// set logging to console
StdErrLog logger = new StdErrLog();
logger.setDebugEnabled(webserverLogging);
Log.setLog(logger);
ServletHolder jerseyServlet = dynamicResourceContext
.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
// Tells the Jersey Servlet which REST service/class to load.
jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", getMyClasses());
try
server.start();
catch (Exception e)
e.printStackTrace();
finally
// server.destroy();
What do I wrong? I do not get any error message!
EDIT
Also the following tutorial is not working. Neither with Postman nor with chrome I see an additional response head entry.
The response looks like the following:
HTTP/1.1 200 OK
Date: Tue, 26 Mar 2019 19:41:36 GMT
Content-Length: 0
Server: Jetty(9.4.15.v20190215)
EDIT
I was able to create the header fields using a Resource Configuration but I am still unable to create them with the CrossOriginFilter.
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
Server jettyServer = new Server(9998);
jettyServer.setHandler(context);
ResourceConfig webapiResourceConfig = new ResourceConfig();
webapiResourceConfig.register(CorsFilter.class);
ServletHolder jerseyServlet = new ServletHolder(new ServletContainer(webapiResourceConfig));
context.addServlet(jerseyServlet, "/*");
//context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
jerseyServlet.setInitParameter( "jersey.config.server.provider.classnames",MyServerConfig.class.getCanonicalName());
jetty jersey-2.0 embedded-jetty
add a comment |
I know this question and that on however they were not answered and asked 4 years ago. Further, non of the answers worked for me.
I am unable to add a crossOriginFilter to my embedded jetty server.
My pom
<!-- Jetty -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.2.11.v20150529</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.2.11.v20150529</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>9.2.11.v20150529</version>
</dependency>
My code - unfortunatly I do not get any Header field in the responses!
ServletContextHandler dynamicResourceContext = new ServletContextHandler();
dynamicResourceContext.setContextPath("/rest");
FilterHolder holder = new FilterHolder(CrossOriginFilter.class);
holder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*");
holder.setInitParameter(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*");
holder.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET,POST,HEAD");
holder.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "X-Requested-With,Content-Type,Accept,Origin");
dynamicResourceContext.addFilter(holder, "/*", EnumSet.of(DispatcherType.REQUEST));
ServletContextHandler staticResourceContext = new ServletContextHandler();
staticResourceContext.setContextPath("/resources");
DefaultServlet defaultServlet = new DefaultServlet();
ServletHolder holderPwd = new ServletHolder("default", defaultServlet);
holderPwd.setInitParameter("resourceBase", "./src/webapp/");
staticResourceContext.addServlet(holderPwd, "/*");
HandlerList handlers = new HandlerList();
handlers.addHandler(dynamicResourceContext);
handlers.addHandler(staticResourceContext);
server = new Server(port);
server.setHandler(handlers);
// set logging to console
StdErrLog logger = new StdErrLog();
logger.setDebugEnabled(webserverLogging);
Log.setLog(logger);
ServletHolder jerseyServlet = dynamicResourceContext
.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
// Tells the Jersey Servlet which REST service/class to load.
jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", getMyClasses());
try
server.start();
catch (Exception e)
e.printStackTrace();
finally
// server.destroy();
What do I wrong? I do not get any error message!
EDIT
Also the following tutorial is not working. Neither with Postman nor with chrome I see an additional response head entry.
The response looks like the following:
HTTP/1.1 200 OK
Date: Tue, 26 Mar 2019 19:41:36 GMT
Content-Length: 0
Server: Jetty(9.4.15.v20190215)
EDIT
I was able to create the header fields using a Resource Configuration but I am still unable to create them with the CrossOriginFilter.
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
Server jettyServer = new Server(9998);
jettyServer.setHandler(context);
ResourceConfig webapiResourceConfig = new ResourceConfig();
webapiResourceConfig.register(CorsFilter.class);
ServletHolder jerseyServlet = new ServletHolder(new ServletContainer(webapiResourceConfig));
context.addServlet(jerseyServlet, "/*");
//context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
jerseyServlet.setInitParameter( "jersey.config.server.provider.classnames",MyServerConfig.class.getCanonicalName());
jetty jersey-2.0 embedded-jetty
1
You shouldn't see the headers in Postman, as Postman doesn't require CORS support. And in Chrome, you should only see them if you are actually making a cross origin request. If the filter is implemented correctly, it should only spit out CORS response headers if there is an Origin request header. And that should only happen on cross origin requests made from a browser.
– Paul Samsotha
Mar 27 at 4:56
You are right: if i set the origin in the request head then I get the required response
– user3579222
Mar 27 at 6:22
Please, post your comment as an answer - I will accept it!
– user3579222
Mar 27 at 14:27
add a comment |
I know this question and that on however they were not answered and asked 4 years ago. Further, non of the answers worked for me.
I am unable to add a crossOriginFilter to my embedded jetty server.
My pom
<!-- Jetty -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.2.11.v20150529</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.2.11.v20150529</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>9.2.11.v20150529</version>
</dependency>
My code - unfortunatly I do not get any Header field in the responses!
ServletContextHandler dynamicResourceContext = new ServletContextHandler();
dynamicResourceContext.setContextPath("/rest");
FilterHolder holder = new FilterHolder(CrossOriginFilter.class);
holder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*");
holder.setInitParameter(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*");
holder.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET,POST,HEAD");
holder.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "X-Requested-With,Content-Type,Accept,Origin");
dynamicResourceContext.addFilter(holder, "/*", EnumSet.of(DispatcherType.REQUEST));
ServletContextHandler staticResourceContext = new ServletContextHandler();
staticResourceContext.setContextPath("/resources");
DefaultServlet defaultServlet = new DefaultServlet();
ServletHolder holderPwd = new ServletHolder("default", defaultServlet);
holderPwd.setInitParameter("resourceBase", "./src/webapp/");
staticResourceContext.addServlet(holderPwd, "/*");
HandlerList handlers = new HandlerList();
handlers.addHandler(dynamicResourceContext);
handlers.addHandler(staticResourceContext);
server = new Server(port);
server.setHandler(handlers);
// set logging to console
StdErrLog logger = new StdErrLog();
logger.setDebugEnabled(webserverLogging);
Log.setLog(logger);
ServletHolder jerseyServlet = dynamicResourceContext
.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
// Tells the Jersey Servlet which REST service/class to load.
jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", getMyClasses());
try
server.start();
catch (Exception e)
e.printStackTrace();
finally
// server.destroy();
What do I wrong? I do not get any error message!
EDIT
Also the following tutorial is not working. Neither with Postman nor with chrome I see an additional response head entry.
The response looks like the following:
HTTP/1.1 200 OK
Date: Tue, 26 Mar 2019 19:41:36 GMT
Content-Length: 0
Server: Jetty(9.4.15.v20190215)
EDIT
I was able to create the header fields using a Resource Configuration but I am still unable to create them with the CrossOriginFilter.
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
Server jettyServer = new Server(9998);
jettyServer.setHandler(context);
ResourceConfig webapiResourceConfig = new ResourceConfig();
webapiResourceConfig.register(CorsFilter.class);
ServletHolder jerseyServlet = new ServletHolder(new ServletContainer(webapiResourceConfig));
context.addServlet(jerseyServlet, "/*");
//context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
jerseyServlet.setInitParameter( "jersey.config.server.provider.classnames",MyServerConfig.class.getCanonicalName());
jetty jersey-2.0 embedded-jetty
I know this question and that on however they were not answered and asked 4 years ago. Further, non of the answers worked for me.
I am unable to add a crossOriginFilter to my embedded jetty server.
My pom
<!-- Jetty -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.2.11.v20150529</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.2.11.v20150529</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>9.2.11.v20150529</version>
</dependency>
My code - unfortunatly I do not get any Header field in the responses!
ServletContextHandler dynamicResourceContext = new ServletContextHandler();
dynamicResourceContext.setContextPath("/rest");
FilterHolder holder = new FilterHolder(CrossOriginFilter.class);
holder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*");
holder.setInitParameter(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*");
holder.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET,POST,HEAD");
holder.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "X-Requested-With,Content-Type,Accept,Origin");
dynamicResourceContext.addFilter(holder, "/*", EnumSet.of(DispatcherType.REQUEST));
ServletContextHandler staticResourceContext = new ServletContextHandler();
staticResourceContext.setContextPath("/resources");
DefaultServlet defaultServlet = new DefaultServlet();
ServletHolder holderPwd = new ServletHolder("default", defaultServlet);
holderPwd.setInitParameter("resourceBase", "./src/webapp/");
staticResourceContext.addServlet(holderPwd, "/*");
HandlerList handlers = new HandlerList();
handlers.addHandler(dynamicResourceContext);
handlers.addHandler(staticResourceContext);
server = new Server(port);
server.setHandler(handlers);
// set logging to console
StdErrLog logger = new StdErrLog();
logger.setDebugEnabled(webserverLogging);
Log.setLog(logger);
ServletHolder jerseyServlet = dynamicResourceContext
.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
// Tells the Jersey Servlet which REST service/class to load.
jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", getMyClasses());
try
server.start();
catch (Exception e)
e.printStackTrace();
finally
// server.destroy();
What do I wrong? I do not get any error message!
EDIT
Also the following tutorial is not working. Neither with Postman nor with chrome I see an additional response head entry.
The response looks like the following:
HTTP/1.1 200 OK
Date: Tue, 26 Mar 2019 19:41:36 GMT
Content-Length: 0
Server: Jetty(9.4.15.v20190215)
EDIT
I was able to create the header fields using a Resource Configuration but I am still unable to create them with the CrossOriginFilter.
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
Server jettyServer = new Server(9998);
jettyServer.setHandler(context);
ResourceConfig webapiResourceConfig = new ResourceConfig();
webapiResourceConfig.register(CorsFilter.class);
ServletHolder jerseyServlet = new ServletHolder(new ServletContainer(webapiResourceConfig));
context.addServlet(jerseyServlet, "/*");
//context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
jerseyServlet.setInitParameter( "jersey.config.server.provider.classnames",MyServerConfig.class.getCanonicalName());
jetty jersey-2.0 embedded-jetty
jetty jersey-2.0 embedded-jetty
edited Mar 26 at 20:46
user3579222
asked Mar 26 at 16:23
user3579222user3579222
1821 silver badge10 bronze badges
1821 silver badge10 bronze badges
1
You shouldn't see the headers in Postman, as Postman doesn't require CORS support. And in Chrome, you should only see them if you are actually making a cross origin request. If the filter is implemented correctly, it should only spit out CORS response headers if there is an Origin request header. And that should only happen on cross origin requests made from a browser.
– Paul Samsotha
Mar 27 at 4:56
You are right: if i set the origin in the request head then I get the required response
– user3579222
Mar 27 at 6:22
Please, post your comment as an answer - I will accept it!
– user3579222
Mar 27 at 14:27
add a comment |
1
You shouldn't see the headers in Postman, as Postman doesn't require CORS support. And in Chrome, you should only see them if you are actually making a cross origin request. If the filter is implemented correctly, it should only spit out CORS response headers if there is an Origin request header. And that should only happen on cross origin requests made from a browser.
– Paul Samsotha
Mar 27 at 4:56
You are right: if i set the origin in the request head then I get the required response
– user3579222
Mar 27 at 6:22
Please, post your comment as an answer - I will accept it!
– user3579222
Mar 27 at 14:27
1
1
You shouldn't see the headers in Postman, as Postman doesn't require CORS support. And in Chrome, you should only see them if you are actually making a cross origin request. If the filter is implemented correctly, it should only spit out CORS response headers if there is an Origin request header. And that should only happen on cross origin requests made from a browser.
– Paul Samsotha
Mar 27 at 4:56
You shouldn't see the headers in Postman, as Postman doesn't require CORS support. And in Chrome, you should only see them if you are actually making a cross origin request. If the filter is implemented correctly, it should only spit out CORS response headers if there is an Origin request header. And that should only happen on cross origin requests made from a browser.
– Paul Samsotha
Mar 27 at 4:56
You are right: if i set the origin in the request head then I get the required response
– user3579222
Mar 27 at 6:22
You are right: if i set the origin in the request head then I get the required response
– user3579222
Mar 27 at 6:22
Please, post your comment as an answer - I will accept it!
– user3579222
Mar 27 at 14:27
Please, post your comment as an answer - I will accept it!
– user3579222
Mar 27 at 14:27
add a comment |
1 Answer
1
active
oldest
votes
You shouldn't see the headers in Postman, as Postman doesn't require CORS support. And in Chrome (or any browser), you should only see them if you are actually making a cross origin request. If the filter is implemented correctly, it should only spit out CORS response headers if there is an Origin request header. And that should only happen on cross origin requests made from a browser.
The reason your Jersey filter worked is probably because it is not implemented correctly, according to the CORS protocol; it is probably just a lazy version where headers are added for all requests. In this answer, I originally also implemented the same "lazy" CORS support, but if you look at the UPDATE, I explain how it should be implemented. If you want to learn more about CORS, that UPDATE is a good read.
1
Jetty'sCrossOriginFilter
will generate the CORS headers if there is anOrigin
header (and it isn't anUpgrade
request).
– Joakim Erdfelt
Mar 27 at 20:51
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55361898%2fcrossoriginfilter-with-jersey-and-embedded-jetty%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You shouldn't see the headers in Postman, as Postman doesn't require CORS support. And in Chrome (or any browser), you should only see them if you are actually making a cross origin request. If the filter is implemented correctly, it should only spit out CORS response headers if there is an Origin request header. And that should only happen on cross origin requests made from a browser.
The reason your Jersey filter worked is probably because it is not implemented correctly, according to the CORS protocol; it is probably just a lazy version where headers are added for all requests. In this answer, I originally also implemented the same "lazy" CORS support, but if you look at the UPDATE, I explain how it should be implemented. If you want to learn more about CORS, that UPDATE is a good read.
1
Jetty'sCrossOriginFilter
will generate the CORS headers if there is anOrigin
header (and it isn't anUpgrade
request).
– Joakim Erdfelt
Mar 27 at 20:51
add a comment |
You shouldn't see the headers in Postman, as Postman doesn't require CORS support. And in Chrome (or any browser), you should only see them if you are actually making a cross origin request. If the filter is implemented correctly, it should only spit out CORS response headers if there is an Origin request header. And that should only happen on cross origin requests made from a browser.
The reason your Jersey filter worked is probably because it is not implemented correctly, according to the CORS protocol; it is probably just a lazy version where headers are added for all requests. In this answer, I originally also implemented the same "lazy" CORS support, but if you look at the UPDATE, I explain how it should be implemented. If you want to learn more about CORS, that UPDATE is a good read.
1
Jetty'sCrossOriginFilter
will generate the CORS headers if there is anOrigin
header (and it isn't anUpgrade
request).
– Joakim Erdfelt
Mar 27 at 20:51
add a comment |
You shouldn't see the headers in Postman, as Postman doesn't require CORS support. And in Chrome (or any browser), you should only see them if you are actually making a cross origin request. If the filter is implemented correctly, it should only spit out CORS response headers if there is an Origin request header. And that should only happen on cross origin requests made from a browser.
The reason your Jersey filter worked is probably because it is not implemented correctly, according to the CORS protocol; it is probably just a lazy version where headers are added for all requests. In this answer, I originally also implemented the same "lazy" CORS support, but if you look at the UPDATE, I explain how it should be implemented. If you want to learn more about CORS, that UPDATE is a good read.
You shouldn't see the headers in Postman, as Postman doesn't require CORS support. And in Chrome (or any browser), you should only see them if you are actually making a cross origin request. If the filter is implemented correctly, it should only spit out CORS response headers if there is an Origin request header. And that should only happen on cross origin requests made from a browser.
The reason your Jersey filter worked is probably because it is not implemented correctly, according to the CORS protocol; it is probably just a lazy version where headers are added for all requests. In this answer, I originally also implemented the same "lazy" CORS support, but if you look at the UPDATE, I explain how it should be implemented. If you want to learn more about CORS, that UPDATE is a good read.
edited Mar 27 at 18:36
answered Mar 27 at 18:26
Paul SamsothaPaul Samsotha
159k21 gold badges319 silver badges520 bronze badges
159k21 gold badges319 silver badges520 bronze badges
1
Jetty'sCrossOriginFilter
will generate the CORS headers if there is anOrigin
header (and it isn't anUpgrade
request).
– Joakim Erdfelt
Mar 27 at 20:51
add a comment |
1
Jetty'sCrossOriginFilter
will generate the CORS headers if there is anOrigin
header (and it isn't anUpgrade
request).
– Joakim Erdfelt
Mar 27 at 20:51
1
1
Jetty's
CrossOriginFilter
will generate the CORS headers if there is an Origin
header (and it isn't an Upgrade
request).– Joakim Erdfelt
Mar 27 at 20:51
Jetty's
CrossOriginFilter
will generate the CORS headers if there is an Origin
header (and it isn't an Upgrade
request).– Joakim Erdfelt
Mar 27 at 20:51
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55361898%2fcrossoriginfilter-with-jersey-and-embedded-jetty%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You shouldn't see the headers in Postman, as Postman doesn't require CORS support. And in Chrome, you should only see them if you are actually making a cross origin request. If the filter is implemented correctly, it should only spit out CORS response headers if there is an Origin request header. And that should only happen on cross origin requests made from a browser.
– Paul Samsotha
Mar 27 at 4:56
You are right: if i set the origin in the request head then I get the required response
– user3579222
Mar 27 at 6:22
Please, post your comment as an answer - I will accept it!
– user3579222
Mar 27 at 14:27