parent
899f7aa8f0
commit
684be0f048
|
@ -25,6 +25,7 @@ import org.springframework.context.ApplicationEvent;
|
||||||
import org.springframework.context.ApplicationEventPublisher;
|
import org.springframework.context.ApplicationEventPublisher;
|
||||||
import org.springframework.security.authentication.BadCredentialsException;
|
import org.springframework.security.authentication.BadCredentialsException;
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
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.AuthenticationFailureExpiredEvent;
|
||||||
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
|
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
|
||||||
import org.springframework.security.authentication.event.InteractiveAuthenticationSuccessEvent;
|
import org.springframework.security.authentication.event.InteractiveAuthenticationSuccessEvent;
|
||||||
|
@ -55,12 +56,10 @@ public class AuthenticationAuditListenerTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAuthenticationSuccess() {
|
public void testAuthenticationSuccess() {
|
||||||
this.listener.onApplicationEvent(new AuthenticationSuccessEvent(
|
AuditApplicationEvent event = handleAuthenticationEvent(
|
||||||
|
new AuthenticationSuccessEvent(
|
||||||
new UsernamePasswordAuthenticationToken("user", "password")));
|
new UsernamePasswordAuthenticationToken("user", "password")));
|
||||||
ArgumentCaptor<AuditApplicationEvent> argumentCaptor = ArgumentCaptor
|
assertThat(event.getAuditEvent().getType())
|
||||||
.forClass(AuditApplicationEvent.class);
|
|
||||||
verify(this.publisher).publishEvent(argumentCaptor.capture());
|
|
||||||
assertThat(argumentCaptor.getValue().getAuditEvent().getType())
|
|
||||||
.isEqualTo(AuthenticationAuditListener.AUTHENTICATION_SUCCESS);
|
.isEqualTo(AuthenticationAuditListener.AUTHENTICATION_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,26 +73,22 @@ public class AuthenticationAuditListenerTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAuthenticationFailed() {
|
public void testAuthenticationFailed() {
|
||||||
this.listener.onApplicationEvent(new AuthenticationFailureExpiredEvent(
|
AuditApplicationEvent event = handleAuthenticationEvent(
|
||||||
|
new AuthenticationFailureExpiredEvent(
|
||||||
new UsernamePasswordAuthenticationToken("user", "password"),
|
new UsernamePasswordAuthenticationToken("user", "password"),
|
||||||
new BadCredentialsException("Bad user")));
|
new BadCredentialsException("Bad user")));
|
||||||
ArgumentCaptor<AuditApplicationEvent> argumentCaptor = ArgumentCaptor
|
assertThat(event.getAuditEvent().getType())
|
||||||
.forClass(AuditApplicationEvent.class);
|
|
||||||
verify(this.publisher).publishEvent(argumentCaptor.capture());
|
|
||||||
assertThat(argumentCaptor.getValue().getAuditEvent().getType())
|
|
||||||
.isEqualTo(AuthenticationAuditListener.AUTHENTICATION_FAILURE);
|
.isEqualTo(AuthenticationAuditListener.AUTHENTICATION_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAuthenticationSwitch() {
|
public void testAuthenticationSwitch() {
|
||||||
this.listener.onApplicationEvent(new AuthenticationSwitchUserEvent(
|
AuditApplicationEvent event = handleAuthenticationEvent(
|
||||||
|
new AuthenticationSwitchUserEvent(
|
||||||
new UsernamePasswordAuthenticationToken("user", "password"),
|
new UsernamePasswordAuthenticationToken("user", "password"),
|
||||||
new User("user", "password",
|
new User("user", "password",
|
||||||
AuthorityUtils.commaSeparatedStringToAuthorityList("USER"))));
|
AuthorityUtils.commaSeparatedStringToAuthorityList("USER"))));
|
||||||
ArgumentCaptor<AuditApplicationEvent> argumentCaptor = ArgumentCaptor
|
assertThat(event.getAuditEvent().getType())
|
||||||
.forClass(AuditApplicationEvent.class);
|
|
||||||
verify(this.publisher).publishEvent(argumentCaptor.capture());
|
|
||||||
assertThat(argumentCaptor.getValue().getAuditEvent().getType())
|
|
||||||
.isEqualTo(AuthenticationAuditListener.AUTHENTICATION_SWITCH);
|
.isEqualTo(AuthenticationAuditListener.AUTHENTICATION_SWITCH);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,16 +98,21 @@ public class AuthenticationAuditListenerTests {
|
||||||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
|
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
|
||||||
"user", "password");
|
"user", "password");
|
||||||
authentication.setDetails(details);
|
authentication.setDetails(details);
|
||||||
this.listener.onApplicationEvent(new AuthenticationFailureExpiredEvent(
|
AuditApplicationEvent event = handleAuthenticationEvent(new AuthenticationFailureExpiredEvent(
|
||||||
authentication, new BadCredentialsException("Bad user")));
|
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())
|
assertThat(event.getAuditEvent().getType())
|
||||||
.isEqualTo(AuthenticationAuditListener.AUTHENTICATION_FAILURE);
|
.isEqualTo(AuthenticationAuditListener.AUTHENTICATION_FAILURE);
|
||||||
assertThat(event.getAuditEvent().getData())
|
assertThat(event.getAuditEvent().getData())
|
||||||
.containsEntry("details", details);
|
.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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
package org.springframework.boot.actuate.security;
|
package org.springframework.boot.actuate.security;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Collections;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -27,6 +27,7 @@ import org.springframework.context.ApplicationEventPublisher;
|
||||||
import org.springframework.security.access.AccessDeniedException;
|
import org.springframework.security.access.AccessDeniedException;
|
||||||
import org.springframework.security.access.ConfigAttribute;
|
import org.springframework.security.access.ConfigAttribute;
|
||||||
import org.springframework.security.access.SecurityConfig;
|
import org.springframework.security.access.SecurityConfig;
|
||||||
|
import org.springframework.security.access.event.AbstractAuthorizationEvent;
|
||||||
import org.springframework.security.access.event.AuthenticationCredentialsNotFoundEvent;
|
import org.springframework.security.access.event.AuthenticationCredentialsNotFoundEvent;
|
||||||
import org.springframework.security.access.event.AuthorizationFailureEvent;
|
import org.springframework.security.access.event.AuthorizationFailureEvent;
|
||||||
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
|
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
|
||||||
|
@ -53,26 +54,22 @@ public class AuthorizationAuditListenerTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAuthenticationCredentialsNotFound() {
|
public void testAuthenticationCredentialsNotFound() {
|
||||||
this.listener.onApplicationEvent(new AuthenticationCredentialsNotFoundEvent(this,
|
AuditApplicationEvent event = handleAuthorizationEvent(
|
||||||
Arrays.<ConfigAttribute>asList(new SecurityConfig("USER")),
|
new AuthenticationCredentialsNotFoundEvent(this,
|
||||||
|
Collections.<ConfigAttribute>singletonList(new SecurityConfig("USER")),
|
||||||
new AuthenticationCredentialsNotFoundException("Bad user")));
|
new AuthenticationCredentialsNotFoundException("Bad user")));
|
||||||
ArgumentCaptor<AuditApplicationEvent> eventArgumentCaptor = ArgumentCaptor
|
assertThat(event.getAuditEvent().getType())
|
||||||
.forClass(AuditApplicationEvent.class);
|
|
||||||
verify(this.publisher).publishEvent(eventArgumentCaptor.capture());
|
|
||||||
assertThat(eventArgumentCaptor.getValue().getAuditEvent().getType())
|
|
||||||
.isEqualTo(AuthenticationAuditListener.AUTHENTICATION_FAILURE);
|
.isEqualTo(AuthenticationAuditListener.AUTHENTICATION_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAuthorizationFailure() {
|
public void testAuthorizationFailure() {
|
||||||
this.listener.onApplicationEvent(new AuthorizationFailureEvent(this,
|
AuditApplicationEvent event = handleAuthorizationEvent(
|
||||||
Arrays.<ConfigAttribute>asList(new SecurityConfig("USER")),
|
new AuthorizationFailureEvent(this,
|
||||||
|
Collections.<ConfigAttribute>singletonList(new SecurityConfig("USER")),
|
||||||
new UsernamePasswordAuthenticationToken("user", "password"),
|
new UsernamePasswordAuthenticationToken("user", "password"),
|
||||||
new AccessDeniedException("Bad user")));
|
new AccessDeniedException("Bad user")));
|
||||||
ArgumentCaptor<AuditApplicationEvent> eventArgumentCaptor = ArgumentCaptor
|
assertThat(event.getAuditEvent().getType())
|
||||||
.forClass(AuditApplicationEvent.class);
|
|
||||||
verify(this.publisher).publishEvent(eventArgumentCaptor.capture());
|
|
||||||
assertThat(eventArgumentCaptor.getValue().getAuditEvent().getType())
|
|
||||||
.isEqualTo(AuthorizationAuditListener.AUTHORIZATION_FAILURE);
|
.isEqualTo(AuthorizationAuditListener.AUTHORIZATION_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,16 +79,22 @@ public class AuthorizationAuditListenerTests {
|
||||||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
|
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
|
||||||
"user", "password");
|
"user", "password");
|
||||||
authentication.setDetails(details);
|
authentication.setDetails(details);
|
||||||
this.listener.onApplicationEvent(new AuthorizationFailureEvent(this,
|
AuditApplicationEvent event = handleAuthorizationEvent(
|
||||||
Arrays.<ConfigAttribute>asList(new SecurityConfig("USER")),
|
new AuthorizationFailureEvent(this,
|
||||||
|
Collections.<ConfigAttribute>singletonList(new SecurityConfig("USER")),
|
||||||
authentication, new AccessDeniedException("Bad user")));
|
authentication, new AccessDeniedException("Bad user")));
|
||||||
ArgumentCaptor<AuditApplicationEvent> eventArgumentCaptor = ArgumentCaptor
|
|
||||||
.forClass(AuditApplicationEvent.class);
|
|
||||||
verify(this.publisher).publishEvent(eventArgumentCaptor.capture());
|
|
||||||
AuditApplicationEvent event = eventArgumentCaptor.getValue();
|
|
||||||
assertThat(event.getAuditEvent().getType())
|
assertThat(event.getAuditEvent().getType())
|
||||||
.isEqualTo(AuthorizationAuditListener.AUTHORIZATION_FAILURE);
|
.isEqualTo(AuthorizationAuditListener.AUTHORIZATION_FAILURE);
|
||||||
assertThat(event.getAuditEvent().getData()).containsEntry("details", details);
|
assertThat(event.getAuditEvent().getData()).containsEntry("details", details);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private AuditApplicationEvent handleAuthorizationEvent(
|
||||||
|
AbstractAuthorizationEvent event) {
|
||||||
|
ArgumentCaptor<AuditApplicationEvent> eventCaptor = ArgumentCaptor
|
||||||
|
.forClass(AuditApplicationEvent.class);
|
||||||
|
this.listener.onApplicationEvent(event);
|
||||||
|
verify(this.publisher).publishEvent(eventCaptor.capture());
|
||||||
|
return eventCaptor.getValue();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue