Quick revision
The short answer
We define a separate SecurityFilterChain bean for Actuator, scoped with http.securityMatcher(EndpointRequest.toAnyEndpoint()), so it only applies to actuator paths. Inside it, EndpointRequest.to("health", "info") is permitted to everyone and anyRequest() requires hasRole("OPS"). The bean gets a higher priority with @Order than the application's own chain, which handles every other request.
What to remember
In this answer, we wrote a separate SecurityFilterChain for Actuator, scoped with securityMatcher(EndpointRequest.toAnyEndpoint()), that permits health and info to everyone and requires ROLE_OPS for every other endpoint. We gave it @Order(1) so it runs before the catch-all application chain, saw how a wrong order breaks the Kubernetes probes, limited health details to operators, and proved the rules with MockMvc tests.
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.
We define a separate SecurityFilterChain bean for Actuator, scoped with http.securityMatcher(EndpointRequest.toAnyEndpoint()), so it only applies to actuator paths. Inside it, EndpointRequest.to("health", "info") is permitted to everyone and anyRequest() requires hasRole("OPS"). The bean gets a higher priority with @Order than the application's own chain, which handles every other request.
Why this question is asked
Actuator endpoints are some of the most sensitive URLs in a Spring Boot application: env shows configuration, loggers can change log levels, heapdump hands out the whole memory. At the same time, health must stay open for load balancers and Kubernetes, and info is often harmless and useful. Interviewers ask this coding question to see whether we know how Spring Security picks a filter chain and how Boot's EndpointRequest helper matches actuator paths. In this answer, we will recap how multiple filter chains work, write the actuator chain, explain each line, see why the order matters, and test it.
1A Quick Recap of Security Filter Chains
Spring Security protects requests with a chain of servlet filters. A SecurityFilterChain bean is one such chain, built with the HttpSecurity builder, and an application can have several. For every request, Spring Security goes through the chains in order and uses the first one whose request matcher matches. Only that chain runs; the others are skipped.
Two parts of the builder are easy to confuse. securityMatcher decides which requests this chain handles at all. authorizeHttpRequests decides, within the chain, what each matching request needs. A chain with no securityMatcher matches every request, which is why such a chain must come last.
EndpointRequest is a Spring Boot helper that builds request matchers for actuator endpoints. It knows the configured base path, /actuator by default, any custom paths and the management port, so we never hard-code /actuator/** strings that break when someone changes management.endpoints.web.base-path.
2Writing the Actuator Chain
Here is the full configuration, with the actuator chain first and the application chain second.
EndpointRequest lives in Spring Boot's actuator auto-configuration, in the servlet security package in Boot 3; in Boot 4 the package moved with the modularised auto-configuration, so check your version's import. There is also a reactive variant for WebFlux, used with ServerHttpSecurity.
3What Each Line Does
securityMatcher(EndpointRequest.toAnyEndpoint()) makes this chain apply only to actuator endpoints. A request to /orders does not match, so Spring Security moves on to the next chain. EndpointRequest.to("health", "info") matches the health and info endpoints, including their sub-paths such as /actuator/health/liveness and /actuator/health/readiness, so the Kubernetes probes stay open as well.
anyRequest().hasRole("OPS") covers every other actuator endpoint. hasRole adds the ROLE_ prefix, so the user needs the authority ROLE_OPS. Notice that the order of rules inside authorizeHttpRequests also matters: the specific permitAll rule must come before anyRequest(), which Spring Security enforces by refusing rules after anyRequest().
The last three lines suit operators and tools that call Actuator with basic authentication. Disabling CSRF protection in this chain is reasonable because the callers do not use browser sessions, and without it a POST to /actuator/loggers from a script would be rejected with 403 even with correct credentials. The application chain keeps its own settings untouched.
- Request arrives
- Matches EndpointRequest.toAnyEndpoint()?YesUse actuatorChain (Order 1)NoTry the next chain
- Is it health or info?YespermitAll: 200NoNeeds ROLE_OPS
- Authenticated with ROLE_OPS?YesEndpoint runsNo401 without credentials, 403 without the role
- Non-actuator request goes to appChain (Order 2)JWT required
5 steps in order. Play them, or step through them one at a time.
4Why the Order Matters
Suppose we forget @Order, or give the application chain the higher priority. The application chain has no securityMatcher, so it matches everything, including /actuator/health. Spring Security picks it first, and now the probes need a JWT that Kubernetes does not have. The pods fail readiness, and the deployment never becomes ready.
The rule is simple: chains with a narrow securityMatcher come first, and the catch-all chain comes last. A lower @Order value means higher priority, so @Order(1) on the actuator chain and @Order(2) on the application chain express exactly that. Spring Security also refuses to start when a chain that matches any request is placed before another chain, which catches many of these mistakes early.
5Health Details for Operators Only
Health being open does not mean everyone should see the details. With show-details set to when-authorized and roles set to OPS, anonymous callers see only the overall status, while an authenticated operator sees each component.
Notice that exposure is a separate setting from security. An endpoint that is not exposed returns 404 whatever the security rules say, so we expose only what operators actually use.
6Testing the Rules
A short MockMvc test proves the rules and protects them from future changes.
7Follow-up questions
Why not just use requestMatchers("/actuator/**")?
Because the path can change. If someone sets management.endpoints.web.base-path or moves Actuator to a different management port, hard-coded patterns silently stop matching and the endpoints may become unprotected. EndpointRequest reads the actual configuration.
How would you exclude one endpoint from the OPS rule?
EndpointRequest.toAnyEndpoint().excluding("health", "info") builds a matcher for every endpoint except those. It is handy for a rule like requestMatchers(EndpointRequest.toAnyEndpoint().excluding("health")).hasRole("OPS").
Is it enough to secure Actuator with roles if it runs on the main port?
Roles are necessary but I would add network isolation too: put Actuator on management.server.port and only allow that port inside the cluster. Then a mistake in the security rules is not immediately reachable from the internet.
8Conclusion
In this answer, we wrote a separate SecurityFilterChain for Actuator, scoped with securityMatcher(EndpointRequest.toAnyEndpoint()), that permits health and info to everyone and requires ROLE_OPS for every other endpoint. We gave it @Order(1) so it runs before the catch-all application chain, saw how a wrong order breaks the Kubernetes probes, limited health details to operators, and proved the rules with MockMvc tests.
Open with
I would write a separate SecurityFilterChain just for Actuator and give it a higher priority with @Order(1). It starts with http.securityMatcher(EndpointRequest.toAnyEndpoint()), so the chain only handles actuator paths, and EndpointRequest reads the configured base path and management port instead of me hard-coding /actuator/**.
Then, if they want more
Inside authorizeHttpRequests, I first permit EndpointRequest.to("health", "info") with permitAll. That also covers sub-paths like /actuator/health/liveness and readiness, so Kubernetes probes keep working. Then anyRequest().hasRole("OPS") protects everything else, which means the user needs the ROLE_OPS authority. For operator tools I enable HTTP basic, make the chain stateless and disable CSRF there, otherwise POSTs to loggers would be rejected.
The application's own chain has no securityMatcher and gets @Order(2), because a catch-all chain must come last; if it came first, the probes would suddenly need a JWT and the pods would never become ready.
I would also set health show-details to when-authorized for the OPS role, expose only the endpoints we use, and add a MockMvc test that checks 200 for health, 401 without credentials, 403 for a wrong role and 200 for OPS.
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 Boot reference, Actuator
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 "Write a SecurityFilterChain that allows anyone to call health and info but requires role...": https://scrivio-interview-library.netlify.app/spring-boot/actuator/8e33e84ae1/