Actuator endpoints in Spring Boot provide various useful insights into the application's health, configuration, and performance. However, by default, these endpoints are accessible to anyone who has access to the application. In a production environment, it is crucial to secure these endpoints to prevent unauthorized access. Additionally, Actuator endpoints can be customized to meet specific requirements and provide more tailored information to the application administrators.
Securing Actuator endpoints can be achieved by leveraging Spring Security, which provides a comprehensive security framework for Spring applications. The following steps outline how to secure Actuator endpoints in your Spring Boot application:
pom.xml
or build.gradle
file:<!-- Maven -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
application.properties
or application.yml
file and add the following configuration:# YAML
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: when_authorized
The above configuration allows all Actuator endpoints to be accessible. You can further customize the list of exposed endpoints according to your requirements.
SecurityConfig
class that extends WebSecurityConfigurerAdapter
and override the configure(HttpSecurity http)
method:// Java
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin().and()
.httpBasic();
}
}
The above configuration allows any authenticated user to access Actuator endpoints.
application.properties
or application.yml
file and add the following configuration:# YAML
spring:
security:
user:
name: admin
password: password
roles: ACTUATOR_ADMIN
The above configuration sets the username to "admin" and the password to "password". It also assigns the "ACTUATOR_ADMIN" role to the user.
Actuator endpoints provide various insights into your application, and you can customize them to obtain more specific information or provide tailored information. Here are some ways to customize Actuator endpoints:
/info
endpoint by creating a class that implements the InfoContributor
interface and override the contribute(Info.Builder builder)
method:// Java
@Component
public class CustomInfoContributor implements InfoContributor {
@Override
public void contribute(Info.Builder builder) {
builder.withDetail("customKey", "customValue");
}
}
The above code adds a custom key-value pair to the /info
endpoint, providing additional information about your application.
/health
endpoint by creating a class that extends HealthIndicator
and override the health()
method:// Java
@Component
public class CustomHealthIndicator implements HealthIndicator {
@Override
public Health health() {
// custom health check logic here
return Health.up().build();
}
}
You can add custom health checks in the health()
method to determine the overall health of your application. The result of this check will be visible in the /health
endpoint.
application.properties
or application.yml
file and add the following configuration:# YAML
management:
endpoints:
web:
exposure:
exclude: env,beans
The above configuration excludes the /env
and /beans
endpoints from being accessible. You can customize the excluded endpoints according to your requirements.
By securing and customizing Actuator endpoints, you can ensure that your application's sensitive information is protected and present tailored insights to the application administrators. Spring Boot's Actuator provides a robust framework to monitor and manage your applications effectively.
noob to master © copyleft