Skip to content

Commit 9c94bcc

Browse files
abimaelrsergiojzheaux
authored andcommitted
SecuredAuthorizationManager Finds @secured on Subclasses
Closes spring-projectsgh-15002
1 parent 0e8fd1c commit 9c94bcc

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

core/src/main/java/org/springframework/security/authorization/method/SecuredAuthorizationManager.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,14 +61,14 @@ private static final class SecuredAuthorizationManagerRegistry extends AbstractA
6161
@Override
6262
AuthorizationManager<MethodInvocation> resolveManager(Method method, Class<?> targetClass) {
6363
Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass);
64-
Secured secured = findSecuredAnnotation(specificMethod);
64+
Secured secured = findSecuredAnnotation(specificMethod, targetClass);
6565
return (secured != null) ? AuthorityAuthorizationManager.hasAnyAuthority(secured.value()) : NULL_MANAGER;
6666
}
6767

68-
private Secured findSecuredAnnotation(Method method) {
68+
private Secured findSecuredAnnotation(Method method, Class<?> targetClass) {
6969
Secured secured = AuthorizationAnnotationUtils.findUniqueAnnotation(method, Secured.class);
70-
return (secured != null) ? secured
71-
: AuthorizationAnnotationUtils.findUniqueAnnotation(method.getDeclaringClass(), Secured.class);
70+
return (secured != null) ? secured : AuthorizationAnnotationUtils
71+
.findUniqueAnnotation((targetClass != null) ? targetClass : method.getDeclaringClass(), Secured.class);
7272
}
7373

7474
}

core/src/test/java/org/springframework/security/authorization/method/SecuredAuthorizationManagerTests.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -141,6 +141,14 @@ public void checkTargetClassAwareWhenInterfaceLevelAnnotationsThenApplies() thro
141141
assertThat(decision.isGranted()).isTrue();
142142
}
143143

144+
@Test
145+
public void checkSecuredAnnotationOnSubclassWhenMethodInSuperclassWasCalledThenApplies() throws Exception {
146+
MockMethodInvocation methodInvocation = new MockMethodInvocation(new Service(), Service.class, "doSmth");
147+
SecuredAuthorizationManager manager = new SecuredAuthorizationManager();
148+
AuthorizationDecision decision = manager.check(TestAuthentication::authenticatedUser, methodInvocation);
149+
assertThat(decision).isNotNull();
150+
}
151+
144152
public static class TestClass implements InterfaceAnnotationsOne, InterfaceAnnotationsTwo {
145153

146154
public void doSomething() {
@@ -235,4 +243,16 @@ public void inheritedAnnotations() {
235243

236244
}
237245

246+
public abstract class AbstractService {
247+
248+
public void doSmth() {
249+
}
250+
251+
}
252+
253+
@Secured("SECURE")
254+
public class Service extends AbstractService {
255+
256+
}
257+
238258
}

0 commit comments

Comments
 (0)