1616
1717package org .springframework .security .core .annotation ;
1818
19+ import java .lang .annotation .ElementType ;
20+ import java .lang .annotation .Retention ;
21+ import java .lang .annotation .RetentionPolicy ;
22+ import java .lang .annotation .Target ;
1923import java .lang .reflect .Method ;
24+ import java .lang .reflect .Parameter ;
25+ import java .util .List ;
2026
2127import org .junit .jupiter .api .Test ;
2228
@@ -34,6 +40,9 @@ public class UniqueSecurityAnnotationScannerTests {
3440 private UniqueSecurityAnnotationScanner <PreAuthorize > scanner = new UniqueSecurityAnnotationScanner <>(
3541 PreAuthorize .class );
3642
43+ private UniqueSecurityAnnotationScanner <CustomParameterAnnotation > parameterScanner = new UniqueSecurityAnnotationScanner <>(
44+ CustomParameterAnnotation .class );
45+
3746 @ Test
3847 void scanWhenAnnotationOnInterfaceThenResolves () throws Exception {
3948 Method method = AnnotationOnInterface .class .getDeclaredMethod ("method" );
@@ -251,6 +260,77 @@ void scanWhenClassInheritingAbstractClassNoAnnotationsThenNoAnnotation() throws
251260 assertThat (preAuthorize ).isNull ();
252261 }
253262
263+ @ Test
264+ void scanParameterAnnotationWhenAnnotationOnInterface () throws Exception {
265+ Parameter parameter = UserService .class .getDeclaredMethod ("add" , String .class ).getParameters ()[0 ];
266+ CustomParameterAnnotation customParameterAnnotation = this .parameterScanner .scan (parameter );
267+ assertThat (customParameterAnnotation .value ()).isEqualTo ("one" );
268+ }
269+
270+ @ Test
271+ void scanParameterAnnotationWhenClassInheritingInterfaceAnnotation () throws Exception {
272+ Parameter parameter = UserServiceImpl .class .getDeclaredMethod ("add" , String .class ).getParameters ()[0 ];
273+ CustomParameterAnnotation customParameterAnnotation = this .parameterScanner .scan (parameter );
274+ assertThat (customParameterAnnotation .value ()).isEqualTo ("one" );
275+ }
276+
277+ @ Test
278+ void scanParameterAnnotationWhenClassOverridingMethodOverridingInterface () throws Exception {
279+ Parameter parameter = UserServiceImpl .class .getDeclaredMethod ("get" , String .class ).getParameters ()[0 ];
280+ CustomParameterAnnotation customParameterAnnotation = this .parameterScanner .scan (parameter );
281+ assertThat (customParameterAnnotation .value ()).isEqualTo ("five" );
282+ }
283+
284+ @ Test
285+ void scanParameterAnnotationWhenMultipleMethodInheritanceThenException () throws Exception {
286+ Parameter parameter = UserServiceImpl .class .getDeclaredMethod ("list" , String .class ).getParameters ()[0 ];
287+ assertThatExceptionOfType (AnnotationConfigurationException .class )
288+ .isThrownBy (() -> this .parameterScanner .scan (parameter ));
289+ }
290+
291+ interface UserService {
292+
293+ void add (@ CustomParameterAnnotation ("one" ) String user );
294+
295+ List <String > list (@ CustomParameterAnnotation ("two" ) String user );
296+
297+ String get (@ CustomParameterAnnotation ("three" ) String user );
298+
299+ }
300+
301+ interface OtherUserService {
302+
303+ List <String > list (@ CustomParameterAnnotation ("four" ) String user );
304+
305+ }
306+
307+ static class UserServiceImpl implements UserService , OtherUserService {
308+
309+ @ Override
310+ public void add (String user ) {
311+
312+ }
313+
314+ @ Override
315+ public List <String > list (String user ) {
316+ return List .of (user );
317+ }
318+
319+ @ Override
320+ public String get (@ CustomParameterAnnotation ("five" ) String user ) {
321+ return user ;
322+ }
323+
324+ }
325+
326+ @ Target ({ ElementType .PARAMETER })
327+ @ Retention (RetentionPolicy .RUNTIME )
328+ @interface CustomParameterAnnotation {
329+
330+ String value ();
331+
332+ }
333+
254334 @ PreAuthorize ("one" )
255335 private interface AnnotationOnInterface {
256336
0 commit comments