Spring 安全性 - 基本身份验证 header 是针对所有 URL 而不仅仅是针对安全端点发送的
Spring Security - Basic Authentication header is sent for all URLs instead of just for secured endpoint
在 Spring Security/Boot 应用程序中,我为特定 URL-pattern 配置了基本身份验证:
http.antMatcher(StringUtils.join("myURL", "/**")).authorizeRequests().anyRequest().authenticated().and().httpBasic().realmName("realmName");
这就像一个魅力,因为当我请求该模式的 URL 时,浏览器会提示我提供凭据,然后我可以访问该端点。
但是,在成功授权此端点后,浏览器会发送带有 "Basic ..." 令牌的授权 header,即使对于与上述代码中配置的请求无关的 URLs 请求.例如网站主页。
这会导致 webapp 的其他授权机制(即 keycloak)启动,因为它们需要授权内的有效令牌 header。我知道我可以以一种不会尝试解释以 "Basic " 开头的 Authorization-Header 的方式配置 keycloak,但似乎造成这种困境的根本原因是 Header为不属于 basic-auth URL 的请求发送 get。
有什么方法可以告诉 Spring 安全/浏览器/任何人 basic-auth 授权 header 只应包含在请求中URL 匹配 http-basic 配置的模式?这不应该是标准行为吗?
示例 URLs:
- localhost:8083/myURL :我希望浏览器发送
身份验证 Header
- localhost:8083/myURL/moreURL :我也希望浏览器发送
身份验证 Header
- localhost:8083/someOtherURL :我不希望浏览器发送
身份验证 Header、但确实如此!
- localhost:8083/someOtherURL/moreURL :同样的事情,浏览器发送
Header没想到
您的应用程序必须使用不同的包含目录和不同的领域,请参阅 RFC 2617:
2 Basic Authentication Scheme
[...]
A client SHOULD assume that all paths at or deeper than the depth of
the last symbolic element in the path field of the Request-URI also
are within the protection space specified by the Basic realm value of
the current challenge. A client MAY preemptively send the
corresponding Authorization header with requests for resources in
that space without receipt of another challenge from the server.
另见 Chromium source:
// Helper to find the containing directory of path. In RFC 2617 this is what
// they call the "last symbolic element in the absolute path".
// Examples:
// "/foo/bar.txt" --> "/foo/"
// "/foo/" --> "/foo/"
在您的情况下,您的一个应用程序的包含目录是 /
。另一个应用程序位于该包含目录的 sub-path 中。因此,您的浏览器会先发制人地向两个应用程序发送相同的 Authorization
header。
在 Spring Security/Boot 应用程序中,我为特定 URL-pattern 配置了基本身份验证:
http.antMatcher(StringUtils.join("myURL", "/**")).authorizeRequests().anyRequest().authenticated().and().httpBasic().realmName("realmName");
这就像一个魅力,因为当我请求该模式的 URL 时,浏览器会提示我提供凭据,然后我可以访问该端点。 但是,在成功授权此端点后,浏览器会发送带有 "Basic ..." 令牌的授权 header,即使对于与上述代码中配置的请求无关的 URLs 请求.例如网站主页。
这会导致 webapp 的其他授权机制(即 keycloak)启动,因为它们需要授权内的有效令牌 header。我知道我可以以一种不会尝试解释以 "Basic " 开头的 Authorization-Header 的方式配置 keycloak,但似乎造成这种困境的根本原因是 Header为不属于 basic-auth URL 的请求发送 get。
有什么方法可以告诉 Spring 安全/浏览器/任何人 basic-auth 授权 header 只应包含在请求中URL 匹配 http-basic 配置的模式?这不应该是标准行为吗?
示例 URLs:
- localhost:8083/myURL :我希望浏览器发送 身份验证 Header
- localhost:8083/myURL/moreURL :我也希望浏览器发送 身份验证 Header
- localhost:8083/someOtherURL :我不希望浏览器发送 身份验证 Header、但确实如此!
- localhost:8083/someOtherURL/moreURL :同样的事情,浏览器发送 Header没想到
您的应用程序必须使用不同的包含目录和不同的领域,请参阅 RFC 2617:
2 Basic Authentication Scheme
[...]
A client SHOULD assume that all paths at or deeper than the depth of the last symbolic element in the path field of the Request-URI also are within the protection space specified by the Basic realm value of the current challenge. A client MAY preemptively send the corresponding Authorization header with requests for resources in that space without receipt of another challenge from the server.
另见 Chromium source:
// Helper to find the containing directory of path. In RFC 2617 this is what // they call the "last symbolic element in the absolute path". // Examples: // "/foo/bar.txt" --> "/foo/" // "/foo/" --> "/foo/"
在您的情况下,您的一个应用程序的包含目录是 /
。另一个应用程序位于该包含目录的 sub-path 中。因此,您的浏览器会先发制人地向两个应用程序发送相同的 Authorization
header。