【问题标题】:PutMapping 403 forbidden禁止放置映射 403
【发布时间】:2019-07-05 15:15:14
【问题描述】:

我正在尝试使用“put”动词使用 http 协议进行一次更新,但我使用 Request Method: OPTIONS from chrome 得到 403 禁止。

如果我尝试将动词从 PUT 更改为 POST,它会起作用。

这是我的服务器代码(spring)

    @PutMapping("/path")
    public ResponseEntity putMethod(@RequestBody Dto dto) throws URISyntaxException {
        log.debug("put is called");

        return ResponseEntity.ok().build();
    }

这是我的角度代码

update(dto: dto) {
    return this.http
      .put<Dto>(this.resourceUrl + '/path', dto, { observe: 'response' })
      .pipe();
  }

如果我将 put 更改为 angular 并且 spring 日志写入正确,但我需要更新并且我想使用正确的动词

Chrome 日志

 Request URL: http://localhost:9080/path
Request Method: OPTIONS
Status Code: 403 Forbidden
Remote Address: 127.0.0.1:9080
Referrer Policy: no-referrer-when-downgrade
Allow: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
Connection: Close
Content-Language: it-IT
Content-Length: 20
Date: Fri, 05 Jul 2019 15:19:14 GMT
Vary: Origin, Access-Control-Request-Method, Access-Control-Request-Headers
X-Powered-By: Servlet/3.1
Provisional headers are shown
Access-Control-Request-Headers: content-type
Access-Control-Request-Method: PUT
Origin: http://localhost:4200
Referer: http://localhost:4200/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36

这里是我的java post映射

@PostMapping("/path")
public ResponseEntity postMethod(@RequestBody Dto dto) throws URISyntaxException {
    log.debug("post is called");

    return ResponseEntity.ok().build();
}

和角度邮政编码

post(dto: dto) {
    return this.http
      .post<Dto>(this.resourceUrl + '/path', dto, { observe: 'response' })
      .pipe();
  }

此处为发布请求(工作)的 chrome 日志

Request URL: http://localhost:9080/path
Request Method: POST
Status Code: 200 OK
Remote Address: 127.0.0.1:9080
Referrer Policy: no-referrer-when-downgrade
Access-Control-Allow-Origin: *
Content-Language: it-IT
Content-Length: 16
Content-Type: application/json;charset=UTF-8
Date: Fri, 05 Jul 2019 15:48:01 GMT
Vary: Origin, Access-Control-Request-Method, Access-Control-Request-Headers
X-Powered-By: Servlet/3.1
Provisional headers are shown
Accept: application/json, text/plain, */*
Content-Type: application/json
Origin: http://localhost:4200
Referer: http://localhost:4200/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36

【问题讨论】:

  • 您是否在任何阻止 put 的代理背后?
  • @PasupathiRajamanickam 不,我没有设置代理
  • 发布此 ajax 调用的 chrome 错误日志。
  • 更新日志问题。

标签: angular spring controller put


【解决方案1】:

您的后端服务器中是否允许使用动词 PUT?我在前端没有看到任何问题。

【讨论】:

  • 这应该在 cmets 中,而不是作为答案,你可能会得到 -1 的答案。
【解决方案2】:

我看到您正在从 4200 调用在 9090 上运行的 API,您可能会收到显示 Access control allow origin 错误的 chrome 日志。

如果这是您需要的,您可能必须允许您的来源。里面会有安全隐患。因此,您必须谨慎行事。

添加CrossOrigin 将允许此流程。

@CrossOrigin(origins = "http://localhost:4200")
@PutMapping("/path")

https://spring.io/guides/gs/rest-service-cors/

【讨论】:

  • 不确定,您可以尝试在端口4200 发帖,这没有问题,但您不能跨端口或跨域。
  • 我已经用 chrome 日志从 post request 中更新了问题,没有 cors
  • 发布您的POST Requestmapping
  • OK Java 和 Angular 代码现在存在问题
  • 我不知道没有@CrossOrigin 的帖子是如何工作的。它应该会抛出同样的错误。
【解决方案3】:

最后我在我的开发应用程序中使用了这个设置:

cors.allowed-origins=*
cors.allowed-methods=*
cors.allowed-headers=*
cors.exposed-headers=Authorization,Link,X-Total-Count
cors.allow-credentials=true

因为在开发环境中,我使用位于 localhost:4200 的节点服务器,他们对 localhost:9080 进行 http 请求,而在生产环境中,angular 和 java 在同一服务器中(设置了 maven-war-plugin 和 webResources)在同一个端口并且不需要此设置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 2015-11-18
    • 2019-08-24
    • 2013-02-09
    • 2012-07-29
    • 2012-01-08
    • 2017-01-20
    相关资源
    最近更新 更多