Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

A secured Spring Boot app shows a login page or a 401 instead of the custom 404 page for unknown URLs, and the logs show access denied for /error. Why, and how do you fix it?

Spring Boot renders the 404 page by forwarding the request to /error, and since Spring Security 6 (Spring Boot 3) the authorization filter also runs on that error dispatch. An anonymous request to /error is not covered by any permitAll rule, so it is denied, and the user sees the login redirect or a 401 instead of the 404 page. We fix it by permitting /error or, more precisely, the ERROR dispatcher type, then verify that the original status survives and that the page reveals nothing sensitive, with a test against a running server.

Why this question is asked

This problem shows up in almost every application that upgrades to Spring Boot 3. Nothing about the error page changed, yet users who mistype a URL suddenly land on the login page, and API clients get 401 for endpoints that simply do not exist, which sends them chasing token problems that are not there. Interviewers ask it because the answer requires knowing two mechanisms at once, Boot's error dispatch and Security's filter chain, and how they meet. In this answer, we will recap how the 404 page is produced, see why Security denies it, apply the fix, check that the status code is right, and write a test that would have caught it.

1A Quick Recap: The 404 Page Is a Second Request Inside the First

When a URL matches no controller or static resource, Spring MVC sets the response status to 404 through response.sendError(). It does not write a page itself. The servlet container sees the error status and performs an ERROR dispatch: it sends the same request object through the application again, this time to /error, so that Boot's BasicErrorController can render the custom 404 page or the JSON body.

That second pass goes through the servlet filters again, and Spring Security is a filter. The DispatcherType of the second pass is ERROR instead of REQUEST, which is how filters can tell the two apart.

2Why Spring Security Denies /error

In Spring Security 6, the AuthorizationFilter behind authorizeHttpRequests applies to every dispatcher type by default, including ERROR and FORWARD. Earlier versions only checked the original request. So the error dispatch to /error is authorized like any other request. If our rules are permitAll() for /public/** and authenticated() for anyRequest(), then /error falls under anyRequest(), and an anonymous user is not authenticated.

Security then does what it always does for an anonymous user without access: it calls the authentication entry point. With form login, that is a 302 redirect to /login; with HTTP Basic or a resource server, it is a 401. The log shows access denied for /error, and the 404 that started everything is lost.

Java
@Bean
SecurityFilterChain security(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(auth -> auth
.requestMatchers("/", "/public/**", "/css/**").permitAll()
.anyRequest().authenticated())
.formLogin(Customizer.withDefaults());
return http.build();
}
// Anonymous GET /public/old-page (no such page):
// 1. REQUEST /public/old-page -> permitted, no handler -> sendError(404)
// 2. ERROR /error -> anyRequest().authenticated() -> denied
// 3. entry point -> 302 Location: /login (expected: 404 page)
Step throughA 404 that turns into a login redirect
BrowserSecurity filterDispatcherServlet/error
GET /public/old-page (REQUEST)
permitted: /public/**
no handler: sendError(404)
ERROR dispatch to /error: authenticated() required
anonymous, so 302 to /login

5 messages in order. Play them, or step through them one at a time.

The fix is to let the ERROR dispatch through, so the 404 page can render.

Notice that the original URL was public; only the error page was protected. For an unknown URL outside the public area, the original request is already denied before Spring MVC can look for a handler, so an anonymous user is sent to login without anyone knowing the page does not exist. That part is usually correct: it avoids telling an anonymous visitor which private URLs exist.

3The Fix: Permit the Error Dispatch

We add a rule that lets the error dispatch through. Permitting the ERROR dispatcher type covers every error, whatever path the error page uses. Permitting /error as a path also covers a client that requests /error directly, which is harmless because the page holds no secrets. Many applications add both, and the rules must come before anyRequest(), because Security uses the first matching rule.

Java
http.authorizeHttpRequests(auth -> auth
.dispatcherTypeMatchers(DispatcherType.ERROR).permitAll() // the forward to /error
.requestMatchers("/error").permitAll() // direct requests to /error
.requestMatchers("/", "/public/**", "/css/**").permitAll()
.anyRequest().authenticated())
.formLogin(Customizer.withDefaults());
// Anonymous GET /public/old-page -> 404 with the custom error/404.html
// Logged-in GET /orders/does-not-exist -> 404 with the custom page
// Anonymous GET /orders/123 -> 302 to /login (still protected, as intended)

Notice that this does not open any real endpoint. The ERROR dispatch only renders the error page for a request that already failed, and the original request was already authorized on its own. A user who is not allowed to see /orders/123 still never reaches the controller.

4Checking That the Original Status Survives

After the fix, we check the status code, not only that a page appears. A 404 that becomes a 302 or a 401 is worse for API clients than for browsers: a client that receives 401 refreshes its token and tries again, gets 401 again, and reports an authentication outage, when the real problem is a wrong URL. So for API security chains we also make sure the entry point returns 401 without a redirect (for example with HttpStatusEntryPoint) and that unknown URLs under the API return 404 to authenticated clients.

We also check what the page shows. The custom error page is now visible to anonymous users, so it must not include stack traces, exception messages or internal paths. That means keeping server.error.include-stacktrace and include-message at their default never, and making sure the template shows only the status, a friendly sentence and perhaps a support id.

5Testing It Against a Running Server

MockMvc runs the DispatcherServlet and the security filters, but it does not perform the container's error dispatch, so an anonymous MockMvc request to an unknown URL returns 404 even while the bug is present. We can reproduce the second pass by calling /error with the ERROR dispatcher type and the error status attribute set, but the most honest test starts a real server and sends a request the way a browser does.

Java
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ErrorPageSecurityTest {
@Autowired TestRestTemplate rest; // anonymous by default
@Test
void unknownPublicUrlShows404() {
ResponseEntity<String> r = rest.getForEntity("/public/old-page", String.class);
assertThat(r.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); // was 302 to /login before the fix
assertThat(r.getBody()).contains("We could not find that page")
.doesNotContain("Exception"); // no internals on a public page
}
}

Notice that this test fails before the fix with a 302, as long as the client does not follow redirects (the usual TestRestTemplate setup; check your version), and passes after it. In Spring Boot 4 there are newer test clients as well; check your version for the recommended one. A browser test that visits an unknown URL gives the same assurance for the HTML page.

6Follow-up questions

Why did this work in Spring Boot 2?

Spring Security 5 only applied authorization to the original REQUEST dispatch by default, so the forward to /error was not checked. Spring Boot 2.6 and 2.7 added their own check for error pages to avoid leaking details, and Spring Security 6 then made authorizing every dispatch the default. So the same configuration behaves differently after the upgrade.

Is it safe to permit all ERROR dispatches?

Yes, as long as the error page itself is harmless. The ERROR dispatch only happens after the original request failed, and the original request was authorized on its own terms. What we must not do is put sensitive information on the error page, since anonymous users can now see it.

Should an anonymous user get 404 for a private URL that does not exist?

Usually not. If the URL is under a protected area, sending the anonymous user to login, or returning 401, avoids revealing which URLs exist. The fix in this answer only makes sure that requests which were allowed in the first place keep their real error status.

7Conclusion

In this answer, we saw that Boot's 404 page comes from an ERROR dispatch to /error, a second pass through the filters, and that Spring Security 6 authorizes that pass too. An anonymous user therefore gets the login redirect or a 401 instead of the 404 page. We fixed it by permitting the ERROR dispatcher type and /error before the anyRequest() rule, checked that the original status reaches the client, kept sensitive details off the now public error page, and wrote a test against a running server, because MockMvc does not perform the error dispatch.

What to say in the interviewAbout 80 seconds aloud

Open with

The custom 404 page is not rendered by the original request. Spring MVC calls sendError(404), and the servlet container then makes an ERROR dispatch to /error, which goes through the filter chain again.

Then, if they want more

Since Spring Security 6, which is what Spring Boot 3 uses, authorization applies to every dispatcher type, including ERROR. So if my rules are permitAll for some public paths and authenticated for anyRequest, the dispatch to /error falls under anyRequest, an anonymous user is denied, and the entry point sends a redirect to login or a 401.

That is the access denied for /error in the logs. The fix is to permit the error dispatch before the catch-all rule, either with dispatcherTypeMatchers(DispatcherType.ERROR).permitAll() or requestMatchers("/error").permitAll(), and I usually add both. That does not open real endpoints, because the original request was already authorized.

Then I check the status: a 404 must stay a 404, especially for API clients, which otherwise think their token is broken. I also make sure the error page shows nothing sensitive, since anonymous users can see it now, so include-stacktrace and include-message stay at never.

And I test it with a SpringBootTest on a random port, because MockMvc does not do the container's error dispatch and would pass even with the bug.

About this answer

Last updated
12 November 2026
Versions it names
Spring Security 6, Spring Boot 3, Spring Boot 2.6, Spring Boot 4
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.
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 secured Spring Boot app shows a login page or a 401 instead of the custom 404 page for...": https://scrivio-interview-library.netlify.app/spring-boot/error-handling/6bd5340fe1/