Sub topic 10
Securing Actuator Endpoints with Spring Security
Actuator endpoints can reveal configuration values, bean wiring, and thread dumps, and some of them can change application state. This chapter shows how to protect them with Spring Security. It assumes you know how to expose endpoints (see the exposure chapter), have a basic Spring Security setup, and can read a Spring Boot YAML file.
Why Actuator needs its own rules
When Spring Security is on the classpath, Spring Boot secures all HTTP endpoints, including Actuator, with a default form-login and basic-auth setup. That default is rarely what you want in production. Load balancers and Kubernetes probes need to read health without credentials. Operators need the sensitive endpoints, such as env, loggers, and heapdump, to require a role.
The working model is simple: a dedicated SecurityFilterChain applies only to Actuator requests and decides who can reach which endpoint.
Writing the filter chain
Use EndpointRequest to match endpoints by id instead of hard-coding paths. It keeps working if you change the base path or move Actuator to another port.
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
public class ActuatorSecurityConfig {
@Bean
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());
return http.build();
}
}Three parts matter here:
securityMatcher(EndpointRequest.toAnyEndpoint())limits this chain to Actuator requests. Your application's own chain is unaffected.permitAll()forhealthandinfoleaves them open for probes and load balancers.anyRequest().hasRole("ACTUATOR")requires the role for everything else, using HTTP Basic.
Creating a user with the role
The chain needs a user that holds the role. For a small setup, define one in configuration:
spring:
security:
user:
name: ops
password: ${ACTUATOR_PASSWORD}
roles: ACTUATORRead the password from an environment variable or a secrets store. Never commit it. For real deployments, back the chain with your normal user store or an identity provider.
Common errors
| Symptom | Likely cause |
|---|---|
| 404 on an endpoint | The endpoint is not in `management.endpoints.web.exposure.include`. Security does not expose endpoints. |
| 401 on `/actuator/health` | Your chain does not match, or another chain with higher priority catches the request. |
| 403 after a correct login | The user lacks the `ACTUATOR` role. Check that you are not writing `ROLE_ACTUATOR` in `hasRole`. |
| Application pages stop being protected | A chain with no `securityMatcher` was declared first and matched all requests. |
Checking your work
Start the application and test both cases with curl:
curl -i http://localhost:8080/actuator/healthshould return 200 without credentials.curl -i http://localhost:8080/actuator/metricsshould return 401.curl -i -u ops:secret http://localhost:8080/actuator/metricsshould return 200 oncemetricsis exposed and the password matches.
Exercise
A teammate adds management.endpoints.web.exposure.include: health,loggers and the filter chain above. A request to /actuator/loggers with valid credentials for a user who has only the role USER is sent. What status code results, and why?
Show answer
403 Forbidden. The user is authenticated, so it is not a 401, but anyRequest().hasRole("ACTUATOR") denies anyone without that role. The endpoint is exposed, so it is not a 404.
Exercise
Why use EndpointRequest.to("health", "info") rather than requestMatchers("/actuator/health", "/actuator/info")?
Show answer
EndpointRequest matches by endpoint id and follows the configured base path and management port, so the rule stays correct if those settings change. Hard-coded paths break silently when they change.