32
32
import org .aopalliance .intercept .MethodInvocation ;
33
33
import org .junit .jupiter .api .Test ;
34
34
import org .junit .jupiter .api .extension .ExtendWith ;
35
+ import org .junit .jupiter .params .ParameterizedTest ;
36
+ import org .junit .jupiter .params .provider .ValueSource ;
35
37
36
38
import org .springframework .aop .Advisor ;
37
39
import org .springframework .aop .support .DefaultPointcutAdvisor ;
76
78
import org .springframework .security .config .test .SpringTestContextExtension ;
77
79
import org .springframework .security .config .test .SpringTestParentApplicationContextExecutionListener ;
78
80
import org .springframework .security .core .Authentication ;
81
+ import org .springframework .security .core .annotation .AnnotationTemplateExpressionDefaults ;
79
82
import org .springframework .security .core .context .SecurityContextHolderStrategy ;
80
83
import org .springframework .security .test .context .support .WithAnonymousUser ;
81
84
import org .springframework .security .test .context .support .WithMockUser ;
@@ -605,69 +608,77 @@ public void allAnnotationsWhenAdviceAfterAllOffsetThenReturnsFilteredList() {
605
608
assertThat (filtered ).containsExactly ("DoNotDrop" );
606
609
}
607
610
608
- @ Test
611
+ @ ParameterizedTest
612
+ @ ValueSource (classes = { LegacyMetaAnnotationPlaceholderConfig .class , MetaAnnotationPlaceholderConfig .class })
609
613
@ WithMockUser
610
- public void methodeWhenParameterizedPreAuthorizeMetaAnnotationThenPasses () {
611
- this .spring .register (MetaAnnotationPlaceholderConfig . class ).autowire ();
614
+ public void methodeWhenParameterizedPreAuthorizeMetaAnnotationThenPasses (Class <?> config ) {
615
+ this .spring .register (config ).autowire ();
612
616
MetaAnnotationService service = this .spring .getContext ().getBean (MetaAnnotationService .class );
613
617
assertThat (service .hasRole ("USER" )).isTrue ();
614
618
}
615
619
616
- @ Test
620
+ @ ParameterizedTest
621
+ @ ValueSource (classes = { LegacyMetaAnnotationPlaceholderConfig .class , MetaAnnotationPlaceholderConfig .class })
617
622
@ WithMockUser
618
- public void methodRoleWhenPreAuthorizeMetaAnnotationHardcodedParameterThenPasses () {
619
- this .spring .register (MetaAnnotationPlaceholderConfig . class ).autowire ();
623
+ public void methodRoleWhenPreAuthorizeMetaAnnotationHardcodedParameterThenPasses (Class <?> config ) {
624
+ this .spring .register (config ).autowire ();
620
625
MetaAnnotationService service = this .spring .getContext ().getBean (MetaAnnotationService .class );
621
626
assertThat (service .hasUserRole ()).isTrue ();
622
627
}
623
628
624
- @ Test
625
- public void methodWhenParameterizedAnnotationThenFails () {
626
- this .spring .register (MetaAnnotationPlaceholderConfig .class ).autowire ();
629
+ @ ParameterizedTest
630
+ @ ValueSource (classes = { LegacyMetaAnnotationPlaceholderConfig .class , MetaAnnotationPlaceholderConfig .class })
631
+ public void methodWhenParameterizedAnnotationThenFails (Class <?> config ) {
632
+ this .spring .register (config ).autowire ();
627
633
MetaAnnotationService service = this .spring .getContext ().getBean (MetaAnnotationService .class );
628
634
assertThatExceptionOfType (IllegalArgumentException .class )
629
635
.isThrownBy (service ::placeholdersOnlyResolvedByMetaAnnotations );
630
636
}
631
637
632
- @ Test
638
+ @ ParameterizedTest
639
+ @ ValueSource (classes = { LegacyMetaAnnotationPlaceholderConfig .class , MetaAnnotationPlaceholderConfig .class })
633
640
@ WithMockUser (authorities = "SCOPE_message:read" )
634
- public void methodWhenMultiplePlaceholdersHasAuthorityThenPasses () {
635
- this .spring .register (MetaAnnotationPlaceholderConfig . class ).autowire ();
641
+ public void methodWhenMultiplePlaceholdersHasAuthorityThenPasses (Class <?> config ) {
642
+ this .spring .register (config ).autowire ();
636
643
MetaAnnotationService service = this .spring .getContext ().getBean (MetaAnnotationService .class );
637
644
assertThat (service .readMessage ()).isEqualTo ("message" );
638
645
}
639
646
640
- @ Test
647
+ @ ParameterizedTest
648
+ @ ValueSource (classes = { LegacyMetaAnnotationPlaceholderConfig .class , MetaAnnotationPlaceholderConfig .class })
641
649
@ WithMockUser (roles = "ADMIN" )
642
- public void methodWhenMultiplePlaceholdersHasRoleThenPasses () {
643
- this .spring .register (MetaAnnotationPlaceholderConfig . class ).autowire ();
650
+ public void methodWhenMultiplePlaceholdersHasRoleThenPasses (Class <?> config ) {
651
+ this .spring .register (config ).autowire ();
644
652
MetaAnnotationService service = this .spring .getContext ().getBean (MetaAnnotationService .class );
645
653
assertThat (service .readMessage ()).isEqualTo ("message" );
646
654
}
647
655
648
- @ Test
656
+ @ ParameterizedTest
657
+ @ ValueSource (classes = { LegacyMetaAnnotationPlaceholderConfig .class , MetaAnnotationPlaceholderConfig .class })
649
658
@ WithMockUser
650
- public void methodWhenPostAuthorizeMetaAnnotationThenAuthorizes () {
651
- this .spring .register (MetaAnnotationPlaceholderConfig . class ).autowire ();
659
+ public void methodWhenPostAuthorizeMetaAnnotationThenAuthorizes (Class <?> config ) {
660
+ this .spring .register (config ).autowire ();
652
661
MetaAnnotationService service = this .spring .getContext ().getBean (MetaAnnotationService .class );
653
662
service .startsWithDave ("daveMatthews" );
654
663
assertThatExceptionOfType (AccessDeniedException .class )
655
664
.isThrownBy (() -> service .startsWithDave ("jenniferHarper" ));
656
665
}
657
666
658
- @ Test
667
+ @ ParameterizedTest
668
+ @ ValueSource (classes = { LegacyMetaAnnotationPlaceholderConfig .class , MetaAnnotationPlaceholderConfig .class })
659
669
@ WithMockUser
660
- public void methodWhenPreFilterMetaAnnotationThenFilters () {
661
- this .spring .register (MetaAnnotationPlaceholderConfig . class ).autowire ();
670
+ public void methodWhenPreFilterMetaAnnotationThenFilters (Class <?> config ) {
671
+ this .spring .register (config ).autowire ();
662
672
MetaAnnotationService service = this .spring .getContext ().getBean (MetaAnnotationService .class );
663
673
assertThat (service .parametersContainDave (new ArrayList <>(List .of ("dave" , "carla" , "vanessa" , "paul" ))))
664
674
.containsExactly ("dave" );
665
675
}
666
676
667
- @ Test
677
+ @ ParameterizedTest
678
+ @ ValueSource (classes = { LegacyMetaAnnotationPlaceholderConfig .class , MetaAnnotationPlaceholderConfig .class })
668
679
@ WithMockUser
669
- public void methodWhenPostFilterMetaAnnotationThenFilters () {
670
- this .spring .register (MetaAnnotationPlaceholderConfig . class ).autowire ();
680
+ public void methodWhenPostFilterMetaAnnotationThenFilters (Class <?> config ) {
681
+ this .spring .register (config ).autowire ();
671
682
MetaAnnotationService service = this .spring .getContext ().getBean (MetaAnnotationService .class );
672
683
assertThat (service .resultsContainDave (new ArrayList <>(List .of ("dave" , "carla" , "vanessa" , "paul" ))))
673
684
.containsExactly ("dave" );
@@ -825,7 +836,7 @@ void preAuthorizeWhenDeniedAndHandlerWithCustomAnnotationInClassThenHandlerCanUs
825
836
@ WithMockUser
826
837
void postAuthorizeWhenNullDeniedMetaAnnotationThanWorks () {
827
838
this .spring
828
- .register (MethodSecurityServiceEnabledConfig .class , MetaAnnotationPlaceholderConfig .class ,
839
+ .register (MethodSecurityServiceEnabledConfig .class , LegacyMetaAnnotationPlaceholderConfig .class ,
829
840
MethodSecurityService .NullPostProcessor .class )
830
841
.autowire ();
831
842
MethodSecurityService service = this .spring .getContext ().getBean (MethodSecurityService .class );
@@ -1259,7 +1270,7 @@ Authz authz() {
1259
1270
1260
1271
@ Configuration
1261
1272
@ EnableMethodSecurity
1262
- static class MetaAnnotationPlaceholderConfig {
1273
+ static class LegacyMetaAnnotationPlaceholderConfig {
1263
1274
1264
1275
@ Bean
1265
1276
PrePostTemplateDefaults methodSecurityDefaults () {
@@ -1273,6 +1284,22 @@ MetaAnnotationService metaAnnotationService() {
1273
1284
1274
1285
}
1275
1286
1287
+ @ Configuration
1288
+ @ EnableMethodSecurity
1289
+ static class MetaAnnotationPlaceholderConfig {
1290
+
1291
+ @ Bean
1292
+ AnnotationTemplateExpressionDefaults methodSecurityDefaults () {
1293
+ return new AnnotationTemplateExpressionDefaults ();
1294
+ }
1295
+
1296
+ @ Bean
1297
+ MetaAnnotationService metaAnnotationService () {
1298
+ return new MetaAnnotationService ();
1299
+ }
1300
+
1301
+ }
1302
+
1276
1303
static class MetaAnnotationService {
1277
1304
1278
1305
@ RequireRole (role = "#role" )
0 commit comments