Secure Logging with ELK in Spring Boot: Don’t Leak Secrets!
Logging is crucial for monitoring and debugging Spring Boot applications, but improperly managed logs can inadvertently leak sensitive information. From API credentials to user passwords, a mishandled log entry could expose your organization to security risks and compliance violations. Integrating your Spring Boot app with the ELK stack (Elasticsearch, Logstash, Kibana) brings powerful capabilities for logging and visualization, but these systems must be configured to prioritize security.
This guide dives into secure logging practices with ELK for Spring Boot. You’ll learn how to redact sensitive fields, avoid logging critical data like passwords, configure Logback filters, and secure your ELK stack endpoints to prevent unauthorized access. By following these best practices, you can log effectively while keeping sensitive information safe.
Table of Contents
- Why Secure Logging Matters
- Redacting Sensitive Fields from Logs
- Best Practices to Avoid Logging Passwords
- Using Logback Filters for Secure Logging
- Securing ELK Stack Endpoints
- Summary
Why Secure Logging Matters

Logs are often the first place developers and operators look when troubleshooting errors, monitoring performance, or tracking activity. However, if logs include unredacted sensitive data, the risks outweigh the benefits.
Risks of Logging Sensitive Data:
- Data Leakage: Exposing sensitive data like passwords, API keys, or personally identifiable information (PII) can lead to breaches and undermine user trust.
- Compliance Violations: Regulations like GDPR, HIPAA, and PCI DSS require sensitive data to be handled securely, even in logs.
- Security Exploits: Attackers can compromise logs to retrieve credentials or escalate privileges within a system.
Secure logging works to address these issues by ensuring logs are redacted, filtered, and properly secured at every stage of the pipeline.
Redacting Sensitive Fields from Logs
Why Redacting Matters:
Logs often capture sensitive JSON payloads in REST API requests, which may include fields like password
, creditCardNumber
, or SSN
. Failing to redact these fields can expose sensitive information to anyone with log access.
Step 1. Define Sensitive Fields
Identify the fields you want to redact. Examples typically include:
password
ssn
creditCardNumber
apiKey
Step 2. Code Example for Field Redaction
You can achieve field redaction by implementing a custom RequestBodyAdviceAdapter
in Spring Boot. Here’s how:
Example Code:
@RestControllerAdvice
public class AuditLoggingAdvice extends RequestBodyAdviceAdapter {
@Override
public Object afterBodyRead(Object body, MethodParameter parameter, MediaType mediaType,
Class<? extends HttpMessageConverter<?>> converterType,
ServerHttpRequest request, ServerHttpResponse response) {
redactSensitiveFields(body);
return body;
}
private void redactSensitiveFields(Object body) {
if (body instanceof Map) {
Map<String, Object> requestPayload = (Map<String, Object>) body;
if (requestPayload.containsKey("password")) {
requestPayload.put("password", "REDACTED");
}
}
}
}
Masking Strategies:
- Replace sensitive values with placeholders, e.g.,
"password": "REDACTED"
. - Store partial values, like the last 4 digits of an SSN (
"ssn": "xxxx-12-34"
).
By redacting fields at the source, you ensure that raw logs never expose critical information at any stage.
Best Practices to Avoid Logging Passwords
Logging sensitive inputs such as passwords, PINs, or session tokens must be avoided entirely. The following best practices minimize the risk of accidental leaks:
1. Disable Logging of HTTP Request Bodies
Payloads in POST requests often carry sensitive data. Set strict logging controls to avoid saving entire request bodies.
Spring Boot Configuration:
Disable default request logging using Spring Web:
server.tomcat.accesslog.enabled=false
spring.web.log-request-details=false
2. Leverage Predefined Annotations
Use annotations like @JsonIgnore
, so critical fields are excluded automatically during serialization:
Example:
public class User {
private String username;
@JsonIgnore
private String password;
// Getters and Setters
}
This prevents sensitive fields from appearing in JSON logs.
3. Carefully Review Log Statements
Avoid the temptation to log complete objects in DEBUG
mode when troubleshooting:
logger.debug("User input: {}", userDto);
Instead, prefer targeted logging:
logger.debug("Processing input for username {}", userDto.getUsername());
By carefully crafting log statements, you strike a balance between observability and security.
Using Logback Filters for Secure Logging
Spring Boot uses Logback as its default logging implementation. You can utilize Logback filters to censor sensitive data before it reaches the logs.
Step 1. Add Dependencies
Ensure Logback or Logback JSON encoders are configured in your pom.xml
file:
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>7.3</version>
</dependency>
Step 2. Use a Regex Replace Filter
The Logback TurboFilter
can replace matching patterns with placeholder text. Here’s how to mask passwords in JSON logs:
logback-spring.xml:
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<pattern>
%replace(%message){'(\"password\":\")[^\"]+': '"password":"REDACTED"'}
</pattern>
</providers>
</encoder>
This configuration uses a regex to redact any logged password fields.
Step 3. Test Your Configuration
Ensure no sensitive data is being logged by inspecting the filtered outputs during debugging or testing stages.
Securing ELK Stack Endpoints
While securing the data within your logs is essential, protecting access to your ELK infrastructure is equally critical. Elasticsearch and Kibana need to be locked down to prevent unauthorized data access.
1. Enable TLS Encryption
Secure communication between Elasticsearch nodes and clients using HTTPS. Update your elasticsearch.yml
:
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.http.ssl.enabled: true
xpack.security.http.ssl.keystore.path: "path/to/keystore.p12"
2. Use Role-Based Access Control (RBAC)
Configure Elasticsearch users and roles to restrict access only to necessary indices:
POST /_security/role/logs_reader
{
"indices": [
{
"names": ["logs-*"],
"privileges": ["read"]
}
]
}
3. Set Up Reverse Proxies
Use tools like NGINX or Traefik to restrict public access to ELK endpoints:
location / {
allow 192.168.1.0/24;
deny all;
}
This ensures that only trusted sources within your network can reach the ELK services.
4. Regularly Audit Access
Monitor access logs for Kibana and Elasticsearch to detect unauthorized access attempts:
GET /_security/audit/
By locking down your ELK endpoints, you can ensure that sensitive logs stay protected.
Summary
Logging securely in Spring Boot with the ELK stack requires a multi-layered approach. Here’s a recap of the steps to apply:
- Redact Sensitive Fields: Strip out or mask fields like
password
andSSN
at the source during logging. - Avoid Logging Passwords Entirely: Disable unnecessary payload logging and rely on annotations to exclude sensitive fields.
- Leverage Logback Filters: Use regex or custom Logback filters to prevent sensitive data from appearing in log files.
- Secure ELK Endpoints: Implement TLS encryption, RBAC, and restricted access to Kibana and Elasticsearch.
By following these practices, you can safely leverage the ELK stack while protecting critical information in your Spring Boot applications. Start implementing secure logging today to safeguard your systems without sacrificing observability!