Skip to main content
the auth layer

Audit Logging for Auth Events: What to Log, What Not to Log, and Why the Difference Matters

2 min read Chapter 40 of 45

Audit Logging for Auth Events

Every authentication event is evidence. A successful login is evidence of access. A failed login is evidence of an attempt. A token refresh is evidence of continued activity. A password change is evidence of credential modification. A permission escalation is evidence of authorization change.

Without audit logging, incident response is guesswork. “Was the account compromised?” becomes unanswerable. “When did the attacker first access the system?” has no data to answer it. “What did they access?” cannot be determined.

What to Log

EventWhy it matters
Login successAccess timeline, session start
Login failureAttack detection, account compromise indicator
Token issuedWhat client, what scopes, what TTL
Token refreshedSession continuation, scope changes
Token revokedVoluntary logout vs forced revocation
Password changedSelf-service vs admin-initiated
MFA enrolled/removedSecurity posture change
Permission changedAuthorization scope modification
Account locked/unlockedAttack indicator or false positive

What NOT to Log

Credentials. Passwords (even hashed). Tokens (even partially). Session IDs that could be replayed. PII beyond what is needed for identification (username or user ID, not email address in every log line).

Log Structure

Auth logs must be structured (JSON), not free-text. Free-text logs are unparseable at scale. A structured log entry:

{
  "timestamp": "2024-07-15T14:23:01.442Z",
  "event_type": "AUTH_LOGIN_SUCCESS",
  "user_id": "usr_abc123",
  "tenant_id": "acme-corp",
  "client_id": "frontend-shell",
  "ip_address": "203.0.113.42",
  "user_agent": "Mozilla/5.0...",
  "session_id_hash": "sha256:a1b2c3...",
  "mfa_used": true,
  "grant_type": "authorization_code"
}

What This Chapter Covers

Section 1: Implementing secure auth event logging in Spring Security. Using Spring Security’s AuthenticationEventPublisher, structured logging with SLF4J and MDC, and ensuring auth events are never logged at DEBUG level (where they can be silently disabled in production).

Section 2: Incident detection from auth patterns. Building alerts from log data: credential stuffing signatures, impossible travel, session anomalies, and privilege escalation sequences.