Add authenticated().withAuthentication(Consumer<Authentication>)
This allows arbitrary assertions of the authenticated user Fixes: gh-4996
This commit is contained in:
+16
@@ -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
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user