|
| 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