I had wasted lot's of time fixing this issue, but none of the existing solutions work in my case. Let me explain you the server set up I have. I have 2 docker containers, one for angular app (nginx - url - http://localhost:8080) and spring boot application (tomcat - url - http://localhost:8081). This app is using Oauth2 jdbcToken authentication for API request.
This app is a simple user registration app.
I could register a new user since the register url is not secured and not passing any Authorization header. But once when user logs in the CORS issue kick in. below I have listed the errors.
Access to XMLHttpRequest at 'http://localhost:8081/v1/api/group/find/shib' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
Let me tell you what I had done to resolve this. In angular HTTP request I have added the following headers.
'Authorization' : 'Bearer '+this.token(),
'Access-Control-Allow-Methods' : '*',
'Access-Control-Allow-Origin' : "*",
'Access-Control-Allow-Headers' : 'Content-Type, Accept, X-Requested-With, remember-me, Authorization',
"Access-Control-Expose-Headers" : "Content-Type, Accept, X-Requested-With, remember-me, Authorization"
In spring boot I added @CrossOrigin / @CrossOrigin("http:localhost:8080") on Rest controllers added with a CORSFilter
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
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, Authorization");
chain.doFilter(req, res);
}
After all the trial and error, I am still getting the same error