Reputation: 3763
I have searched around for whole day on internet. This is what I have included on web.xml
<filter>
<filter-name>cors</filter-name>
<filter-class>org.sampe.service.context.filter.CorsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>cors</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Here is the Cors Filter (CorsFilter.java)
@Component
public class CorsFilter 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");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization");
chain.doFilter(req, res);
}
@Override
public void init(FilterConfig filterConfig) {}
@Override
public void destroy() {}
}
It works fine on postman. But I am having problem making ajax calls or using angular 2. I have looked around these questions too. solved stackoverflow answer Whats wrong with mine.
Upvotes: 0
Views: 393
Reputation: 153
You have to annotate your Cors filter with the following annotation :
@Order(Ordered.HIGHEST_PRECEDENCE)
@Order
defines the sort order for an annotated component. The default value is Ordered.LOWEST_PRECEDENCE
, indicating lowest priority (losing to any other specified order value).
Upvotes: 1
Reputation: 13805
This has nothing to do with angular. The thing is that your spring API does not allow cross origin requests.
To allow your API to be accessible from cross origin you can setup annotations on the rest controller.
What I am trying to say is you can annotate your rest controller with @CrossOrigin , so that it will allow cross origin access.
Hope this helps. Kudos
Upvotes: 1