繁体   English   中英

禁止PutMapping 403

[英]PutMapping 403 forbidden

我正在尝试使用“ put”动词使用http协议进行一次更新,但是我从chrome的请求方法:OPTIONS中获得了403禁止。

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

这是我的服务器代码(春季)

    @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();
  }

如果我更改角度和弹簧日志中的发布位置,则可以正确书写,但是我需要进行更新,并且我想使用正确的动词

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贴图

@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();
  }

铬日志在这里发布请求(工作)

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? 我看不到前端方面的任何问题。

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

如果这是您需要的,则可能必须允许您的出身。 里面会有安全隐患。 因此,您在执行此操作时必须谨慎。

添加CrossOrigin将允许此流程。

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

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

最后,我在我的dev application.properties中使用了以下设置:

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)在同一端口上,不需要此设置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM