A filter to release the CORS would be something like:
@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-Allow-Headers", "Content-Type, Authorization");
chain.doFilter(req, response);
}
Since he’s using Apache Corsfilter, I think there’s something missing.
It would be something like:
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
</init-param>
<init-param>
<param-name>cors.exposed.headers</param-name>
<param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
</init-param>
<init-param>
<param-name>cors.support.credentials</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>cors.preflight.maxage</param-name>
<param-value>10</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Source: Using CORS Headers with JAVA
So Rafael, a while ago I had a similar problem with an API, basically CORS is an authorization that defines who is authorized to consume data from the API, take a look at this [https://stackoverflow.com/questions/16296145/set-cors-header-in-tomcat/18850438#18850438]as the headers of your request are defined ?
– Caio
From your comment on the Luks Sys response you don’t have access to the server. The code that he gave you is a Tomcat server configuration. You need to change your app to include CORS headers in the calls to the api.
– Grasshopper
Another thing that strikes you is that problems with CORS only occur when you have a page loaded from a host wanting to make Ajax calls to another host. On mobile this should not happen because your calls do not run in a browser :).
– Grasshopper
@Grasshopper did not understand very well, but yes, when I run in the Browser the API returns correctly, when I build and will run in the app I have this problem, the app is being done using Cordova + F7. is the first time I have this problem, all other projects (with another back), works perfectly.
– Rafael Augusto
@Rafaelaugusto 401 is not error of
CORS
– Denis Rudnei de Souza
@Rafaelaugusto puts the code where you call the API
– Denis Rudnei de Souza
Fortunately I ended up remaking the Back-End in Nodejs, all the information that went over I passed to the other developer and unfortunately he could not solve, but I thank everyone who tried to help me.
– Rafael Augusto