Skip to content

Commit be11812

Browse files
committed
Account for Super-super-interface Inheritance
Closes gh-13625
1 parent 9c44b70 commit be11812

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,28 @@ static <A extends Annotation> A findUniqueAnnotation(Class<?> type, Class<A> ann
9595

9696
private static <A extends Annotation> boolean hasDuplicate(MergedAnnotations mergedAnnotations,
9797
Class<A> annotationType) {
98-
boolean alreadyFound = false;
98+
MergedAnnotation<Annotation> alreadyFound = null;
9999
for (MergedAnnotation<Annotation> mergedAnnotation : mergedAnnotations) {
100100
if (isSynthetic(mergedAnnotation.getSource())) {
101101
continue;
102102
}
103103

104-
if (mergedAnnotation.getType() == annotationType) {
105-
if (alreadyFound) {
106-
return true;
107-
}
108-
alreadyFound = true;
104+
if (mergedAnnotation.getType() != annotationType) {
105+
continue;
106+
}
107+
108+
if (alreadyFound == null) {
109+
alreadyFound = mergedAnnotation;
110+
continue;
111+
}
112+
113+
// https://github.com/spring-projects/spring-framework/issues/31803
114+
if (!mergedAnnotation.getSource().equals(alreadyFound.getSource())) {
115+
return true;
116+
}
117+
118+
if (mergedAnnotation.getRoot().getType() != alreadyFound.getRoot().getType()) {
119+
return true;
109120
}
110121
}
111122
return false;

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ void annotationsOnSyntheticMethodsShouldNotTriggerAnnotationConfigurationExcepti
4141
.isThrownBy(() -> AuthorizationAnnotationUtils.findUniqueAnnotation(method, PreAuthorize.class));
4242
}
4343

44+
@Test // gh-13625
45+
void annotationsFromSuperSuperInterfaceShouldNotTriggerAnnotationConfigurationException() throws Exception {
46+
Method method = HelloImpl.class.getMethod("sayHello");
47+
assertThatNoException()
48+
.isThrownBy(() -> AuthorizationAnnotationUtils.findUniqueAnnotation(method, PreAuthorize.class));
49+
}
50+
4451
private interface BaseRepository<T> {
4552

4653
Iterable<T> findAll();
@@ -55,4 +62,24 @@ private interface StringRepository extends BaseRepository<String> {
5562

5663
}
5764

65+
private interface Hello {
66+
67+
@PreAuthorize("hasRole('someRole')")
68+
String sayHello();
69+
70+
}
71+
72+
private interface SayHello extends Hello {
73+
74+
}
75+
76+
private static class HelloImpl implements SayHello {
77+
78+
@Override
79+
public String sayHello() {
80+
return "hello";
81+
}
82+
83+
}
84+
5885
}

0 commit comments

Comments
 (0)