Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

This configuration lets everyone access /admin/users: http.authorizeHttpRequests(a -> a.anyRequest().authenticated().requestMatchers("/admin/**").hasRole("ADMIN")) Explain the problem. (In recent versions this throws at startup; why?)

Spring Security checks request rules in the order we declare them and stops at the first matcher that fits. Because anyRequest() is declared first and matches every request, the /admin/** rule would never be reached, and any authenticated user, not only admins, could open /admin/users. Recent versions catch this at startup and throw an exception when a rule is declared after anyRequest(); the fix is to put specific matchers first and anyRequest() last, and to prove it with tests for users with and without the role.

Why this question is asked

This is one of the most common authorization mistakes, and it is dangerous because the application works perfectly in a demo. Every page loads, logging in is required, and nobody notices that the admin pages accept everyone. Interviewers ask it to see whether we understand how the rule list is evaluated and why the framework now refuses to start with such a configuration. In this answer, we will recap how authorizeHttpRequests evaluates rules, walk through the broken configuration request by request, see why newer versions fail fast, fix the order and finish with tests that would have caught it.

1A Quick Recap: An Ordered List of Rules

authorizeHttpRequests builds an ordered list of pairs. Each pair has a request matcher, which answers whether this rule applies to the current request, and an authorization rule, such as authenticated() or hasRole("ADMIN"). For every request, the AuthorizationFilter walks the list from the top and uses the first pair whose matcher matches. It never looks further down the list, even if a later rule would be stricter.

This is the same idea as firewall rules or a chain of if and else if statements. Order is part of the meaning. anyRequest() is simply a matcher that always answers yes, which is why it only makes sense at the bottom, as the catch-all for everything not listed above it.

2Walking Through the Broken Configuration

Let us take the configuration from the question and follow a request from a user who is logged in but has only ROLE_USER.

Java
http.authorizeHttpRequests(a -> a
.anyRequest().authenticated() // rule 1: matches EVERY request
.requestMatchers("/admin/**").hasRole("ADMIN")); // rule 2: never reached
// Imagined evaluation, if it were allowed to start:
// GET /admin/users as anonymous -> rule 1 matches -> not authenticated -> 401 / login
// GET /admin/users as USER -> rule 1 matches -> authenticated -> ALLOWED
// GET /admin/users as ADMIN -> rule 1 matches -> authenticated -> allowed
Step throughHow the broken list is evaluated
  1. Rule 1: anyRequest() matches?always true
    YesApply authenticated(), stopNonever happens
  2. Rule 2: /admin/** hasRole(ADMIN)never evaluated
  3. Result for a plain useradmin page allowed

3 steps in order. Play them, or step through them one at a time.

Notice that the question says everyone can access /admin/users. Strictly, it is every authenticated user: anonymous visitors are still sent to the login page by rule 1. But in most companies every employee or customer can log in, so in practice the admin area is open to all of them. The admin rule is dead code. It reads as if it protects something, which is worse than having no rule, because reviewers trust it.

3Why Recent Versions Throw at Startup

Because this mistake is easy to make and silent at runtime, Spring Security added a guard. Once anyRequest() has been configured, calling requestMatchers() on the same registry fails with an IllegalStateException, with a message along the lines of "Can't configure requestMatchers after anyRequest" (the wording varies by version). The application context fails to start, so the bug cannot reach production.

The reasoning is simple: a rule after anyRequest() can never match, so there is no valid reason to write one. Failing fast turns a security hole into a startup error that the developer sees in seconds. Older configurations written with the deprecated authorizeRequests() and antMatchers() got a similar check in later 5.x releases, so an upgrade can suddenly surface an ordering bug that has been in production for years. If that happens, treat it as a security finding, not just an upgrade chore, and check the logs for admin access by non-admins.

4The Fix: Specific First, Catch-All Last

We reverse the order so that the narrow rule is checked before the broad one. When several specific rules overlap, the most specific must come first as well, for example /admin/audit/** before /admin/**.

Java
http.authorizeHttpRequests(a -> a
.requestMatchers("/actuator/health", "/public/**").permitAll()
.requestMatchers("/admin/audit/**").hasRole("AUDITOR") // narrower admin area first
.requestMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().authenticated()); // catch-all, always last
// GET /admin/users as USER -> rule 3 matches -> no ROLE_ADMIN -> 403 Forbidden
// GET /admin/users as ADMIN -> rule 3 matches -> allowed
// GET /orders as USER -> rule 4 matches -> allowed
// GET /admin/audit/x as ADMIN -> rule 2 matches -> no ROLE_AUDITOR -> 403

Notice the last line. Order works in both directions: because the audit rule comes first, an admin without ROLE_AUDITOR is refused there, even though the /admin/** rule would have allowed them. That is usually what we want, but it shows why each new rule must be placed deliberately. Remember too that hasRole("ADMIN") checks for the authority ROLE_ADMIN, so the users' roles must carry that prefix.

5Proving It With Tests

Configuration that decides who sees admin pages deserves tests that run on every build. spring-security-test lets us call endpoints as fake users with chosen roles, without a real login.

Java
@WebMvcTest(AdminController.class)
@Import(SecurityConfig.class)
class AdminSecurityTest {
@Autowired MockMvc mvc;
@Test @WithMockUser(roles = "USER")
void plainUserIsForbidden() throws Exception {
mvc.perform(get("/admin/users")).andExpect(status().isForbidden()); // 403
}
@Test @WithMockUser(roles = "ADMIN")
void adminIsAllowed() throws Exception {
mvc.perform(get("/admin/users")).andExpect(status().isOk()); // 200
}
@Test
void anonymousIsRejected() throws Exception {
mvc.perform(get("/admin/users")).andExpect(status().is4xxClientError()); // 401 or redirect, per setup
}
}

The important test is the first one. Teams often test only that admins can reach admin pages, which passes with the broken configuration too. A security test is valuable when it proves that someone is refused. A user with the wrong role, an anonymous user and, in multi-tenant systems, a user from another tenant are the three cases worth having for every protected area.

6Follow-up questions

Is this the same as the order of multiple SecurityFilterChain beans?

It is the same idea one level up. With several SecurityFilterChain beans, Spring Security uses the first chain whose securityMatcher matches the request, ordered by @Order. A chain without a securityMatcher matches everything, so it must have the lowest priority, just as anyRequest() must come last inside a chain.

Should the catch-all be authenticated() or denyAll()?

denyAll() is safer for APIs, where every endpoint should have an explicit rule; a forgotten endpoint then fails closed and someone notices quickly. authenticated() is common for applications with many ordinary pages. Either is far better than permitAll() as the last rule.

Why did the application work fine in manual testing?

Because manual testers usually log in as an admin to test admin pages, and the broken rule allows admins too. The bug only shows when someone without the role tries the URL, which is exactly the case negative tests cover.

7Conclusion

In this answer, we saw that authorizeHttpRequests is an ordered list where the first matching rule decides. With anyRequest().authenticated() first, every authenticated user passes before the admin rule is ever checked. Recent versions refuse to start when a rule follows anyRequest(), turning a silent hole into an obvious error. The fix is to order rules from most specific to least specific with anyRequest() last, and to keep tests that prove users without the role get a 403.

What to say in the interviewAbout 70 seconds aloud

Open with

The problem is ordering. Spring Security evaluates the authorizeHttpRequests rules top to bottom and the first matcher that matches decides. anyRequest() matches every request, so with it first, every request is decided by authenticated() and the hasRole ADMIN rule is never reached.

Then, if they want more

That means any logged-in user, not just admins, can open /admin/users; only anonymous users are stopped. Recent Spring Security versions guard against this: once anyRequest() has been configured, calling requestMatchers afterwards throws an IllegalStateException while the configuration is built, so the application fails to start.

The reasoning is that a rule after anyRequest can never match, so writing one is always a bug, and failing fast is better than shipping a rule that only looks like protection. The fix is to declare specific matchers first, the narrowest before the broader ones, and finish with anyRequest().authenticated() or denyAll().

Then I would add tests with spring-security-test: a user with role USER must get 403 on /admin/users, an admin must get 200, and an anonymous request must be rejected. The negative test is the important one, because the broken configuration passes the admin test too.

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 "This configuration lets everyone access /admin/users: http.authorizeHttpRequests(a ->...": https://scrivio-interview-library.netlify.app/spring-boot/authorization/579b0e7dd8/