I\'ve got an extremely simple example that I can\'t get to work.
I have my domain that models my database, and my Repository.
public interface MyTes
Using Spring Boot 2.2.6
I had to add a filter to allow OPTIONS to work. Without it, I got a 403 Forbidden. The "Origin" request header is what triggered the 403 - I tested in Postman and without sending that header OPTIONS worked without a filter.
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
@Component
public class CORSFilter implements Filter {
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", "OPTIONS"); // "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");
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) {}
public void destroy() {}
}
Along with
@Configuration
public class ConfigCORS implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*") allowedOrigins("http://localhost:3000")
.allowedMethods("POST", "PUT", "GET", "DELETE", "OPTIONS")
.allowedHeaders("Content-Type", "Origin")
.exposedHeaders("X-Total-Count", "Location", "Access-Control-Allow-Origin")
.allowCredentials(false)
.maxAge(6000);
}
}