Spring Boot and CORS -
i facing issue cors in spring boot. have configured cors this
@configuration @enablewebmvc public class webconfig extends webmvcconfigureradapter { @override public void addcorsmappings(corsregistry registry) { registry.addmapping("/**"); } }
which suppose enables header , other stuff.
it works excellently request
$.get("someurl, function(data, status){ console.log(data[0].latitude); });
but whenever make post request this
$.ajax({ url: 'someurl', type: 'post', datatype: 'json', crossdomain: true, contenttype: "application/json; charset=utf-8", success: function (data) { console.log(data); }, data: object });
i following
options xhr "someurl" [http/1.1 403 forbidden 4ms] cross-origin request blocked: same origin policy disallows reading remote resource @ "someurl". (reason: cors header 'access-control-allow-origin' missing).
how can solve issue?
simple way of configuring cors filter spring-boot app make @component class implements filter this:
@component public class simplecorsfilter implements filter { @override public void dofilter(servletrequest req, servletresponse res, filterchain chain) throws ioexception, servletexception { httpservletresponse response = (httpservletresponse) res; response.setheader("access-control-allow-origin", "*"); response.setheader("access-control-allow-methods", "post, get, put, options, delete, patch"); response.setheader("access-control-max-age", "3600"); response.setheader("access-control-allow-headers", "origin, x-requested-with, content-type, accept"); response.setheader("access-control-expose-headers", "location"); chain.dofilter(req, res); } @override public void init(filterconfig filterconfig) {} @override public void destroy() {} }
it works great spring-boot 1.3.0
Comments
Post a Comment