38
38
import static org .mockito .ArgumentMatchers .any ;
39
39
import static org .mockito .Mockito .mock ;
40
40
import static org .mockito .Mockito .verify ;
41
+ import static org .springframework .security .config .Customizer .withDefaults ;
41
42
import static org .springframework .security .test .web .servlet .request .SecurityMockMvcRequestPostProcessors .httpBasic ;
42
43
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
43
44
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .header ;
@@ -102,6 +103,36 @@ protected void configure(HttpSecurity http) throws Exception {
102
103
}
103
104
}
104
105
106
+ @ Test
107
+ public void basicAuthenticationWhenUsingDefaultsInLambdaThenMatchesNamespace () throws Exception {
108
+ this .spring .register (HttpBasicLambdaConfig .class , UserConfig .class ).autowire ();
109
+
110
+ this .mvc .perform (get ("/" ))
111
+ .andExpect (status ().isUnauthorized ());
112
+
113
+ this .mvc .perform (get ("/" )
114
+ .with (httpBasic ("user" , "invalid" )))
115
+ .andExpect (status ().isUnauthorized ())
116
+ .andExpect (header ().string (HttpHeaders .WWW_AUTHENTICATE , "Basic realm=\" Realm\" " ));
117
+
118
+ this .mvc .perform (get ("/" )
119
+ .with (httpBasic ("user" , "password" )))
120
+ .andExpect (status ().isNotFound ());
121
+ }
122
+
123
+ @ EnableWebSecurity
124
+ static class HttpBasicLambdaConfig extends WebSecurityConfigurerAdapter {
125
+ protected void configure (HttpSecurity http ) throws Exception {
126
+ // @formatter:off
127
+ http
128
+ .authorizeRequests ()
129
+ .anyRequest ().hasRole ("USER" )
130
+ .and ()
131
+ .httpBasic (withDefaults ());
132
+ // @formatter:on
133
+ }
134
+ }
135
+
105
136
/**
106
137
* http@realm equivalent
107
138
*/
@@ -127,6 +158,30 @@ protected void configure(HttpSecurity http) throws Exception {
127
158
}
128
159
}
129
160
161
+ @ Test
162
+ public void basicAuthenticationWhenUsingCustomRealmInLambdaThenMatchesNamespace () throws Exception {
163
+ this .spring .register (CustomHttpBasicLambdaConfig .class , UserConfig .class ).autowire ();
164
+
165
+ this .mvc .perform (get ("/" )
166
+ .with (httpBasic ("user" , "invalid" )))
167
+ .andExpect (status ().isUnauthorized ())
168
+ .andExpect (header ().string (HttpHeaders .WWW_AUTHENTICATE , "Basic realm=\" Custom Realm\" " ));
169
+ }
170
+
171
+ @ EnableWebSecurity
172
+ static class CustomHttpBasicLambdaConfig extends WebSecurityConfigurerAdapter {
173
+ @ Override
174
+ protected void configure (HttpSecurity http ) throws Exception {
175
+ // @formatter:off
176
+ http
177
+ .authorizeRequests ()
178
+ .anyRequest ().hasRole ("USER" )
179
+ .and ()
180
+ .httpBasic (httpBasicConfig -> httpBasicConfig .realmName ("Custom Realm" ));
181
+ // @formatter:on
182
+ }
183
+ }
184
+
130
185
/**
131
186
* http/http-basic@authentication-details-source-ref equivalent
132
187
*/
@@ -161,6 +216,40 @@ protected void configure(HttpSecurity http) throws Exception {
161
216
}
162
217
}
163
218
219
+ @ Test
220
+ public void basicAuthenticationWhenUsingAuthenticationDetailsSourceRefInLambdaThenMatchesNamespace ()
221
+ throws Exception {
222
+ this .spring .register (AuthenticationDetailsSourceHttpBasicLambdaConfig .class , UserConfig .class ).autowire ();
223
+
224
+ AuthenticationDetailsSource <HttpServletRequest , ?> source =
225
+ this .spring .getContext ().getBean (AuthenticationDetailsSource .class );
226
+
227
+ this .mvc .perform (get ("/" )
228
+ .with (httpBasic ("user" , "password" )));
229
+
230
+ verify (source ).buildDetails (any (HttpServletRequest .class ));
231
+ }
232
+
233
+ @ EnableWebSecurity
234
+ static class AuthenticationDetailsSourceHttpBasicLambdaConfig extends WebSecurityConfigurerAdapter {
235
+ AuthenticationDetailsSource <HttpServletRequest , ?> authenticationDetailsSource =
236
+ mock (AuthenticationDetailsSource .class );
237
+
238
+ @ Override
239
+ protected void configure (HttpSecurity http ) throws Exception {
240
+ // @formatter:off
241
+ http
242
+ .httpBasic (httpBasicConfig ->
243
+ httpBasicConfig .authenticationDetailsSource (this .authenticationDetailsSource ));
244
+ // @formatter:on
245
+ }
246
+
247
+ @ Bean
248
+ AuthenticationDetailsSource <HttpServletRequest , ?> authenticationDetailsSource () {
249
+ return this .authenticationDetailsSource ;
250
+ }
251
+ }
252
+
164
253
/**
165
254
* http/http-basic@entry-point-ref
166
255
*/
@@ -195,4 +284,38 @@ protected void configure(HttpSecurity http) throws Exception {
195
284
.authenticationEntryPoint (this .authenticationEntryPoint );
196
285
}
197
286
}
287
+
288
+ @ Test
289
+ public void basicAuthenticationWhenUsingEntryPointRefInLambdaThenMatchesNamespace () throws Exception {
290
+ this .spring .register (EntryPointRefHttpBasicLambdaConfig .class , UserConfig .class ).autowire ();
291
+
292
+ this .mvc .perform (get ("/" ))
293
+ .andExpect (status ().is (999 ));
294
+
295
+ this .mvc .perform (get ("/" )
296
+ .with (httpBasic ("user" , "invalid" )))
297
+ .andExpect (status ().is (999 ));
298
+
299
+ this .mvc .perform (get ("/" )
300
+ .with (httpBasic ("user" , "password" )))
301
+ .andExpect (status ().isNotFound ());
302
+ }
303
+
304
+ @ EnableWebSecurity
305
+ static class EntryPointRefHttpBasicLambdaConfig extends WebSecurityConfigurerAdapter {
306
+ AuthenticationEntryPoint authenticationEntryPoint =
307
+ (request , response , ex ) -> response .setStatus (999 );
308
+
309
+ @ Override
310
+ protected void configure (HttpSecurity http ) throws Exception {
311
+ // @formatter:off
312
+ http
313
+ .authorizeRequests ()
314
+ .anyRequest ().hasRole ("USER" )
315
+ .and ()
316
+ .httpBasic (httpBasicConfig ->
317
+ httpBasicConfig .authenticationEntryPoint (this .authenticationEntryPoint ));
318
+ // @formatter:on
319
+ }
320
+ }
198
321
}
0 commit comments