Skip to content

SecuredAnnotationSecurityMetadataSource cannot infer annotation class when annotationMetadataExtractor is provided as a lamda expression #10592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
CLS-CLS opened this issue Dec 7, 2021 · 3 comments
Assignees
Labels
for: external-project For an external project and not something we can fix

Comments

@CLS-CLS
Copy link

CLS-CLS commented Dec 7, 2021

Summary

In the following example the constructor of the SecuredAnnotationSecurityMetadataSource cannot understand the type of the annotation and instead of assigning the value "MyCustomAnnotation" in the annotationType field it assigns the value "Annotation"
which later does not allow the org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor to be triggered for any method that is annotated with the "MyCustomAnnotation" annotation.

public class CustomMethodSecurityConfiguration extends GlobalMethodSecurityConfiguration {
    @Override
    protected MethodSecurityMetadataSource customMethodSecurityMetadataSource() {
        return new SecuredAnnotationSecurityMetadataSource(annotationMetaDataExtractor());
    }
    private AnnotationMetadataExtractor<MyCustomAnnotation> annotationMetaDataExtractor() {
         return (MyCustomAnnotation securityAnnotation) -> Collections.singleton((ConfigAttribute) () -> "test");
    }   
}  

When rewriting the annotationMetaDataExtractor method without lambdas, the annotationType field of the SecuredAnnotationSecurityMetadataSource is correctly set to "MyCustomAnnotation" and the interceptor is triggered as expected

 private AnnotationMetadataExtractor<MyCustomAnnotation> annotationMetaDataExtractor() {
     return new AnnotationMetadataExtractor<MyCustomAnnotation>() {
        @Override
        public Collection<? extends ConfigAttribute> extractAttributes(MyCustomAnnotation securityAnnotation) {
            return Collections.singleton((ConfigAttribute) () -> "test");
        }
    };
 }

Actual Behavior

org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor not to be triggered for any method that is annotated with the "MyCustomAnnotation" annotation.

Expected Behavior

Any method annotation with @MyCustomAnnotation should be picked up by the interceptor (and apply the security logic)

Version

spring-security-code: 5.5.3

@eleftherias eleftherias self-assigned this Dec 7, 2021
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 7, 2021
@eleftherias
Copy link
Contributor

Thanks for reaching out and providing a workaround @CLS-CLS.
Unfortunately this is a known limitation of using lambdas with generic types.
You can track the related Spring Framework issue spring-projects/spring-framework#17130.

@eleftherias eleftherias added for: external-project For an external project and not something we can fix and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 8, 2021
@CLS-CLS
Copy link
Author

CLS-CLS commented Dec 9, 2021

May i propose until the issues is fixed (which it seems it will take a lot of time), to update the javadoc to warn the user not to use lambdas? At least that way will not be afraid to use lambdas in all places.
i.e on the existing javadoc of SecuredAnnotationSecurityMetadataSource add an implementation note

/**
 * Sources method security metadata from Spring Security's {@link Secured} annotation.
 * <p>
 * Can also be used with custom security annotations by injecting an
 * {@link AnnotationMetadataExtractor}. The annotation type will then be obtained from the
 * generic parameter type supplied to this interface
 * @ImplNote use caution when providing an extractor as a lambda function because the generic type is not picked up

@eleftherias
Copy link
Contributor

eleftherias commented Dec 13, 2021

I think adding a note to the Javadoc is reasonable @CLS-CLS. Would you like to submit a PR for that?
Note that we don't use the @ImplNote tag in this project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix
Projects
None yet
Development

No branches or pull requests

3 participants