无法将POST请求发送到Spring Resource服务器

时间:2019-10-20 17:32:44

标签: spring-boot react-redux axios spring-oauth2

我有一个Spring Boot ResourceServer和一个React客户端应用程序。我正在尝试向服务器端点之一(具有POST注解btw。)发送@CrossOrigin请求,但是,我在控制台中收到此错误:

  

从“ http://localhost:8080/api/search”访问XMLHttpRequest   原产地“ http://localhost:3000”已被CORS政策阻止:   对预检请求的响应未通过访问控制检查:   没有HTTP正常状态。

预检请求返回401 Http状态,我不知道为什么。

预检请求的响应标头如下所示:

Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: content-type
Access-Control-Allow-Methods: POST
Access-Control-Allow-Origin: http://localhost:3000
Allow: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Length: 0
Date: Sun, 20 Oct 2019 17:23:54 GMT
Expires: 0
Pragma: no-cache
Vary: Origin, Access-Control-Request-Method, Access-Control-Request-Headers
WWW-Authenticate: Basic realm="Realm"
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

我的飞行前请求标头如下:

Access-Control-Request-Headers: content-type
Access-Control-Request-Method: POST
Origin: http://localhost:3000
Referer: http://localhost:3000/movie-search
Sec-Fetch-Mode: no-cors
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36

我正在使用Axios发送请求(如果很重要)。有人知道这是怎么回事吗?

1 个答案:

答案 0 :(得分:1)

哇...我的HTTPSecurity配置看起来像这样:

http
    .authorizeRequests()
    .antMatchers(HTTPMethod.POST, "/api/search").permitAll()
    .anyRequest().authenticated()
    .and().httpBasic()

代替此:

http
    .authorizeRequests()
    .antMatchers("/api/search").permitAll()
    .anyRequest().authenticated()
    .and().httpBasic()