前端部署在http://localhost:8080,而SpringBoot创建的后端部署在http://localhost:9000。即使都是在本地,端口号不一样也是跨域。所以当前端页面点击登陆按钮要访问后端时会产生Invalid CORS request,且Request Method为OPTIONAL。
解决方案:
编写一个CorsConfig:
1 | import org.springframework.boot.web.servlet.FilterRegistrationBean; |
编写以上配置类后,能成功访问后端并且Request Method变为了POST。