24
24
import org .springframework .beans .factory .annotation .Autowired ;
25
25
import org .springframework .context .annotation .Bean ;
26
26
import org .springframework .context .annotation .Configuration ;
27
+ import org .springframework .context .annotation .Primary ;
27
28
import org .springframework .security .config .ObjectPostProcessor ;
28
29
import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
29
30
import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
34
35
import org .springframework .security .core .userdetails .User ;
35
36
import org .springframework .security .test .web .servlet .response .SecurityMockMvcResultMatchers ;
36
37
import org .springframework .security .web .SecurityFilterChain ;
38
+ import org .springframework .security .web .authentication .preauth .PreAuthenticatedAuthenticationToken ;
37
39
import org .springframework .security .web .authentication .preauth .j2ee .J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource ;
38
40
import org .springframework .security .web .authentication .preauth .j2ee .J2eePreAuthenticatedProcessingFilter ;
39
41
import org .springframework .test .web .servlet .MockMvc ;
@@ -64,18 +66,16 @@ public class JeeConfigurerTests {
64
66
65
67
@ Test
66
68
public void configureWhenRegisteringObjectPostProcessorThenInvokedOnJ2eePreAuthenticatedProcessingFilter () {
67
- ObjectPostProcessorConfig .objectPostProcessor = spy (ReflectingObjectPostProcessor .class );
68
69
this .spring .register (ObjectPostProcessorConfig .class ).autowire ();
69
- verify ( ObjectPostProcessorConfig . objectPostProcessor )
70
- .postProcess (any (J2eePreAuthenticatedProcessingFilter .class ));
70
+ ObjectPostProcessor < Object > objectPostProcessor = this . spring . getContext (). getBean ( ObjectPostProcessor . class );
71
+ verify ( objectPostProcessor ) .postProcess (any (J2eePreAuthenticatedProcessingFilter .class ));
71
72
}
72
73
73
74
@ Test
74
75
public void configureWhenRegisteringObjectPostProcessorThenInvokedOnJ2eeBasedPreAuthenticatedWebAuthenticationDetailsSource () {
75
- ObjectPostProcessorConfig .objectPostProcessor = spy (ReflectingObjectPostProcessor .class );
76
76
this .spring .register (ObjectPostProcessorConfig .class ).autowire ();
77
- verify ( ObjectPostProcessorConfig . objectPostProcessor )
78
- .postProcess (any (J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource .class ));
77
+ ObjectPostProcessor < Object > objectPostProcessor = this . spring . getContext (). getBean ( ObjectPostProcessor . class );
78
+ verify ( objectPostProcessor ) .postProcess (any (J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource .class ));
79
79
}
80
80
81
81
@ Test
@@ -135,12 +135,14 @@ public void requestWhenJeeMappableAuthoritiesInLambdaThenAuthenticatedWithMappab
135
135
public void requestWhenCustomAuthenticatedUserDetailsServiceInLambdaThenCustomAuthenticatedUserDetailsServiceUsed ()
136
136
throws Exception {
137
137
this .spring .register (JeeCustomAuthenticatedUserDetailsServiceConfig .class ).autowire ();
138
+ AuthenticationUserDetailsService <PreAuthenticatedAuthenticationToken > userDetailsService = this .spring
139
+ .getContext ()
140
+ .getBean (AuthenticationUserDetailsService .class );
138
141
Principal user = mock (Principal .class );
139
142
User userDetails = new User ("user" , "N/A" , true , true , true , true ,
140
143
AuthorityUtils .createAuthorityList ("ROLE_USER" ));
141
144
given (user .getName ()).willReturn ("user" );
142
- given (JeeCustomAuthenticatedUserDetailsServiceConfig .authenticationUserDetailsService .loadUserDetails (any ()))
143
- .willReturn (userDetails );
145
+ given (userDetailsService .loadUserDetails (any ())).willReturn (userDetails );
144
146
// @formatter:off
145
147
MockHttpServletRequestBuilder authRequest = get ("/" )
146
148
.principal (user )
@@ -157,7 +159,7 @@ public void requestWhenCustomAuthenticatedUserDetailsServiceInLambdaThenCustomAu
157
159
@ EnableWebSecurity
158
160
static class ObjectPostProcessorConfig {
159
161
160
- static ObjectPostProcessor <Object > objectPostProcessor ;
162
+ ObjectPostProcessor <Object > objectPostProcessor = spy ( ReflectingObjectPostProcessor . class ) ;
161
163
162
164
@ Bean
163
165
SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
@@ -169,8 +171,9 @@ SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
169
171
}
170
172
171
173
@ Bean
172
- static ObjectPostProcessor <Object > objectPostProcessor () {
173
- return objectPostProcessor ;
174
+ @ Primary
175
+ ObjectPostProcessor <Object > objectPostProcessor () {
176
+ return this .objectPostProcessor ;
174
177
}
175
178
176
179
}
@@ -245,7 +248,7 @@ SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
245
248
@ EnableWebSecurity
246
249
public static class JeeCustomAuthenticatedUserDetailsServiceConfig {
247
250
248
- static AuthenticationUserDetailsService authenticationUserDetailsService = mock (
251
+ private AuthenticationUserDetailsService < PreAuthenticatedAuthenticationToken > authenticationUserDetailsService = mock (
249
252
AuthenticationUserDetailsService .class );
250
253
251
254
@ Bean
@@ -256,12 +259,17 @@ SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
256
259
.anyRequest ().hasRole ("USER" )
257
260
)
258
261
.jee ((jee ) -> jee
259
- .authenticatedUserDetailsService (authenticationUserDetailsService )
262
+ .authenticatedUserDetailsService (this . authenticationUserDetailsService )
260
263
);
261
264
return http .build ();
262
265
// @formatter:on
263
266
}
264
267
268
+ @ Bean
269
+ AuthenticationUserDetailsService <PreAuthenticatedAuthenticationToken > authenticationUserDetailsService () {
270
+ return this .authenticationUserDetailsService ;
271
+ }
272
+
265
273
}
266
274
267
275
}
0 commit comments