Quick revision
The short answer
The redirect comes from form login's entry point, which sends unauthenticated requests to the login page. When both form login and HTTP Basic are enabled, Spring Security picks the entry point from request headers such as Accept and X-Requested-With, so a client that does not actually send credentials, or sends an Accept header that looks like a browser, gets the 302. For an API we give it a separate chain with httpBasic only and a 401 entry point, or map a 401 entry point to the API paths with defaultAuthenticationEntryPointFor, and we make sure the client sends credentials preemptively instead of waiting for a challenge.
What to remember
In this answer, we saw that the 302 comes from form login's entry point, which Spring Security chose because the request carried no credentials and its headers looked like a browser's. Non-preemptive clients wait for a 401 challenge that never comes, and then follow the redirect to an HTML page. We fixed the server with a separate API chain that uses HTTP Basic only, or a 401 entry point mapped to the API paths, and fixed the client by sending the Basic header preemptively over TLS.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The redirect comes from form login's entry point, which sends unauthenticated requests to the login page. When both form login and HTTP Basic are enabled, Spring Security picks the entry point from request headers such as Accept and X-Requested-With, so a client that does not actually send credentials, or sends an Accept header that looks like a browser, gets the 302. For an API we give it a separate chain with httpBasic only and a 401 entry point, or map a 401 entry point to the API paths with defaultAuthenticationEntryPointFor, and we make sure the client sends credentials preemptively instead of waiting for a challenge.
Why this question is asked
A REST client that receives an HTML redirect instead of a 401 is a classic integration headache. Many HTTP clients follow the redirect automatically, receive the login page with status 200, and then fail while parsing HTML as JSON, so the error message points nowhere near security. Interviewers ask this because the cause combines two details: how Spring Security chooses between entry points, and how HTTP clients send Basic credentials. In this answer, we will recap what an entry point does, see how Spring Security chooses one when both login methods are on, find out why this client lands on the wrong one, and then fix both the server and the client.
1A Quick Recap of Entry Points
When a request needs authentication and has none, Spring Security calls an AuthenticationEntryPoint, the component that tells the client how to authenticate. Each login method brings its own. Form login uses LoginUrlAuthenticationEntryPoint, which answers 302 with Location: /login, right for a browser. HTTP Basic uses BasicAuthenticationEntryPoint, which answers 401 with WWW-Authenticate: Basic realm="...", right for a program.
Notice that the entry point is only called when the request reaches authorization unauthenticated. If the client sends a Basic header with wrong credentials, BasicAuthenticationFilter rejects it itself and calls the Basic entry point, which gives a 401. So a 302 tells us something specific: no usable credentials reached the server, and the request was classified as a browser request.
2How Spring Security Chooses Between Them
With both formLogin() and httpBasic() configured, as in Spring Boot's default chain, each configurer registers its entry point together with a request matcher describing the requests it is meant for. The exception handling then uses a delegating entry point: the first one whose matcher fits the request wins, and the form login entry point is the default for anything else.
The matchers look at headers. HTTP Basic claims requests with X-Requested-With: XMLHttpRequest and requests whose Accept header prefers non-HTML types such as application/json, while the form login matcher claims requests that accept text/html. A request whose headers fit neither clearly ends up with the default. The exact matching rules differ a little between versions, but the principle is that the client's headers, not its intent, decide.
- Authorization: Basic header with valid credentials?YesAuthenticated: 200NoContinue
- Basic header present but wrong?YesBasicAuthenticationFilter: 401NoNo credentials at all
- Headers look like an API call (JSON Accept, X-Requested-With)?YesBasic entry point: 401NoForm login entry point: 302 to /login
3 steps in order. Play them, or step through them one at a time.
A 302 means no credentials arrived and the request looked like a browser.
3Why This Client Gets the Redirect
Two client behaviours combine. First, many HTTP clients do not send Basic credentials on the first request. They are configured with a username and password but wait for a 401 with WWW-Authenticate: Basic, the challenge, and only then retry with the header. That is called non-preemptive authentication. Apache HttpClient, for example, behaves this way unless told otherwise. Second, the client sends Accept: */* or text/html, or no special header at all, so Spring Security classifies the unauthenticated first request as a browser request.
So the first request has no credentials, gets the form login entry point and a 302, and the client never sees the 401 challenge it was waiting for. If it follows redirects, it fetches /login and receives the HTML login page with status 200.
6 messages in order. Play them, or step through them one at a time.
Notice that the same unauthenticated request gets a 302 or a 401 depending only on the Accept header, and that a client that sends credentials up front never meets either.
4Fix the Server: a Separate Chain for the API
The clearest fix is to stop mixing browser and API behaviour on the same paths. We give /api/** its own SecurityFilterChain with HTTP Basic only, stateless sessions and a 401 entry point, and keep form login for the browser pages in a second chain.
If a single chain must stay, we can map an entry point to the API paths instead, with exceptionHandling(e -> e.defaultAuthenticationEntryPointFor(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED), apiMatcher)), where apiMatcher is a request matcher for /api/**; the way to build a path matcher has changed between versions, so check yours. A plain 401 entry point loses the Basic challenge, so for non-preemptive clients the separate chain with httpBasic() is the better fix.
5Fix the Client: Send Credentials Preemptively
Even with the server fixed, a non-preemptive client makes two requests for every call, the first one always rejected. Sending the Authorization header on the first request is faster and avoids depending on the challenge at all.
Notice the https base URL. Basic credentials are only Base64-encoded, so the preemptive header must only ever travel over TLS.
6Follow-up questions
Why does the browser still get the login page after the fix?
Because browser pages are handled by the second chain, which still has form login. That is the intended split: people get a page, programs get a 401. FilterChainProxy picks the first chain whose securityMatcher fits, so the API chain must have the lower @Order and a matcher.
The client sends credentials but still gets 302. What else could it be?
Check that the header really arrives: a proxy or load balancer may strip Authorization, the client may send it only to a different host after a redirect, or the header may be malformed. TRACE logging for Spring Security shows whether BasicAuthenticationFilter saw a header for the request.
Should a modern API use HTTP Basic at all?
For internal scripts and simple service accounts over TLS it is acceptable. For anything user-facing or widely distributed, tokens from an identity provider are better, because they expire, carry scopes and do not expose a long-lived password on every request.
7Conclusion
In this answer, we saw that the 302 comes from form login's entry point, which Spring Security chose because the request carried no credentials and its headers looked like a browser's. Non-preemptive clients wait for a 401 challenge that never comes, and then follow the redirect to an HTML page. We fixed the server with a separate API chain that uses HTTP Basic only, or a 401 entry point mapped to the API paths, and fixed the client by sending the Basic header preemptively over TLS.
Open with
The 302 comes from form login's entry point, LoginUrlAuthenticationEntryPoint, which redirects unauthenticated requests to /login. With both form login and Basic enabled, like Boot's default chain, Spring Security registers both entry points with request matchers and picks one from the request headers: X-Requested-With or an Accept header preferring JSON gets the Basic entry point and a 401, anything that looks like a browser gets the redirect.
Then, if they want more
Importantly, a wrong Basic header gives 401 from BasicAuthenticationFilter, so a 302 tells me no credentials arrived at all. The usual reason is a non-preemptive client: it has a username and password configured but waits for a 401 challenge before sending them.
Its first request has no header and an Accept of */*, so it gets the redirect, never sees the challenge, often follows the redirect and then fails parsing the login page HTML.
To fix the server, I'd give /api/** its own SecurityFilterChain with httpBasic only, stateless sessions and CSRF off, at a lower order than the browser chain with form login. Then any unauthenticated API call gets a 401 with WWW-Authenticate: Basic. If it has to stay one chain, defaultAuthenticationEntryPointFor can map a 401 entry point to the API paths.
On the client side, I'd send the Authorization header preemptively, for example with setBasicAuth in RestClient's default headers, and always over HTTPS, since Basic credentials are only Base64.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Security reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A REST client calling a Spring Boot API with HTTP Basic credentials receives a 302...": https://scrivio-interview-library.netlify.app/spring-boot/authentication/d8b85dffe7/