In a Spring Security/Boot application I have configured basic authentication for a specific URL-pattern:
http.antMatcher(StringUtils.join("myURL", "/**")).authorizeRequests().anyRequest().authenticated().and().httpBasic().realmName("realmName");
This works like a charm, as in when I request an URL of that pattern I am prompted by the browser to provide credentials and afterwards I can access that endpoint. However, after successfully authorizing for this endpoint, the browser sends the Authorization header with the "Basic ..." token even for requests to URLs that have nothing to do with the one configured in the above code. For example the websites homepage.
This causes other authorization mechanisms of the webapp, namely keycloak, to fire because they expect valid tokens inside of the Authorization header. I know that I can configure keycloak in a way that it does not try to interpret an Authorization-Header that begins with "Basic ", but it seems like the root cause of this dilemma is that the Header get's sent for requests that don't belong to the basic-auth URL.
Is there any way that I can tell Spring Security / the browser / whoever that the basic-auth Authorization header should only be included in the request if the request is for an URL matching the pattern that http-basic was configured for? Shouldn't this be standard behavior anyway?
Example URLs:
- localhost:8083/myURL : I expect the browser to send the Authentication Header
- localhost:8083/myURL/moreURL : I also expect the browser to send the Authentication Header
- localhost:8083/someOtherURL : I do not expect the browser to send the Authentication Header, but it does!
- localhost:8083/someOtherURL/moreURL : Same thing, browser sends the Header unexpectedly