Skip to content

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

Closed
@CLS-CLS

Description

@CLS-CLS

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

Metadata

Metadata

Assignees

Labels

for: external-projectFor an external project and not something we can fix

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions