@ -25,10 +25,8 @@ import org.mockito.junit.jupiter.MockitoExtension;
@@ -25,10 +25,8 @@ import org.mockito.junit.jupiter.MockitoExtension;
import reactor.core.publisher.Mono ;
import org.springframework.security.authentication.BadCredentialsException ;
import org.springframework.security.authentication.NonBuildableAuthenticationToken ;
import org.springframework.security.authentication.ReactiveAuthenticationManager ;
import org.springframework.security.authentication.ReactiveAuthenticationManagerResolver ;
import org.springframework.security.authentication.SecurityAssertions ;
import org.springframework.security.authentication.TestingAuthenticationToken ;
import org.springframework.security.core.Authentication ;
import org.springframework.security.core.GrantedAuthority ;
@ -178,31 +176,6 @@ public class AuthenticationWebFilterTests {
@@ -178,31 +176,6 @@ public class AuthenticationWebFilterTests {
assertThat ( result . getResponseCookies ( ) ) . isEmpty ( ) ;
}
@Test
public void filterWhenAuthenticatedThenCombinesAuthorities ( ) {
String ROLE_EXISTING = "ROLE_EXISTING" ;
TestingAuthenticationToken existingAuthn = new TestingAuthenticationToken ( "username" , "password" ,
ROLE_EXISTING ) ;
given ( this . authenticationManager . authenticate ( any ( ) ) )
. willReturn ( Mono . just ( new TestingAuthenticationToken ( "user" , "password" , "TEST" ) ) ) ;
given ( this . securityContextRepository . save ( any ( ) , any ( ) ) ) . willReturn ( Mono . empty ( ) ) ;
this . filter = new AuthenticationWebFilter ( this . authenticationManager ) ;
this . filter . setSecurityContextRepository ( this . securityContextRepository ) ;
WebTestClient client = WebTestClientBuilder . bindToWebFilters ( new RunAsWebFilter ( existingAuthn ) , this . filter )
. build ( ) ;
client . get ( )
. uri ( "/" )
. headers ( ( headers ) - > headers . setBasicAuth ( "test" , "this" ) )
. exchange ( )
. expectStatus ( )
. isOk ( ) ;
ArgumentCaptor < SecurityContext > context = ArgumentCaptor . forClass ( SecurityContext . class ) ;
verify ( this . securityContextRepository ) . save ( any ( ) , context . capture ( ) ) ;
Authentication authentication = context . getValue ( ) . getAuthentication ( ) ;
assertThat ( authentication . getAuthorities ( ) ) . extracting ( GrantedAuthority : : getAuthority )
. containsExactlyInAnyOrder ( ROLE_EXISTING , "TEST" ) ;
}
/ * *
* This is critical to avoid adding duplicate GrantedAuthority instances with the
* same ' authority when the issuedAt is too old and a new instance is requested .
@ -232,31 +205,6 @@ public class AuthenticationWebFilterTests {
@@ -232,31 +205,6 @@ public class AuthenticationWebFilterTests {
. containsExactly ( DefaultEqualsGrantedAuthority . AUTHORITY ) ;
}
@Test
void doFilterWhenNotOverridingToBuilderThenDoesNotMergeAuthorities ( ) throws Exception {
TestingAuthenticationToken existingAuthn = new TestingAuthenticationToken ( "username" , "password" , "FACTORONE" ) ;
given ( this . authenticationManager . authenticate ( any ( ) ) )
. willReturn ( Mono . just ( new NonBuildableAuthenticationToken ( "user" , "password" , "FACTORTWO" ) ) ) ;
given ( this . securityContextRepository . save ( any ( ) , any ( ) ) ) . willReturn ( Mono . empty ( ) ) ;
this . filter = new AuthenticationWebFilter ( this . authenticationManager ) ;
this . filter . setSecurityContextRepository ( this . securityContextRepository ) ;
WebTestClient client = WebTestClientBuilder . bindToWebFilters ( new RunAsWebFilter ( existingAuthn ) , this . filter )
. build ( ) ;
client . get ( )
. uri ( "/" )
. headers ( ( headers ) - > headers . setBasicAuth ( "test" , "this" ) )
. exchange ( )
. expectStatus ( )
. isOk ( ) ;
ArgumentCaptor < SecurityContext > context = ArgumentCaptor . forClass ( SecurityContext . class ) ;
verify ( this . securityContextRepository ) . save ( any ( ) , context . capture ( ) ) ;
Authentication authentication = context . getValue ( ) . getAuthentication ( ) ;
SecurityAssertions . assertThat ( authentication )
. authorities ( )
. extracting ( GrantedAuthority : : getAuthority )
. containsExactly ( "FACTORTWO" ) ;
}
@Test
public void filterWhenAuthenticationManagerResolverDefaultsAndAuthenticationFailThenUnauthorized ( ) {
given ( this . authenticationManager . authenticate ( any ( ) ) )