I'm actually working on a SrpingBoot back end.
To resolve the CORS problem I use the simple filter that we can find everywhere.
@Component
public class CORSResponseFilter implements Filter {
@Override
public void init(FilterConfig arg0) throws ServletException {}
@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse response=(HttpServletResponse) resp;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
chain.doFilter(req, resp);
}
@Override
public void destroy() {}
}
This filtre is add to the configuration here :
public class ApplicationConfiguration extends ResourceConfig {
public ApplicationConfiguration() {
super();
// Create a recursive package scanner
PackageNamesScanner resourceFinder = new PackageNamesScanner(
new String[] { "X.service" }, true);
// Register the scanner with this Application
registerFinder(resourceFinder);
register(JacksonFeature.class);
// Mise en place du filtre
register(CORSResponseFilter.class);
}
}
But I still got this error :
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
What did I missed ?
Aucun commentaire:
Enregistrer un commentaire