i have implemented filter for cors in spring boot.the code is as follow:-
@springbootapplication
@component
public class application implements filter {
public static void main(string[] args) {
springapplication.run(application.class, args);
}
@override
public void init(filterconfig filterconfig) throws servletexception {
}
@override
public void dofilter(servletrequest servletrequest, servletresponse servletresponse, filterchain filterchain) throws ioexception, servletexception {
httpservletrequest request = (httpservletrequest) servletrequest;
httpservletresponse response = (httpservletresponse) servletresponse;
response.setheader("access-control-allow-origin", "*");
response.setheader("access-control-allow-credentials", "true");
response.setheader("access-control-allow-methods", "post, get, options, delete");
response.setheader("access-control-max-age", "3600");
response.setheader("access-control-allow-headers", "content-type, accept, x-requested-with, remember-me");
filterchain.dofilter(servletrequest, servletresponse);
}
@override
public void destroy() {
}
}
to get the access_token from oauth2 i have created following object from angularjs:-
{"url":"http://localhost:8080/oauth/token?grant_type=client_credentials&client_id=clientid&client_secret=clientsecret","headers":{"authorization":"basic token_value"},"withcredentials":true,"method":"post"}
i am getting following error when i hit the server:-
options url... 401() response for preflight has invalid http status code 401
i have looked for the solutions for similar problems in but none of them fixed my problem.could someone please help on this?
please read more about preflight requests.
they simply suggest the browser if the server supports a cross-origin request. the response to such
options
requests should good (i.e. < 400).i think the statement
filterchain.dofilter(servletrequest, servletresponse);
is passing the request further, instead of returning a response.you can read more about enabling cors using spring in java here enable cors for options request using spring framework