Skip to content

Commit 613165b

Browse files
committed
Merge branch '6.0.x'
2 parents a4e13c5 + c6c091b commit 613165b

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

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

Lines changed: 14 additions & 1 deletion
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-2023 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.
@@ -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,58 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.authorization.method;
18+
19+
import java.lang.reflect.Method;
20+
import java.lang.reflect.Proxy;
21+
import java.util.List;
22+
23+
import org.junit.jupiter.api.Test;
24+
25+
import org.springframework.security.access.prepost.PreAuthorize;
26+
27+
import static org.assertj.core.api.Assertions.assertThatNoException;
28+
29+
/**
30+
* Tests for {@link AuthorizationAnnotationUtils}
31+
*/
32+
class AuthorizationAnnotationUtilsTests {
33+
34+
@Test // gh-13132
35+
void annotationsOnSyntheticMethodsShouldNotTriggerAnnotationConfigurationException() throws NoSuchMethodException {
36+
StringRepository proxy = (StringRepository) Proxy.newProxyInstance(
37+
Thread.currentThread().getContextClassLoader(), new Class[] { StringRepository.class },
38+
(p, m, args) -> null);
39+
Method method = proxy.getClass().getDeclaredMethod("findAll");
40+
assertThatNoException()
41+
.isThrownBy(() -> AuthorizationAnnotationUtils.findUniqueAnnotation(method, PreAuthorize.class));
42+
}
43+
44+
private interface BaseRepository<T> {
45+
46+
Iterable<T> findAll();
47+
48+
}
49+
50+
private interface StringRepository extends BaseRepository<String> {
51+
52+
@Override
53+
@PreAuthorize("hasRole('someRole')")
54+
List<String> findAll();
55+
56+
}
57+
58+
}

0 commit comments

Comments
 (0)