diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEvent.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEvent.java index 84501c61cc3..6a9eb3b4e5c 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEvent.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEvent.java @@ -85,10 +85,9 @@ public class AuditEvent implements Serializable { public AuditEvent(Date timestamp, String principal, String type, Map data) { Assert.notNull(timestamp, "Timestamp must not be null"); - Assert.notNull(principal, "Principal must not be null"); Assert.notNull(type, "Type must not be null"); this.timestamp = timestamp; - this.principal = principal; + this.principal = principal != null ? principal : ""; this.type = type; this.data = Collections.unmodifiableMap(data); } @@ -117,7 +116,8 @@ public class AuditEvent implements Serializable { } /** - * Returns the user principal responsible for the event. + * Returns the user principal responsible for the event or an empty String if the + * principal is not available. * @return the principal */ public String getPrincipal() { diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/audit/AuditEventTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/audit/AuditEventTests.java index cbe594722f4..b13e0bb4707 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/audit/AuditEventTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/audit/AuditEventTests.java @@ -55,6 +55,13 @@ public class AuditEventTests { assertThat(event.getData().get("c")).isEqualTo("d"); } + @Test + public void nullPrincipalIsMappedToEmptyString() { + AuditEvent auditEvent = new AuditEvent(null, "UNKNOWN", + Collections.singletonMap("a", (Object) "b")); + assertThat(auditEvent.getPrincipal()).isEmpty(); + } + @Test public void nullTimestamp() throws Exception { this.thrown.expect(IllegalArgumentException.class); @@ -63,13 +70,6 @@ public class AuditEventTests { Collections.singletonMap("a", (Object) "b")); } - @Test - public void nullPrincipal() throws Exception { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Principal must not be null"); - new AuditEvent(null, "UNKNOWN", Collections.singletonMap("a", (Object) "b")); - } - @Test public void nullType() throws Exception { this.thrown.expect(IllegalArgumentException.class);