Skip to content

Commit

Permalink
SecuredAuthorizationManager Finds @secured on Subclasses
Browse files Browse the repository at this point in the history
Closes gh-15002
  • Loading branch information
abimaelrsergio authored and jzheaux committed May 21, 2024
1 parent 0e8fd1c commit 9c94bcc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -61,14 +61,14 @@ private static final class SecuredAuthorizationManagerRegistry extends AbstractA
@Override
AuthorizationManager<MethodInvocation> resolveManager(Method method, Class<?> targetClass) {
Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass);
Secured secured = findSecuredAnnotation(specificMethod);
Secured secured = findSecuredAnnotation(specificMethod, targetClass);
return (secured != null) ? AuthorityAuthorizationManager.hasAnyAuthority(secured.value()) : NULL_MANAGER;
}

private Secured findSecuredAnnotation(Method method) {
private Secured findSecuredAnnotation(Method method, Class<?> targetClass) {
Secured secured = AuthorizationAnnotationUtils.findUniqueAnnotation(method, Secured.class);
return (secured != null) ? secured
: AuthorizationAnnotationUtils.findUniqueAnnotation(method.getDeclaringClass(), Secured.class);
return (secured != null) ? secured : AuthorizationAnnotationUtils
.findUniqueAnnotation((targetClass != null) ? targetClass : method.getDeclaringClass(), Secured.class);
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -141,6 +141,14 @@ public void checkTargetClassAwareWhenInterfaceLevelAnnotationsThenApplies() thro
assertThat(decision.isGranted()).isTrue();
}

@Test
public void checkSecuredAnnotationOnSubclassWhenMethodInSuperclassWasCalledThenApplies() throws Exception {
MockMethodInvocation methodInvocation = new MockMethodInvocation(new Service(), Service.class, "doSmth");
SecuredAuthorizationManager manager = new SecuredAuthorizationManager();
AuthorizationDecision decision = manager.check(TestAuthentication::authenticatedUser, methodInvocation);
assertThat(decision).isNotNull();
}

public static class TestClass implements InterfaceAnnotationsOne, InterfaceAnnotationsTwo {

public void doSomething() {
Expand Down Expand Up @@ -235,4 +243,16 @@ public void inheritedAnnotations() {

}

public abstract class AbstractService {

public void doSmth() {
}

}

@Secured("SECURE")
public class Service extends AbstractService {

}

}

0 comments on commit 9c94bcc

Please sign in to comment.