Skip to content

Distinguish same-distanced annotations in MergedAnnotations #34611

Open
@jzheaux

Description

@jzheaux

Spring Security does a search for annotations in a slightly different way than TYPE_HIERARCHY. It does the following:

  1. If it finds the annotation on the target method, then it stops searching
  2. Otherwise, it looks one level up (superclass + interfaces). If it finds the annotation on more than one, it errors; otherwise if it finds it on just one, it stops searching
  3. Otherwise, recursively return to level 2 for each class/interface

In other words, Security wants to use the @PreAuthorize annotation (for example) that is closest to the method being invoked, and if more than one is equally close, it errors.

The traversal logic is similar in many ways to AnnotationScanner, but I haven't found a way to exercise the API to achieve the above.

In talking with @sbrannen, it was thought that getAggregateIndex might allow for this kind of searching; however that does not appear to work, so this may be a bug.

Currently, Security duplicates the method hierarchy traversal logic in AnnotationsScanner in its authorization support. Whether a bug or an enhancement, being able to rely on MergedAnnotations instead would be more secure and easier to support.

Activity

added
in: coreIssues in core modules (aop, beans, core, context, expression)
on Mar 17, 2025
changed the title [-]Distinguish Same-Distanced Annotations in MergedAnnotations[/-] [+]Distinguish same-distanced annotations in `MergedAnnotations`[/+] on Mar 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)status: waiting-for-triageAn issue we've not yet triaged or decided on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @sbrannen@jzheaux

        Issue actions

          Distinguish same-distanced annotations in `MergedAnnotations` · Issue #34611 · spring-projects/spring-framework