对于需要 SSL/TLS 的请求,发送的正确 HTTP 响应是什么

发布于 2024-08-27 10:16:33 字数 247 浏览 7 评论 0原文

我正在设计一个 RESTful API,其中一些调用通过 HTTP 公开,而一些调用需要 API 密钥并通过 HTTPS 加密。我正在考虑如果将 HTTP 请求发送到私有资源之一,应该发送什么响应代码。到目前为止,唯一让我惊讶的是412 - Precondition Failed,但标准表明前提条件是由请求者而不是服务器强加的。

对于这种情况是否有适当的响应代码,或者我只需要屈服并执行400

I'm designing an RESTful API where some calls are public over HTTP, and some require an API key and encryption over HTTPS. I'm deliberating on what response code should be sent if an HTTP request is sent to one of the private resources. So far the only one that jumps out at me is 412 - Precondition Failed, but the standard indicates that the precondition is imposed by the requester not the server.

Is there an appropriate response code for this condition or do I just need to give in and do 400?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

醉态萌生 2024-09-03 10:16:34

返回带有原因短语“需要 HTTPS”的 403 似乎是一个实用的选择,也是我所使用的。

请参阅 https://en.wikipedia.org/wiki/HTTP_403

重定向 REST Api 不是好主意,尤其是当您可能不知道如何或什么消耗您的服务时。

Returning a 403 with reason phrase "HTTPS Required" seems like a practical option and what I use.

see https://en.wikipedia.org/wiki/HTTP_403

Redirecting a REST Api is not a good idea especially as you may have no idea as to how or what is consuming your service.

皇甫轩 2024-09-03 10:16:34

返回的相应错误代码类似于 403.4 - 需要 SSL

尽管 HTTP 1.1 的 RFC 中没有明确记录,此行为确实符合此处概述的要求:

服务器理解该请求,但拒绝满足它。授权不会有帮助,并且不应重复请求。如果请求方法不是 HEAD 并且服务器希望公开为什么请求没有被满足,它应该在实体中描述拒绝的原因。如果服务器不希望将此信息提供给客户端,则可以使用状态代码 404(未找到)。

添加您自己的子代码(如 SSL 示例)在某些情况下可能会有所帮助,但由于此子代码对第三方没有意义,因此我建议不要这样做。

因此,您的最终错误消息将类似于“403 - 私有资源”。请注意,即使在缺少 API 密钥的情况下,也不应使用“401 - 未经授权”,除非您的 API 密钥实际上可以在 WWW-Authenticate 标头字段中传输。

The appropriate error code to return would be similar to 403.4 - SSL required.

Although not explicitly documented in the RFC for HTTP 1.1, this behavior does match the requirements outlined there:

The server understood the request, but is refusing to fulfill it. Authorization will not help and the request SHOULD NOT be repeated. If the request method was not HEAD and the server wishes to make public why the request has not been fulfilled, it SHOULD describe the reason for the refusal in the entity. If the server does not wish to make this information available to the client, the status code 404 (Not Found) can be used instead.

Adding your own subcode (as with the SSL example) might be helpful in some cases, but since this subcode would not be meaningful to third parties, I would recommend against it.

So, your final error message would be something like "403 - Private Resource". Note that, even in the case of a missing API key, "401 - Unauthorized" should not be used, unless your API key can actually be transmitted in a WWW-Authenticate header field.

余生再见 2024-09-03 10:16:34

只需发送重定向到相应的 https: URI。

更新

这是一个错误的答案 - 请参阅下面的评论

Just send a redirect to the corresponding https: URI.

UPDATE

The is a wrong answer - see comments below

老子叫无熙 2024-09-03 10:16:33

我不能说这是否被 HTTP 客户端广泛接受,但严格来说 RFC,服务器应该响应:

HTTP/1.1 426 Upgrade Required
Upgrade: TLS/1.0, HTTP/1.1
Connection: Upgrade

来源:
https://www.rfc-editor.org/rfc/rfc2817#section- 4.2

I cannot say if this is broadly accepted by HTTP clients, but speaking strictly RFC, the server should respond with:

HTTP/1.1 426 Upgrade Required
Upgrade: TLS/1.0, HTTP/1.1
Connection: Upgrade

Source:
https://www.rfc-editor.org/rfc/rfc2817#section-4.2

野鹿林 2024-09-03 10:16:33

强制 HTTP 客户端使用 HTTPS 的最安全方法是 HTTP 严格传输安全

以前常见的建议是断开连接,但这种做法已删除以支持 HSTS(OWASP 网站)。

The most secure way to force HTTP client to use HTTPS is HTTP Strict Transport Security.

Previously a common suggestion was to drop the connection, but this practice has been removed in favor of HSTS (OWASP website).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文