如何在 GET 请求中发送密码以访问 REST 资源
How to send a password in a GET request to access a REST resource
我有一个 REST 服务,已经使用授权 header 通过基本身份验证 保护了它。这通常用于访问服务,并且是任何请求所必需的。即 "User1"、"password1"、
我有一个 "file" 资源,它可以有一个与之关联的附加密码(即受密码保护的 Word 文档、PDF 等),"docpassword"。像这样发送敏感信息的最佳方式是什么?我对如何为 GET 请求发送密码特别感兴趣,但我想要一个通用的解决方案,它也适用于 POST 请求。
也许是一个习惯 header?
HTTP 已有身份验证方法,例如,请参阅此 RFC:https://www.rfc-editor.org/rfc/rfc2617
问题澄清后编辑:没有什么可以阻止服务器进行额外的挑战,即使是基于单一资源。我必须承认我还没有实现这样的事情,但是每个授权都可以有自己的 realm。您可以指定不同的领域,如果您真的愿意,甚至可以指定到文档级别。然后服务器可能会在每个领域进行多次挑战(首先登录,然后记录)。请记住,您可以在客户端缓存成功的身份验证(对于一个领域,就像浏览器一样),或者使用缓存的令牌分发 cookie。
这样做的好处是可以避免自定义 header,并且完全 HTTP/REST 符合。可能存在一些性能劣势,但可以通过一些有针对性的缓存来缓解。
当然,如果你愿意,你可以使用自定义 header,但通常 REST 意味着客户端除了 mime-types
和 HTTP
之外没有任何先验知识。协议。习俗 header 意味着 out-of-band 先验知识。
HTTP协议定义标准Authorization
header for sending authentication data (credentials) to the server. This header is defined in the RFC 7235 (which makes the old RFC 2616 obsolete and updates the RFC 2617):
The Authorization
header field allows a user agent to authenticate
itself with an origin server -- usually, but not necessarily, after
receiving a 401
(Unauthorized) response. Its value consists of
credentials containing the authentication information of the user
agent for the realm of the resource being requested.
Authorization = credentials
[...]
请注意,此 HTTP header 的名称很不幸,因为它携带 authentication 数据而不是 authorization。无论如何,这是用于在 HTTP 协议中发送凭据的标准 headers。
一旦您已经在使用 HTTP Basic Authentication Scheme to authenticate the users in your application, I believe you are already using the standard Authorization
header。
我通常不推荐自定义headers,尤其是当可以使用标准headers时,但你的场景似乎并不常见:你需要对同一个进行两次身份验证请求。
因此,对于 GET
请求,使用文档密码的自定义 header(例如 X-Auth-Document
或 Document-Authentication
可能没问题。如果您决定不使用 GET
并决定使用 POST
访问该资源,您可以考虑在请求负载中发送文档的密码。
无论如何,不要忘记使用 HTTPS: is highly advisable once you are sending sensitive data, such as credentials, over the wire. And HTTPS will protect you against the man-in-the-middle attack。
我有一个 REST 服务,已经使用授权 header 通过基本身份验证 保护了它。这通常用于访问服务,并且是任何请求所必需的。即 "User1"、"password1"、
我有一个 "file" 资源,它可以有一个与之关联的附加密码(即受密码保护的 Word 文档、PDF 等),"docpassword"。像这样发送敏感信息的最佳方式是什么?我对如何为 GET 请求发送密码特别感兴趣,但我想要一个通用的解决方案,它也适用于 POST 请求。
也许是一个习惯 header?
HTTP 已有身份验证方法,例如,请参阅此 RFC:https://www.rfc-editor.org/rfc/rfc2617
问题澄清后编辑:没有什么可以阻止服务器进行额外的挑战,即使是基于单一资源。我必须承认我还没有实现这样的事情,但是每个授权都可以有自己的 realm。您可以指定不同的领域,如果您真的愿意,甚至可以指定到文档级别。然后服务器可能会在每个领域进行多次挑战(首先登录,然后记录)。请记住,您可以在客户端缓存成功的身份验证(对于一个领域,就像浏览器一样),或者使用缓存的令牌分发 cookie。
这样做的好处是可以避免自定义 header,并且完全 HTTP/REST 符合。可能存在一些性能劣势,但可以通过一些有针对性的缓存来缓解。
当然,如果你愿意,你可以使用自定义 header,但通常 REST 意味着客户端除了 mime-types
和 HTTP
之外没有任何先验知识。协议。习俗 header 意味着 out-of-band 先验知识。
HTTP协议定义标准Authorization
header for sending authentication data (credentials) to the server. This header is defined in the RFC 7235 (which makes the old RFC 2616 obsolete and updates the RFC 2617):
The
Authorization
header field allows a user agent to authenticate itself with an origin server -- usually, but not necessarily, after receiving a401
(Unauthorized) response. Its value consists of credentials containing the authentication information of the user agent for the realm of the resource being requested.Authorization = credentials
[...]
请注意,此 HTTP header 的名称很不幸,因为它携带 authentication 数据而不是 authorization。无论如何,这是用于在 HTTP 协议中发送凭据的标准 headers。
一旦您已经在使用 HTTP Basic Authentication Scheme to authenticate the users in your application, I believe you are already using the standard Authorization
header。
我通常不推荐自定义headers,尤其是当可以使用标准headers时,但你的场景似乎并不常见:你需要对同一个进行两次身份验证请求。
因此,对于 GET
请求,使用文档密码的自定义 header(例如 X-Auth-Document
或 Document-Authentication
可能没问题。如果您决定不使用 GET
并决定使用 POST
访问该资源,您可以考虑在请求负载中发送文档的密码。
无论如何,不要忘记使用 HTTPS: is highly advisable once you are sending sensitive data, such as credentials, over the wire. And HTTPS will protect you against the man-in-the-middle attack。