Class SecurityAuditLog
SecurityAuditLog provides a centralized facility for recording security events such as authentication, authorization, session lifecycle, and rate limiting. Each event is logged with structured context including timestamp, session ID, user ID, IP address, and event-specific details.
The logger uses the JDK System.Logger SPI, so output can be directed to
any logging backend (SLF4J, Log4j2, java.util.logging, etc.) by configuring a
System.LoggerFinder.
Usage:
// Log a successful authentication
SecurityAuditLog.log(SecurityEventType.AUTH_SUCCESS, "user123", "Login via form");
// Log an access denial
SecurityAuditLog.log(SecurityEventType.ACCESS_DENIED, "user456", "Missing ADMIN role for /admin/users");
// Log a custom security event
SecurityAuditLog.log(SecurityEventType.CUSTOM, "user789", "Exported sensitive data: report-2024.csv");
- Since:
- 2.1
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanReturns whether security audit logging is enabled.static voidlog(SecurityEventType eventType, String detail) Logs a security event without a user ID.static voidlog(SecurityEventType eventType, String userId, String detail) Logs a security event with the specified details.static voidsetEnabled(boolean enabled) Enables or disables security audit logging.
-
Method Details
-
setEnabled
public static void setEnabled(boolean enabled) Enables or disables security audit logging.When disabled (the default), calls to
log()are no-ops.- Parameters:
enabled-trueto enable audit logging
-
isEnabled
public static boolean isEnabled()Returns whether security audit logging is enabled.- Returns:
trueif enabled
-
log
Logs a security event with the specified details.The log entry includes the event type, user ID, session ID, IP address (if available from the current session), and the detail message.
- Parameters:
eventType- the type of security eventuserId- the user identifier (may benullfor unauthenticated events)detail- a description of the event
-
log
Logs a security event without a user ID.Convenience method for events where the user is not yet identified (e.g., failed authentication).
- Parameters:
eventType- the type of security eventdetail- a description of the event
-