Skip to content

Commit 9669747

Browse files
Florian Cramerjzheaux
authored andcommitted
Ignore synthetic methods when checking for duplicate annotations
Closes gh-13132
1 parent b969179 commit 9669747

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.security.authorization.method;
1818

1919
import java.lang.annotation.Annotation;
20+
import java.lang.reflect.Executable;
2021
import java.lang.reflect.Method;
2122

2223
import org.springframework.core.annotation.AnnotationConfigurationException;
@@ -96,6 +97,10 @@ private static <A extends Annotation> boolean hasDuplicate(MergedAnnotations mer
9697
Class<A> annotationType) {
9798
boolean alreadyFound = false;
9899
for (MergedAnnotation<Annotation> mergedAnnotation : mergedAnnotations) {
100+
if (isSynthetic(mergedAnnotation.getSource())) {
101+
continue;
102+
}
103+
99104
if (mergedAnnotation.getType() == annotationType) {
100105
if (alreadyFound) {
101106
return true;
@@ -106,6 +111,14 @@ private static <A extends Annotation> boolean hasDuplicate(MergedAnnotations mer
106111
return false;
107112
}
108113

114+
private static boolean isSynthetic(Object object) {
115+
if (object instanceof Executable) {
116+
return ((Executable) object).isSynthetic();
117+
}
118+
119+
return false;
120+
}
121+
109122
private AuthorizationAnnotationUtils() {
110123

111124
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.springframework.security.authorization.method;
2+
3+
import java.lang.reflect.Method;
4+
import java.lang.reflect.Proxy;
5+
import java.util.List;
6+
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.security.access.prepost.PreAuthorize;
9+
10+
import static org.assertj.core.api.Assertions.assertThatNoException;
11+
12+
/**
13+
* Tests for {@link AuthorizationAnnotationUtils}
14+
*/
15+
class AuthorizationAnnotationUtilsTests {
16+
17+
@Test // gh-13132
18+
public void annotationsOnSyntheticMethodsShouldNotTriggerAnnotationConfigurationException()
19+
throws NoSuchMethodException {
20+
StringRepository proxy =
21+
(StringRepository) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
22+
new Class[] {StringRepository.class}, (p, m, args) -> null);
23+
Method method = proxy.getClass().getDeclaredMethod("findAll");
24+
assertThatNoException()
25+
.isThrownBy(() -> AuthorizationAnnotationUtils.findUniqueAnnotation(method, PreAuthorize.class));
26+
}
27+
28+
private interface BaseRepository<T> {
29+
30+
Iterable<T> findAll();
31+
}
32+
33+
private interface StringRepository extends BaseRepository<String> {
34+
35+
@Override
36+
@PreAuthorize("hasRole('someRole')")
37+
List<String> findAll();
38+
}
39+
}

0 commit comments

Comments
 (0)