I'm able to build my own oauth2 server following this example
I then created my own client side app with frontend being react. I specifically used facebook's create-react-app to get a quick start point.
I also found that in order to run the frontend in dev mode while connecting to backend api, I need to have a proxy added in the package.json file.
Now I have everything at hand:
- The oauth2 server running on "localhost:8080"
- The client app api running on "localhost:9090"
- The frontend running in dev mode on "localhost:3000" with proxy set to connecting api port "9090"
When I try to connect to "localhost:3000", I always get the following error:
Failed to load http://localhost:9090/login: Redirect from 'http://localhost:9090/login' to 'http://localhost:8080/oauth/authorize?client_id=acme&redirect_uri=http://localhost:9090/login&response_type=code&scope=read%20write&state=qbV8P2' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
If I run "yarn build" and let spring boot serve the frontend static files, then if I access "localhost:9090", I will be properly redirected to the login page from the oauth2 server.
I've tried to allow "localhost:3000" by editing cors mapping from spring boot but this problem is still here.
As a side note, If I get authenticated by accessing "localhost:9090", and acquired the session cookie, then I can access protected resource by using "localhost:3000". It's just if I am not authenticated in the first place, then instead of being redirected to the login page, I always get the CORS error.
Any help would be appreciated.