gpt4 book ai didi

playframework-2.6 - Play 框架上的 Web 服务不接受 POST 消息

转载 作者:行者123 更新时间:2023-12-02 17:48:43 25 4
gpt4 key购买 nike

我正在从 Angular 服务向播放应用程序发送以下消息

  public createUser(user:User):any{
console.log('contacting server at '+this.API_URL +this.ADD_USER_URL +" with user data ",user);
/*http.post returns an observable. The caller's code should subscribe to this observable */
return this.http.post(this.API_URL +this.ADD_USER_URL,user)
.map(response => { //handler if post's Observable is successful. map creates new User and returns Observable<User>.
console.log('response from backend service',response);
//return new User(response); //the constructor of User allows passing an object.
/*when the observable of http.post returns (produces) data (the response), the map function prints a message and returns that response*/
return response;
})
.catch(this.handleError); //error handler if Observable fails
}

我在 Play 控制台上收到以下警告和禁止响应。[警告] p.filters.CSRF - [CSRF] 检查失败,因为请求/ws/users/add 的 application/json

WebToBackendInterfaceService::handleError 
Object { headers: {…}, status: 403, statusText: "Forbidden", url: "http://localhost:9000/ws/users/add", ok: false, name: "HttpErrorResponse", message: "Http failure response for http://localhost:9000/ws/users/add: 403 Forbidden", error: "<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <title>Unauthorized</title>\n <link rel=\"shortcut icon\" href=\"data:image/png;base64,...">\n <style>\n html, body, pre {\n margin: 0;\n padding: 0;\n font-family: Monaco, 'Lucida Console', monospace;\n background: #ECECEC;\n }\n h1 {\n margin: 0;\n background: #333;\n padding: 20px 45px;\n color: #fff;\n text-shadow: 1px 1px 1px rgba(0,0,0,.3);\n border-bottom: 1px solid #111;\n font-size: 28px;\n }\n p#detail {\n margin: 0;\n padding: 15px 45px;\n background: #888;\n border-top: 4px solid #666;\n color: #111;\n text-shadow: 1px 1px 1px rgba(255,255,255,.3);\n font-size: 14px;\n border-bottom: 1px solid #333;\n }\n </style>\n </head>\n <body>\n <h1>Unauthorized</h1>\n\n <p id=\"detail\">\n You must be authenticated to access this page.\n </p>\n\n </body>\n</html>\n" }

Play 的 CSRF 似乎不喜欢此消息。我该如何解决这个问题?我在 Play 中读到,错误发生在

  1. 请求不是 GET、HEAD 或 Option - 在我的例子中为 true,我正在发送 POST 消息
  2. 请求具有 cookie 或授权 header 。我没有明确地发送它们。 Angular 是否默认发送它?
  3. CORS 过滤器未配置为信任请求的来源。 - 我正在向 localhost:9000 发送请求。 Play 不应该信任本地主机吗?

如何让 Play 接受我的消息?

最佳答案

这是一个解决方法。正确的解释在Angular not sending CSRF token in header 。基本上,我的客户端应该发送一个 CSRF header ,其中包含最初由播放框架创建的 token

我不知道这是否是正确的方法,但我必须在 Play 中禁用 CSRF。可以通过以下两种方式实现

routes文件中,在规则上方添加nocsrf

+nocsrf
POST /ws/users/add controllers.UserController.addUser

或者在application.conf中完全禁用csrf

play.filters {


# Disabled filters remove elements from the enabled list.
disabled += play.filters.csrf.CSRFFilter
}

我完全禁用了csrf

关于playframework-2.6 - Play 框架上的 Web 服务不接受 POST 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48904971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com