|
|
|
|
@ -25,6 +25,7 @@ import org.springframework.context.ApplicationEvent;
@@ -25,6 +25,7 @@ import org.springframework.context.ApplicationEvent;
|
|
|
|
|
import org.springframework.context.ApplicationEventPublisher; |
|
|
|
|
import org.springframework.security.authentication.BadCredentialsException; |
|
|
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
|
|
|
|
import org.springframework.security.authentication.event.AbstractAuthenticationEvent; |
|
|
|
|
import org.springframework.security.authentication.event.AuthenticationFailureExpiredEvent; |
|
|
|
|
import org.springframework.security.authentication.event.AuthenticationSuccessEvent; |
|
|
|
|
import org.springframework.security.authentication.event.InteractiveAuthenticationSuccessEvent; |
|
|
|
|
@ -55,12 +56,10 @@ public class AuthenticationAuditListenerTests {
@@ -55,12 +56,10 @@ public class AuthenticationAuditListenerTests {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testAuthenticationSuccess() { |
|
|
|
|
this.listener.onApplicationEvent(new AuthenticationSuccessEvent( |
|
|
|
|
new UsernamePasswordAuthenticationToken("user", "password"))); |
|
|
|
|
ArgumentCaptor<AuditApplicationEvent> argumentCaptor = ArgumentCaptor |
|
|
|
|
.forClass(AuditApplicationEvent.class); |
|
|
|
|
verify(this.publisher).publishEvent(argumentCaptor.capture()); |
|
|
|
|
assertThat(argumentCaptor.getValue().getAuditEvent().getType()) |
|
|
|
|
AuditApplicationEvent event = handleAuthenticationEvent( |
|
|
|
|
new AuthenticationSuccessEvent( |
|
|
|
|
new UsernamePasswordAuthenticationToken("user", "password"))); |
|
|
|
|
assertThat(event.getAuditEvent().getType()) |
|
|
|
|
.isEqualTo(AuthenticationAuditListener.AUTHENTICATION_SUCCESS); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -74,26 +73,22 @@ public class AuthenticationAuditListenerTests {
@@ -74,26 +73,22 @@ public class AuthenticationAuditListenerTests {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testAuthenticationFailed() { |
|
|
|
|
this.listener.onApplicationEvent(new AuthenticationFailureExpiredEvent( |
|
|
|
|
new UsernamePasswordAuthenticationToken("user", "password"), |
|
|
|
|
new BadCredentialsException("Bad user"))); |
|
|
|
|
ArgumentCaptor<AuditApplicationEvent> argumentCaptor = ArgumentCaptor |
|
|
|
|
.forClass(AuditApplicationEvent.class); |
|
|
|
|
verify(this.publisher).publishEvent(argumentCaptor.capture()); |
|
|
|
|
assertThat(argumentCaptor.getValue().getAuditEvent().getType()) |
|
|
|
|
AuditApplicationEvent event = handleAuthenticationEvent( |
|
|
|
|
new AuthenticationFailureExpiredEvent( |
|
|
|
|
new UsernamePasswordAuthenticationToken("user", "password"), |
|
|
|
|
new BadCredentialsException("Bad user"))); |
|
|
|
|
assertThat(event.getAuditEvent().getType()) |
|
|
|
|
.isEqualTo(AuthenticationAuditListener.AUTHENTICATION_FAILURE); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testAuthenticationSwitch() { |
|
|
|
|
this.listener.onApplicationEvent(new AuthenticationSwitchUserEvent( |
|
|
|
|
new UsernamePasswordAuthenticationToken("user", "password"), |
|
|
|
|
new User("user", "password", |
|
|
|
|
AuthorityUtils.commaSeparatedStringToAuthorityList("USER")))); |
|
|
|
|
ArgumentCaptor<AuditApplicationEvent> argumentCaptor = ArgumentCaptor |
|
|
|
|
.forClass(AuditApplicationEvent.class); |
|
|
|
|
verify(this.publisher).publishEvent(argumentCaptor.capture()); |
|
|
|
|
assertThat(argumentCaptor.getValue().getAuditEvent().getType()) |
|
|
|
|
AuditApplicationEvent event = handleAuthenticationEvent( |
|
|
|
|
new AuthenticationSwitchUserEvent( |
|
|
|
|
new UsernamePasswordAuthenticationToken("user", "password"), |
|
|
|
|
new User("user", "password", |
|
|
|
|
AuthorityUtils.commaSeparatedStringToAuthorityList("USER")))); |
|
|
|
|
assertThat(event.getAuditEvent().getType()) |
|
|
|
|
.isEqualTo(AuthenticationAuditListener.AUTHENTICATION_SWITCH); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -103,16 +98,21 @@ public class AuthenticationAuditListenerTests {
@@ -103,16 +98,21 @@ public class AuthenticationAuditListenerTests {
|
|
|
|
|
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken( |
|
|
|
|
"user", "password"); |
|
|
|
|
authentication.setDetails(details); |
|
|
|
|
this.listener.onApplicationEvent(new AuthenticationFailureExpiredEvent( |
|
|
|
|
AuditApplicationEvent event = handleAuthenticationEvent(new AuthenticationFailureExpiredEvent( |
|
|
|
|
authentication, new BadCredentialsException("Bad user"))); |
|
|
|
|
ArgumentCaptor<AuditApplicationEvent> argumentCaptor = ArgumentCaptor |
|
|
|
|
.forClass(AuditApplicationEvent.class); |
|
|
|
|
verify(this.publisher).publishEvent(argumentCaptor.capture()); |
|
|
|
|
AuditApplicationEvent event = argumentCaptor.getValue(); |
|
|
|
|
assertThat(event.getAuditEvent().getType()) |
|
|
|
|
.isEqualTo(AuthenticationAuditListener.AUTHENTICATION_FAILURE); |
|
|
|
|
assertThat(event.getAuditEvent().getData()) |
|
|
|
|
.containsEntry("details", details); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private AuditApplicationEvent handleAuthenticationEvent( |
|
|
|
|
AbstractAuthenticationEvent event) { |
|
|
|
|
ArgumentCaptor<AuditApplicationEvent> eventCaptor = ArgumentCaptor |
|
|
|
|
.forClass(AuditApplicationEvent.class); |
|
|
|
|
this.listener.onApplicationEvent(event); |
|
|
|
|
verify(this.publisher).publishEvent(eventCaptor.capture()); |
|
|
|
|
return eventCaptor.getValue(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|