1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
| @Service public class SecurityLoggingService { private final Logger securityLogger = LoggerFactory.getLogger("SECURITY"); private final Logger auditLogger = LoggerFactory.getLogger("AUDIT"); private final RedisTemplate<String, Object> redisTemplate; @Autowired public SecurityLoggingService(RedisTemplate<String, Object> redisTemplate) { this.redisTemplate = redisTemplate; } public void logAuthenticationEvent(String username, String event, String ipAddress, String userAgent, boolean success) { SecurityEvent securityEvent = SecurityEvent.builder() .eventType("AUTHENTICATION") .username(username) .event(event) .ipAddress(ipAddress) .userAgent(userAgent) .success(success) .timestamp(Instant.now()) .build(); logSecurityEvent(securityEvent); if (!success) { incrementFailedAttempts(username, ipAddress); } } public void logAuthorizationEvent(String username, String resource, String action, String ipAddress, boolean success) { SecurityEvent securityEvent = SecurityEvent.builder() .eventType("AUTHORIZATION") .username(username) .resource(resource) .action(action) .ipAddress(ipAddress) .success(success) .timestamp(Instant.now()) .build(); logSecurityEvent(securityEvent); } public void logDataAccessEvent(String username, String dataType, String operation, String recordId, String ipAddress) { AuditEvent auditEvent = AuditEvent.builder() .eventType("DATA_ACCESS") .username(username) .dataType(dataType) .operation(operation) .recordId(recordId) .ipAddress(ipAddress) .timestamp(Instant.now()) .build(); logAuditEvent(auditEvent); } public void logSecurityThreat(String threatType, String description, String ipAddress, String userAgent, String severity) { SecurityEvent securityEvent = SecurityEvent.builder() .eventType("SECURITY_THREAT") .threatType(threatType) .description(description) .ipAddress(ipAddress) .userAgent(userAgent) .severity(severity) .timestamp(Instant.now()) .build(); logSecurityEvent(securityEvent); if ("HIGH".equals(severity) || "CRITICAL".equals(severity)) { sendSecurityAlert(securityEvent); } } public void logConfigurationChange(String username, String configType, String oldValue, String newValue, String ipAddress) { AuditEvent auditEvent = AuditEvent.builder() .eventType("CONFIGURATION_CHANGE") .username(username) .configType(configType) .oldValue(oldValue) .newValue(newValue) .ipAddress(ipAddress) .timestamp(Instant.now()) .build(); logAuditEvent(auditEvent); } public void logFileOperation(String username, String operation, String filePath, String ipAddress, boolean success) { AuditEvent auditEvent = AuditEvent.builder() .eventType("FILE_OPERATION") .username(username) .operation(operation) .filePath(filePath) .ipAddress(ipAddress) .success(success) .timestamp(Instant.now()) .build(); logAuditEvent(auditEvent); } public void logApiCall(String username, String endpoint, String method, String ipAddress, int statusCode, long responseTime) { AuditEvent auditEvent = AuditEvent.builder() .eventType("API_CALL") .username(username) .endpoint(endpoint) .method(method) .ipAddress(ipAddress) .statusCode(statusCode) .responseTime(responseTime) .timestamp(Instant.now()) .build(); logAuditEvent(auditEvent); if (statusCode >= 400) { logSecurityEvent(SecurityEvent.builder() .eventType("API_ERROR") .username(username) .endpoint(endpoint) .statusCode(statusCode) .ipAddress(ipAddress) .timestamp(Instant.now()) .build()); } } private void logSecurityEvent(SecurityEvent event) { try { String logMessage = formatSecurityEvent(event); securityLogger.info(logMessage); String key = "security:events:" + event.getEventType(); redisTemplate.opsForList().leftPush(key, event); redisTemplate.expire(key, Duration.ofDays(7)); } catch (Exception e) { securityLogger.error("Failed to log security event", e); } } private void logAuditEvent(AuditEvent event) { try { String logMessage = formatAuditEvent(event); auditLogger.info(logMessage); String key = "audit:events:" + event.getEventType(); redisTemplate.opsForList().leftPush(key, event); redisTemplate.expire(key, Duration.ofDays(30)); } catch (Exception e) { auditLogger.error("Failed to log audit event", e); } } private String formatSecurityEvent(SecurityEvent event) { return String.format( "SECURITY_EVENT | Type: %s | User: %s | IP: %s | Success: %s | Time: %s | Details: %s", event.getEventType(), event.getUsername(), event.getIpAddress(), event.isSuccess(), event.getTimestamp(), event.getDetails() ); } private String formatAuditEvent(AuditEvent event) { return String.format( "AUDIT_EVENT | Type: %s | User: %s | IP: %s | Time: %s | Details: %s", event.getEventType(), event.getUsername(), event.getIpAddress(), event.getTimestamp(), event.getDetails() ); } private void incrementFailedAttempts(String username, String ipAddress) { String userKey = "failed_attempts:user:" + username; String ipKey = "failed_attempts:ip:" + ipAddress; redisTemplate.opsForValue().increment(userKey); redisTemplate.opsForValue().increment(ipKey); redisTemplate.expire(userKey, Duration.ofMinutes(15)); redisTemplate.expire(ipKey, Duration.ofMinutes(15)); checkAndLockAccount(username, ipAddress); } private void checkAndLockAccount(String username, String ipAddress) { Integer userAttempts = (Integer) redisTemplate.opsForValue().get("failed_attempts:user:" + username); Integer ipAttempts = (Integer) redisTemplate.opsForValue().get("failed_attempts:ip:" + ipAddress); if (userAttempts != null && userAttempts >= 5) { lockAccount(username, "Too many failed login attempts"); } if (ipAttempts != null && ipAttempts >= 10) { blockIpAddress(ipAddress, "Too many failed login attempts from IP"); } } private void lockAccount(String username, String reason) { redisTemplate.opsForValue().set("locked:user:" + username, reason, Duration.ofMinutes(30)); logSecurityEvent(SecurityEvent.builder() .eventType("ACCOUNT_LOCKED") .username(username) .description(reason) .severity("HIGH") .timestamp(Instant.now()) .build()); } private void blockIpAddress(String ipAddress, String reason) { redisTemplate.opsForValue().set("blocked:ip:" + ipAddress, reason, Duration.ofHours(1)); logSecurityEvent(SecurityEvent.builder() .eventType("IP_BLOCKED") .ipAddress(ipAddress) .description(reason) .severity("HIGH") .timestamp(Instant.now()) .build()); } private void sendSecurityAlert(SecurityEvent event) { securityLogger.warn("SECURITY ALERT: {}", formatSecurityEvent(event)); } public SecurityStatistics getSecurityStatistics(LocalDateTime startTime, LocalDateTime endTime) { return SecurityStatistics.builder() .totalEvents(0L) .failedLogins(0L) .blockedIps(0L) .securityThreats(0L) .build(); } }
@Data @Builder class SecurityEvent { private String eventType; private String username; private String ipAddress; private String userAgent; private String event; private String resource; private String action; private String threatType; private String description; private String severity; private String endpoint; private Integer statusCode; private boolean success; private Instant timestamp; public String getDetails() { Map<String, Object> details = new HashMap<>(); if (event != null) details.put("event", event); if (resource != null) details.put("resource", resource); if (action != null) details.put("action", action); if (threatType != null) details.put("threatType", threatType); if (description != null) details.put("description", description); if (severity != null) details.put("severity", severity); if (endpoint != null) details.put("endpoint", endpoint); if (statusCode != null) details.put("statusCode", statusCode); if (userAgent != null) details.put("userAgent", userAgent); return details.toString(); } }
@Data @Builder class AuditEvent { private String eventType; private String username; private String ipAddress; private String dataType; private String operation; private String recordId; private String configType; private String oldValue; private String newValue; private String filePath; private String endpoint; private String method; private Integer statusCode; private Long responseTime; private boolean success; private Instant timestamp; public String getDetails() { Map<String, Object> details = new HashMap<>(); if (dataType != null) details.put("dataType", dataType); if (operation != null) details.put("operation", operation); if (recordId != null) details.put("recordId", recordId); if (configType != null) details.put("configType", configType); if (oldValue != null) details.put("oldValue", oldValue); if (newValue != null) details.put("newValue", newValue); if (filePath != null) details.put("filePath", filePath); if (endpoint != null) details.put("endpoint", endpoint); if (method != null) details.put("method", method); if (statusCode != null) details.put("statusCode", statusCode); if (responseTime != null) details.put("responseTime", responseTime); return details.toString(); } }
@Data @Builder class SecurityStatistics { private Long totalEvents; private Long failedLogins; private Long blockedIps; private Long securityThreats; }
|