Sub topic 10
Securing Actuator Endpoints with Spring Security
Actuator endpoints can expose configuration values, heap and thread data, and the ability to change log levels at runtime. This chapter shows how to protect them with Spring Security. It assumes you know basic Spring Security concepts (filter chains, roles, HTTP Basic), have read the chapter on exposing endpoints, and can edit application.yml. The examples target Spring Boot 3 and Spring Security 6.
Exposure and Security Are Separate Layers
Two independent settings decide whether a caller can use an endpoint.
- Exposure (
management.endpoints.web.exposure.include) decides whether the endpoint exists over HTTP at all. A request to an unexposed endpoint returns 404. - Authorization (Spring Security) decides who may call an exposed endpoint.
Treat exposure as the coarse filter and security as the fine one. Expose only what a monitoring system or operator needs, then restrict that set by role.
Default Behavior
If Spring Security is on the classpath and you define no SecurityFilterChain bean, Boot configures a default chain. Every Actuator endpoint other than health requires authentication, using a generated password printed at startup. That default is safe but awkward for real use: the password changes on every start and the user has no role.
As soon as you define your own SecurityFilterChain bean, the default backs off completely. After that, nothing is protected unless your chain protects it. This is the most common way to expose Actuator by accident.
A Dedicated Filter Chain for Actuator
EndpointRequest is a request matcher provided by Actuator. It resolves the real endpoint paths at runtime, including a custom base path or management port, so you never hard-code /actuator/....
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
public class SecurityConfig {
@Bean
@Order(1)
SecurityFilterChain actuatorSecurity(HttpSecurity http) throws Exception {
http.securityMatcher(EndpointRequest.toAnyEndpoint())
.authorizeHttpRequests(auth -> auth
.requestMatchers(EndpointRequest.to("health", "info")).permitAll()
.anyRequest().hasRole("ACTUATOR"))
.httpBasic(Customizer.withDefaults())
.csrf(csrf -> csrf.disable());
return http.build();
}
@Bean
@Order(2)
SecurityFilterChain appSecurity(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(auth -> auth
.requestMatchers("/public/**").permitAll()
.anyRequest().authenticated())
.formLogin(Customizer.withDefaults());
return http.build();
}
}Points to notice:
securityMatcher(EndpointRequest.toAnyEndpoint())limits this chain to Actuator URLs, including the/actuatordiscovery page.EndpointRequest.to("health", "info")names endpoints by ID, not by path.hasRole("ACTUATOR")checks for the authorityROLE_ACTUATOR. Do not write the prefix yourself.- The second chain has no matcher, so it matches every request. Spring Security evaluates chains in order and uses the first one that matches. The
@Ordervalues keep the catch-all chain last. If the catch-all chain came first, the actuator chain would never run, and Spring Security rejects such a configuration at startup.
The tutorial's basic example omits the second chain and the order. That works only if the application has no other web endpoints to protect. Real applications need both.
Credentials
For a quick setup, Boot can create one in-memory user from properties.
spring:
security:
user:
name: ops
password: ${ACTUATOR_PASSWORD}
roles: ACTUATORThe password comes from an environment variable, so no secret is committed. If the variable is missing, startup fails, which is the behavior you want.
This single shared account is acceptable for a scrape job or a small internal service. For anything larger, define a UserDetailsService, or use your existing identity provider (OAuth 2 resource server, LDAP) and map a group to the ACTUATOR role. Give automated clients their own account so you can rotate or revoke each one separately.
Call a secured endpoint like this:
curl -u ops:"$ACTUATOR_PASSWORD" http://localhost:8080/actuator/metricsHTTP Basic sends credentials in an easily decoded header. Use it only over TLS, or inside a trusted network where TLS terminates at the edge.
CSRF and Write Operations
Spring Security enables CSRF protection by default, and it applies to POST, PUT, PATCH, and DELETE. Actuator has write operations: changing a log level through /actuator/loggers/{name} is a POST. With CSRF on and a Basic-auth client such as curl, the call fails with 403 even when the credentials and role are correct.
The chain above disables CSRF for Actuator requests only:
curl -u ops:"$ACTUATOR_PASSWORD" \
-X POST -H "Content-Type: application/json" \
-d '{"configuredLevel":"DEBUG"}' \
http://localhost:8080/actuator/loggers/com.example.ordersHealth Details and Roles
Even when health is open, you can hide its details from anonymous callers. The setting management.endpoint.health.show-details: when-authorized shows component details only to authenticated users. To require a specific role, add:
management:
endpoint:
health:
show-details: when-authorized
roles: ACTUATORAnonymous callers then see only {"status":"UP"}. Component names, hostnames, and disk figures stay hidden. That is enough for a load balancer check and reveals nothing useful to an attacker.
Sensitive Values in Output
Some endpoints return configuration. In Spring Boot 3, env and configprops mask all values by default (******). You can change this with management.endpoint.env.show-values and management.endpoint.configprops.show-values. The values are NEVER, ALWAYS, and WHEN_AUTHORIZED.
Prefer WHEN_AUTHORIZED (with the role set on the endpoint) over ALWAYS. Masking is a second line of defense. It does not replace authorization, because key names, property sources, and bean names still reveal a lot about your system.
Letting Prometheus Authenticate
If prometheus requires a login, give the scraper credentials. Prometheus supports Basic auth in its scrape configuration:
scrape_configs:
- job_name: orders-service
metrics_path: /actuator/prometheus
basic_auth:
username: ops
password_file: /etc/prometheus/secrets/actuator-password
static_configs:
- targets: ["orders-service:8080"]Keeping the password in a file avoids placing it in the configuration itself. A common alternative is to leave /actuator/prometheus unauthenticated but reachable only on a private management port. Choose deliberately: network isolation is simpler to operate, while authentication still protects you if the network boundary is misconfigured.
Defense in Depth
Combine several measures instead of relying on one.
- Expose the minimum set of endpoints.
- Require a role for everything except
healthandinfo. - Move Actuator to a separate management port so the public load balancer never routes to it (covered in the next chapters).
- Do not expose
heapdump,env, orthreaddumpover HTTP in production unless you need them. A heap dump can contain passwords and tokens held in memory.
Common Errors
| Symptom | Likely cause | Fix |
|---|---|---|
| All Actuator endpoints return 200 with no login | A custom `SecurityFilterChain` exists and does not match Actuator, or Security is not on the classpath | Add an Actuator chain with `EndpointRequest` |
| 403 on POST to `/actuator/loggers/...` with valid credentials | CSRF protection is on | Disable CSRF for the Actuator chain or send a CSRF token |
| 403 for a user who logged in successfully | The user lacks `ROLE_ACTUATOR` | Add the `ACTUATOR` role to the user |
| 404 on an endpoint you secured | The endpoint is not in the exposure list | Add its ID to `management.endpoints.web.exposure.include` |
| Startup error about a chain that matches any request | Catch-all chain placed before another chain | Set `@Order` so the catch-all chain is last |
| Application endpoints became public after adding an Actuator chain | The default chain backed off and no chain covers the app | Add an application chain as shown above |
Exercises
Exercise
You add a SecurityFilterChain bean that applies only to Actuator endpoints. Afterward, a colleague finds that your application's own /orders URLs no longer ask for a login. Why did this happen, and how do you fix it?
Show answer
Defining any SecurityFilterChain bean makes Boot's default chain back off. The new chain matches only Actuator URLs, so nothing protects /orders. Add a second SecurityFilterChain that covers the application's requests (for example, anyRequest().authenticated()), and use @Order so the Actuator chain comes first and the catch-all chain comes last.
Exercise
A script calls POST /actuator/loggers/com.example.orders with valid Basic credentials for a user with the ACTUATOR role, but receives 403. GET requests from the same script succeed. What is the likely cause, and what are two ways to resolve it?
Show answer
CSRF protection is rejecting the POST because the request carries no CSRF token. Either disable CSRF on the Actuator filter chain (reasonable for Basic-auth, non-browser clients), or have the client obtain and send a valid CSRF token with each state-changing request.
Exercise
You want load balancers to read /actuator/health without logging in, but you do not want anonymous callers to see database or disk details. Which settings achieve this?
Show answer
Permit the health endpoint in the Actuator chain with EndpointRequest.to("health"), and set management.endpoint.health.show-details to when-authorized. Optionally set management.endpoint.health.roles to ACTUATOR so only that role sees the details. Anonymous callers then receive only the overall status.