20
20
import java .util .List ;
21
21
import java .util .Map ;
22
22
import java .util .concurrent .ConcurrentHashMap ;
23
+ import java .util .function .Consumer ;
23
24
import java .util .function .Function ;
24
25
25
26
import org .junit .jupiter .api .Test ;
40
41
import org .springframework .security .access .prepost .PostFilter ;
41
42
import org .springframework .security .access .prepost .PreAuthorize ;
42
43
import org .springframework .security .access .prepost .PreFilter ;
43
- import org .springframework .security .authentication .TestingAuthenticationToken ;
44
+ import org .springframework .security .authentication .TestAuthentication ;
44
45
import org .springframework .security .authorization .method .AuthorizeReturnObject ;
45
46
import org .springframework .security .config .core .GrantedAuthorityDefaults ;
46
47
import org .springframework .security .config .test .SpringTestContext ;
47
48
import org .springframework .security .config .test .SpringTestContextExtension ;
49
+ import org .springframework .security .core .Authentication ;
48
50
import org .springframework .security .core .context .ReactiveSecurityContextHolder ;
51
+ import org .springframework .security .core .userdetails .User ;
49
52
50
53
import static org .assertj .core .api .Assertions .assertThat ;
51
54
@@ -63,8 +66,7 @@ public class ReactiveMethodSecurityConfigurationTests {
63
66
@ Test
64
67
public void rolePrefixWithGrantedAuthorityDefaults () throws NoSuchMethodException {
65
68
this .spring .register (WithRolePrefixConfiguration .class ).autowire ();
66
- TestingAuthenticationToken authentication = new TestingAuthenticationToken ("principal" , "credential" ,
67
- "CUSTOM_ABC" );
69
+ Authentication authentication = TestAuthentication .authenticatedUser (authorities ("CUSTOM_ABC" ));
68
70
MockMethodInvocation methodInvocation = new MockMethodInvocation (new Foo (), Foo .class , "bar" , String .class );
69
71
EvaluationContext context = this .methodSecurityExpressionHandler .createEvaluationContext (authentication ,
70
72
methodInvocation );
@@ -78,8 +80,7 @@ public void rolePrefixWithGrantedAuthorityDefaults() throws NoSuchMethodExceptio
78
80
@ Test
79
81
public void rolePrefixWithDefaultConfig () throws NoSuchMethodException {
80
82
this .spring .register (ReactiveMethodSecurityConfiguration .class ).autowire ();
81
- TestingAuthenticationToken authentication = new TestingAuthenticationToken ("principal" , "credential" ,
82
- "ROLE_ABC" );
83
+ Authentication authentication = TestAuthentication .authenticatedUser (authorities ("ROLE_ABC" ));
83
84
MockMethodInvocation methodInvocation = new MockMethodInvocation (new Foo (), Foo .class , "bar" , String .class );
84
85
EvaluationContext context = this .methodSecurityExpressionHandler .createEvaluationContext (authentication ,
85
86
methodInvocation );
@@ -91,8 +92,7 @@ public void rolePrefixWithDefaultConfig() throws NoSuchMethodException {
91
92
@ Test
92
93
public void rolePrefixWithGrantedAuthorityDefaultsAndSubclassWithProxyingEnabled () throws NoSuchMethodException {
93
94
this .spring .register (SubclassConfig .class ).autowire ();
94
- TestingAuthenticationToken authentication = new TestingAuthenticationToken ("principal" , "credential" ,
95
- "ROLE_ABC" );
95
+ Authentication authentication = TestAuthentication .authenticatedUser (authorities ("ROLE_ABC" ));
96
96
MockMethodInvocation methodInvocation = new MockMethodInvocation (new Foo (), Foo .class , "bar" , String .class );
97
97
EvaluationContext context = this .methodSecurityExpressionHandler .createEvaluationContext (authentication ,
98
98
methodInvocation );
@@ -105,7 +105,7 @@ public void rolePrefixWithGrantedAuthorityDefaultsAndSubclassWithProxyingEnabled
105
105
public void findByIdWhenAuthorizedResultThenAuthorizes () {
106
106
this .spring .register (AuthorizeResultConfig .class ).autowire ();
107
107
FlightRepository flights = this .spring .getContext ().getBean (FlightRepository .class );
108
- TestingAuthenticationToken pilot = new TestingAuthenticationToken ( "user" , "pass" , " airplane:read" );
108
+ Authentication pilot = TestAuthentication . authenticatedUser ( authorities ( " airplane:read") );
109
109
StepVerifier
110
110
.create (flights .findById ("1" )
111
111
.flatMap (Flight ::getAltitude )
@@ -124,7 +124,7 @@ public void findByIdWhenAuthorizedResultThenAuthorizes() {
124
124
public void findByIdWhenUnauthorizedResultThenDenies () {
125
125
this .spring .register (AuthorizeResultConfig .class ).autowire ();
126
126
FlightRepository flights = this .spring .getContext ().getBean (FlightRepository .class );
127
- TestingAuthenticationToken pilot = new TestingAuthenticationToken ( "user" , "pass" , " seating:read" );
127
+ Authentication pilot = TestAuthentication . authenticatedUser ( authorities ( " seating:read") );
128
128
StepVerifier
129
129
.create (flights .findById ("1" )
130
130
.flatMap (Flight ::getSeats )
@@ -142,7 +142,7 @@ public void findByIdWhenUnauthorizedResultThenDenies() {
142
142
public void findAllWhenUnauthorizedResultThenDenies () {
143
143
this .spring .register (AuthorizeResultConfig .class ).autowire ();
144
144
FlightRepository flights = this .spring .getContext ().getBean (FlightRepository .class );
145
- TestingAuthenticationToken pilot = new TestingAuthenticationToken ( "user" , "pass" , " seating:read" );
145
+ Authentication pilot = TestAuthentication . authenticatedUser ( authorities ( " seating:read") );
146
146
StepVerifier
147
147
.create (flights .findAll ()
148
148
.flatMap (Flight ::getSeats )
@@ -160,7 +160,7 @@ public void findAllWhenUnauthorizedResultThenDenies() {
160
160
public void removeWhenAuthorizedResultThenRemoves () {
161
161
this .spring .register (AuthorizeResultConfig .class ).autowire ();
162
162
FlightRepository flights = this .spring .getContext ().getBean (FlightRepository .class );
163
- TestingAuthenticationToken pilot = new TestingAuthenticationToken ( "user" , "pass" , " seating:read" );
163
+ Authentication pilot = TestAuthentication . authenticatedUser ( authorities ( " seating:read") );
164
164
StepVerifier .create (flights .remove ("1" ).contextWrite (ReactiveSecurityContextHolder .withAuthentication (pilot )))
165
165
.verifyComplete ();
166
166
}
@@ -169,7 +169,7 @@ public void removeWhenAuthorizedResultThenRemoves() {
169
169
public void findAllWhenPostFilterThenFilters () {
170
170
this .spring .register (AuthorizeResultConfig .class ).autowire ();
171
171
FlightRepository flights = this .spring .getContext ().getBean (FlightRepository .class );
172
- TestingAuthenticationToken pilot = new TestingAuthenticationToken ( "user" , "pass" , " airplane:read" );
172
+ Authentication pilot = TestAuthentication . authenticatedUser ( authorities ( " airplane:read") );
173
173
StepVerifier
174
174
.create (flights .findAll ()
175
175
.flatMap (Flight ::getPassengers )
@@ -183,7 +183,7 @@ public void findAllWhenPostFilterThenFilters() {
183
183
public void findAllWhenPreFilterThenFilters () {
184
184
this .spring .register (AuthorizeResultConfig .class ).autowire ();
185
185
FlightRepository flights = this .spring .getContext ().getBean (FlightRepository .class );
186
- TestingAuthenticationToken pilot = new TestingAuthenticationToken ( "user" , "pass" , " airplane:read" );
186
+ Authentication pilot = TestAuthentication . authenticatedUser ( authorities ( " airplane:read") );
187
187
StepVerifier
188
188
.create (flights .findAll ()
189
189
.flatMap ((flight ) -> flight .board (Flux .just ("John Doe" , "John" )).then (Mono .just (flight )))
@@ -198,7 +198,7 @@ public void findAllWhenPreFilterThenFilters() {
198
198
public void findAllWhenNestedPreAuthorizeThenAuthorizes () {
199
199
this .spring .register (AuthorizeResultConfig .class ).autowire ();
200
200
FlightRepository flights = this .spring .getContext ().getBean (FlightRepository .class );
201
- TestingAuthenticationToken pilot = new TestingAuthenticationToken ( "user" , "pass" , " seating:read" );
201
+ Authentication pilot = TestAuthentication . authenticatedUser ( authorities ( " seating:read") );
202
202
StepVerifier
203
203
.create (flights .findAll ()
204
204
.flatMap (Flight ::getPassengers )
@@ -207,6 +207,10 @@ public void findAllWhenNestedPreAuthorizeThenAuthorizes() {
207
207
.verifyError (AccessDeniedException .class );
208
208
}
209
209
210
+ private static Consumer <User .UserBuilder > authorities (String ... authorities ) {
211
+ return (builder ) -> builder .authorities (authorities );
212
+ }
213
+
210
214
@ Configuration
211
215
@ EnableReactiveMethodSecurity // this imports ReactiveMethodSecurityConfiguration
212
216
static class WithRolePrefixConfiguration {
0 commit comments