|
@@ -17,6 +17,7 @@ package org.springframework.security.test.web.servlet.response;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collection;
|
|
|
+import java.util.function.Consumer;
|
|
|
|
|
|
import org.springframework.security.authentication.AuthenticationTrustResolver;
|
|
|
import org.springframework.security.authentication.AuthenticationTrustResolverImpl;
|
|
@@ -88,6 +89,7 @@ public final class SecurityMockMvcResultMatchers {
|
|
|
private Object expectedAuthenticationPrincipal;
|
|
|
private String expectedAuthenticationName;
|
|
|
private Collection<? extends GrantedAuthority> expectedGrantedAuthorities;
|
|
|
+ private Consumer<Authentication> assertAuthentication;
|
|
|
|
|
|
@Override
|
|
|
public void match(MvcResult result) throws Exception {
|
|
@@ -97,6 +99,10 @@ public final class SecurityMockMvcResultMatchers {
|
|
|
|
|
|
assertTrue("Authentication should not be null", auth != null);
|
|
|
|
|
|
+ if (this.assertAuthentication != null) {
|
|
|
+ this.assertAuthentication.accept(auth);
|
|
|
+ }
|
|
|
+
|
|
|
if (this.expectedContext != null) {
|
|
|
assertEquals(this.expectedContext + " does not equal " + context,
|
|
|
this.expectedContext, context);
|
|
@@ -140,6 +146,16 @@ public final class SecurityMockMvcResultMatchers {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Allows for any validating the authentication with arbitrary assertions
|
|
|
+ * @param assesrtAuthentication the Consumer which validates the authentication
|
|
|
+ * @return the AuthenticatedMatcher to perform additional assertions
|
|
|
+ */
|
|
|
+ public AuthenticatedMatcher withAuthentication(Consumer<Authentication> assesrtAuthentication) {
|
|
|
+ this.assertAuthentication = assesrtAuthentication;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Specifies the expected username
|
|
|
*
|