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