26
26
import org .springframework .expression .BeanResolver ;
27
27
import org .springframework .security .core .Authentication ;
28
28
import org .springframework .security .core .annotation .AuthenticationPrincipal ;
29
+ import org .springframework .security .core .context .ReactiveSecurityContextHolder ;
29
30
import org .springframework .security .web .method .ResolvableMethod ;
30
31
import org .springframework .web .reactive .BindingContext ;
31
32
import org .springframework .web .server .ServerWebExchange ;
@@ -82,28 +83,16 @@ public void supportsParameterCurrentUser() {
82
83
public void resolveArgumentWhenIsAuthenticationThenObtainsPrincipal () {
83
84
MethodParameter parameter = this .authenticationPrincipal .arg (String .class );
84
85
when (authentication .getPrincipal ()).thenReturn ("user" );
85
- when (exchange .getPrincipal ()).thenReturn (Mono .just (authentication ));
86
86
87
- Mono <Object > argument = resolver .resolveArgument (parameter , bindingContext , exchange );
87
+ Mono <Object > argument = resolver .resolveArgument (parameter , bindingContext , exchange )
88
+ .subscriberContext (ReactiveSecurityContextHolder .withAuthentication (authentication ));
88
89
89
90
assertThat (argument .block ()).isEqualTo (authentication .getPrincipal ());
90
91
}
91
92
92
- @ Test
93
- public void resolveArgumentWhenIsNotAuthenticationThenMonoEmpty () {
94
- MethodParameter parameter = this .authenticationPrincipal .arg (String .class );
95
- when (exchange .getPrincipal ()).thenReturn (Mono .just (() -> "" ));
96
-
97
- Mono <Object > argument = resolver .resolveArgument (parameter , bindingContext , exchange );
98
-
99
- assertThat (argument ).isNotNull ();
100
- assertThat (argument .block ()).isNull ();
101
- }
102
-
103
93
@ Test
104
94
public void resolveArgumentWhenIsEmptyThenMonoEmpty () {
105
95
MethodParameter parameter = this .authenticationPrincipal .arg (String .class );
106
- when (exchange .getPrincipal ()).thenReturn (Mono .empty ());
107
96
108
97
Mono <Object > argument = resolver .resolveArgument (parameter , bindingContext , exchange );
109
98
@@ -115,9 +104,9 @@ public void resolveArgumentWhenIsEmptyThenMonoEmpty() {
115
104
public void resolveArgumentWhenMonoIsAuthenticationThenObtainsPrincipal () {
116
105
MethodParameter parameter = this .authenticationPrincipal .arg (Mono .class , String .class );
117
106
when (authentication .getPrincipal ()).thenReturn ("user" );
118
- when (exchange .getPrincipal ()).thenReturn (Mono .just (authentication ));
119
107
120
- Mono <Object > argument = resolver .resolveArgument (parameter , bindingContext , exchange );
108
+ Mono <Object > argument = resolver .resolveArgument (parameter , bindingContext , exchange )
109
+ .subscriberContext (ReactiveSecurityContextHolder .withAuthentication (authentication ));
121
110
122
111
assertThat (argument .cast (Mono .class ).block ().block ()).isEqualTo (authentication .getPrincipal ());
123
112
}
@@ -126,9 +115,9 @@ public void resolveArgumentWhenMonoIsAuthenticationThenObtainsPrincipal() {
126
115
public void resolveArgumentWhenMonoIsAuthenticationAndNoGenericThenObtainsPrincipal () {
127
116
MethodParameter parameter = ResolvableMethod .on (getClass ()).named ("authenticationPrincipalNoGeneric" ).build ().arg (Mono .class );
128
117
when (authentication .getPrincipal ()).thenReturn ("user" );
129
- when (exchange .getPrincipal ()).thenReturn (Mono .just (authentication ));
130
118
131
- Mono <Object > argument = resolver .resolveArgument (parameter , bindingContext , exchange );
119
+ Mono <Object > argument = resolver .resolveArgument (parameter , bindingContext , exchange )
120
+ .subscriberContext (ReactiveSecurityContextHolder .withAuthentication (authentication ));
132
121
133
122
assertThat (argument .cast (Mono .class ).block ().block ()).isEqualTo (authentication .getPrincipal ());
134
123
}
@@ -138,9 +127,9 @@ public void resolveArgumentWhenSpelThenObtainsPrincipal() {
138
127
MyUser user = new MyUser (3L );
139
128
MethodParameter parameter = this .spel .arg (Long .class );
140
129
when (authentication .getPrincipal ()).thenReturn (user );
141
- when (exchange .getPrincipal ()).thenReturn (Mono .just (authentication ));
142
130
143
- Mono <Object > argument = resolver .resolveArgument (parameter , bindingContext , exchange );
131
+ Mono <Object > argument = resolver .resolveArgument (parameter , bindingContext , exchange )
132
+ .subscriberContext (ReactiveSecurityContextHolder .withAuthentication (authentication ));
144
133
145
134
assertThat (argument .block ()).isEqualTo (user .getId ());
146
135
}
@@ -150,10 +139,10 @@ public void resolveArgumentWhenBeanThenObtainsPrincipal() throws Exception {
150
139
MyUser user = new MyUser (3L );
151
140
MethodParameter parameter = this .bean .arg (Long .class );
152
141
when (authentication .getPrincipal ()).thenReturn (user );
153
- when (exchange .getPrincipal ()).thenReturn (Mono .just (authentication ));
154
142
when (this .beanResolver .resolve (any (), eq ("beanName" ))).thenReturn (new Bean ());
155
143
156
- Mono <Object > argument = resolver .resolveArgument (parameter , bindingContext , exchange );
144
+ Mono <Object > argument = resolver .resolveArgument (parameter , bindingContext , exchange )
145
+ .subscriberContext (ReactiveSecurityContextHolder .withAuthentication (authentication ));
157
146
158
147
assertThat (argument .block ()).isEqualTo (user .getId ());
159
148
}
@@ -162,9 +151,9 @@ public void resolveArgumentWhenBeanThenObtainsPrincipal() throws Exception {
162
151
public void resolveArgumentWhenMetaThenObtainsPrincipal () {
163
152
MethodParameter parameter = this .meta .arg (String .class );
164
153
when (authentication .getPrincipal ()).thenReturn ("user" );
165
- when (exchange .getPrincipal ()).thenReturn (Mono .just (authentication ));
166
154
167
- Mono <Object > argument = resolver .resolveArgument (parameter , bindingContext , exchange );
155
+ Mono <Object > argument = resolver .resolveArgument (parameter , bindingContext , exchange )
156
+ .subscriberContext (ReactiveSecurityContextHolder .withAuthentication (authentication ));
168
157
169
158
assertThat (argument .block ()).isEqualTo ("user" );
170
159
}
@@ -173,9 +162,9 @@ public void resolveArgumentWhenMetaThenObtainsPrincipal() {
173
162
public void resolveArgumentWhenErrorOnInvalidTypeImplicit () {
174
163
MethodParameter parameter = ResolvableMethod .on (getClass ()).named ("errorOnInvalidTypeWhenImplicit" ).build ().arg (Integer .class );
175
164
when (authentication .getPrincipal ()).thenReturn ("user" );
176
- when (exchange .getPrincipal ()).thenReturn (Mono .just (authentication ));
177
165
178
- Mono <Object > argument = resolver .resolveArgument (parameter , bindingContext , exchange );
166
+ Mono <Object > argument = resolver .resolveArgument (parameter , bindingContext , exchange )
167
+ .subscriberContext (ReactiveSecurityContextHolder .withAuthentication (authentication ));
179
168
180
169
assertThat (argument .block ()).isNull ();
181
170
}
@@ -184,9 +173,9 @@ public void resolveArgumentWhenErrorOnInvalidTypeImplicit() {
184
173
public void resolveArgumentWhenErrorOnInvalidTypeExplicitFalse () {
185
174
MethodParameter parameter = ResolvableMethod .on (getClass ()).named ("errorOnInvalidTypeWhenExplicitFalse" ).build ().arg (Integer .class );
186
175
when (authentication .getPrincipal ()).thenReturn ("user" );
187
- when (exchange .getPrincipal ()).thenReturn (Mono .just (authentication ));
188
176
189
- Mono <Object > argument = resolver .resolveArgument (parameter , bindingContext , exchange );
177
+ Mono <Object > argument = resolver .resolveArgument (parameter , bindingContext , exchange )
178
+ .subscriberContext (ReactiveSecurityContextHolder .withAuthentication (authentication ));
190
179
191
180
assertThat (argument .block ()).isNull ();
192
181
}
@@ -195,9 +184,9 @@ public void resolveArgumentWhenErrorOnInvalidTypeExplicitFalse() {
195
184
public void resolveArgumentWhenErrorOnInvalidTypeExplicitTrue () {
196
185
MethodParameter parameter = ResolvableMethod .on (getClass ()).named ("errorOnInvalidTypeWhenExplicitTrue" ).build ().arg (Integer .class );
197
186
when (authentication .getPrincipal ()).thenReturn ("user" );
198
- when (exchange .getPrincipal ()).thenReturn (Mono .just (authentication ));
199
187
200
- Mono <Object > argument = resolver .resolveArgument (parameter , bindingContext , exchange );
188
+ Mono <Object > argument = resolver .resolveArgument (parameter , bindingContext , exchange )
189
+ .subscriberContext (ReactiveSecurityContextHolder .withAuthentication (authentication ));
201
190
202
191
assertThatThrownBy (() -> argument .block ()).isInstanceOf (ClassCastException .class );
203
192
}
0 commit comments