REST API 由于 CSRF 检查,同时发布两个具有不同属性的请求失败并显示 403 状态代码
REST API Posting two request simultaneously with different properties fails with 403 status code due to CSRF check
正在使用 REST API 发送两个 post 请求:http://localhost:8111/app/rest/buildQueue but my second request fails with 403 Forbidden: Responding with 403 status code due to failed CSRF check: no "Origin" header is present and no authentication provided with the request, consider adding "Origin: http://localhost:8111" header。我想知道为什么会发生这种情况,因为如果我 运行 在 UI 中构建并更改参数 ex. build1 有 %version=2% 而 build2 有 %version=3% 它将 运行 彼此并行 运行ning on不同的可用代理。
这是我发送的 json 请求
休息 API:http://localhost:8111/app/rest/buildQueue
JSON BODY:
{ "branchName":"master", "buildType":{ "id":"DockerBuild", "projectId":"Test" }, "properties":{ "property":[ { "name":"DOCKER_IMAGE_NAME", "value":"test-3" }, { "name":"SNAPSHOT_DEPENDENCY_VERSION", "value":"0.6" } ] } }
我是否缺少能够运行彼此并行构建的参数?
403 禁止:由于 CSRF 检查失败,响应 403 状态码:不存在 "Origin" header 且请求未提供身份验证,考虑添加 "Origin: http://localhost:8111" header.
当您在 TeamCity 中遇到有关 CSRF 保护的问题时(例如,您从服务器收到 "Responding with 403 status code due to failed CSRF check" 响应),您可以按照以下步骤操作:
如果您使用反向代理,请确保正确配置 Host/Origin headers,如上所述。同时,您可能希望将服务器的 public URL 添加到 CORS-enabled origins.
您可以通过设置 teamcity.csrf.origin.check.enabled=logOnly 内部 属性.
来暂时禁用 CSRF 保护
关于失败的 CSRF 尝试的信息被记录到 TeamCity/logs/teamcity-auth.log 文件中。如需更详细的请求诊断,请启用 debug-auth logging preset.
尝试传入请求 header -H 'Origin: http://localhost:8111'
也许这对某些人有用,我在使用 Postman 的单个 POST 中遇到了同样的错误:
403 Forbidden: Responding with 403 status code due to failed CSRF check: no "Origin" header is present and no authentication provided with the request, consider adding "Origin: http://teamcity:20011" header.
所以我按照错误消息的建议,在 Header 中添加了值为“http://teamcity:20011”的 "Origin" 并解决了问题。顺便说一句,在授权中我选择了 "Bearer Token" 并粘贴了之前通过 TeamCity 生成的令牌。这是调用:
http://teamcity:20011/app/rest/buildQueue
我只是在测试如何使用 API 触发构建并且它成功运行。现在接下来的步骤是使用 JavaScript.
实现此调用
使用适当的请求请求 CSRF header:
https:///teamcity/authenticationTest.html?csrf
并在您的 POST 请求的“X-TC-CSRF-TOKEN”header 中设置它
正在使用 REST API 发送两个 post 请求:http://localhost:8111/app/rest/buildQueue but my second request fails with 403 Forbidden: Responding with 403 status code due to failed CSRF check: no "Origin" header is present and no authentication provided with the request, consider adding "Origin: http://localhost:8111" header。我想知道为什么会发生这种情况,因为如果我 运行 在 UI 中构建并更改参数 ex. build1 有 %version=2% 而 build2 有 %version=3% 它将 运行 彼此并行 运行ning on不同的可用代理。
这是我发送的 json 请求
休息 API:http://localhost:8111/app/rest/buildQueue JSON BODY:
{ "branchName":"master", "buildType":{ "id":"DockerBuild", "projectId":"Test" }, "properties":{ "property":[ { "name":"DOCKER_IMAGE_NAME", "value":"test-3" }, { "name":"SNAPSHOT_DEPENDENCY_VERSION", "value":"0.6" } ] } }
我是否缺少能够运行彼此并行构建的参数?
403 禁止:由于 CSRF 检查失败,响应 403 状态码:不存在 "Origin" header 且请求未提供身份验证,考虑添加 "Origin: http://localhost:8111" header.
当您在 TeamCity 中遇到有关 CSRF 保护的问题时(例如,您从服务器收到 "Responding with 403 status code due to failed CSRF check" 响应),您可以按照以下步骤操作:
如果您使用反向代理,请确保正确配置 Host/Origin headers,如上所述。同时,您可能希望将服务器的 public URL 添加到 CORS-enabled origins.
您可以通过设置 teamcity.csrf.origin.check.enabled=logOnly 内部 属性.
来暂时禁用 CSRF 保护关于失败的 CSRF 尝试的信息被记录到 TeamCity/logs/teamcity-auth.log 文件中。如需更详细的请求诊断,请启用 debug-auth logging preset.
尝试传入请求 header -H 'Origin: http://localhost:8111'
也许这对某些人有用,我在使用 Postman 的单个 POST 中遇到了同样的错误:
403 Forbidden: Responding with 403 status code due to failed CSRF check: no "Origin" header is present and no authentication provided with the request, consider adding "Origin: http://teamcity:20011" header.
所以我按照错误消息的建议,在 Header 中添加了值为“http://teamcity:20011”的 "Origin" 并解决了问题。顺便说一句,在授权中我选择了 "Bearer Token" 并粘贴了之前通过 TeamCity 生成的令牌。这是调用:
http://teamcity:20011/app/rest/buildQueue
我只是在测试如何使用 API 触发构建并且它成功运行。现在接下来的步骤是使用 JavaScript.
实现此调用使用适当的请求请求 CSRF header: https:///teamcity/authenticationTest.html?csrf
并在您的 POST 请求的“X-TC-CSRF-TOKEN”header 中设置它