| 
 | 1 | +/*  | 
 | 2 | + * Copyright 2002-2025 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.core.annotation;  | 
 | 18 | + | 
 | 19 | +import java.lang.annotation.Documented;  | 
 | 20 | +import java.lang.annotation.ElementType;  | 
 | 21 | +import java.lang.annotation.Retention;  | 
 | 22 | +import java.lang.annotation.RetentionPolicy;  | 
 | 23 | +import java.lang.annotation.Target;  | 
 | 24 | +import java.lang.reflect.Method;  | 
 | 25 | + | 
 | 26 | +import org.junit.jupiter.api.Test;  | 
 | 27 | + | 
 | 28 | +import org.springframework.core.annotation.AliasFor;  | 
 | 29 | +import org.springframework.security.access.prepost.PreAuthorize;  | 
 | 30 | + | 
 | 31 | +import static org.assertj.core.api.Assertions.assertThat;  | 
 | 32 | + | 
 | 33 | +/**  | 
 | 34 | + * Tests for {@link ExpressionTemplateSecurityAnnotationScanner}  | 
 | 35 | + *  | 
 | 36 | + * @author DingHao  | 
 | 37 | + */  | 
 | 38 | +public class ExpressionTemplateSecurityAnnotationScannerTests {  | 
 | 39 | + | 
 | 40 | +	private ExpressionTemplateSecurityAnnotationScanner<PreAuthorize> scanner = new ExpressionTemplateSecurityAnnotationScanner<>(  | 
 | 41 | +			PreAuthorize.class, new AnnotationTemplateExpressionDefaults());  | 
 | 42 | + | 
 | 43 | +	@Test  | 
 | 44 | +	void parseMultipleMetaSourceAnnotationParameter() throws Exception {  | 
 | 45 | +		Method method = MessageService.class.getDeclaredMethod("sayHello", String.class);  | 
 | 46 | +		PreAuthorize preAuthorize = this.scanner.scan(method, method.getDeclaringClass());  | 
 | 47 | +		assertThat(preAuthorize.value()).isEqualTo("check(#name)");  | 
 | 48 | +	}  | 
 | 49 | + | 
 | 50 | +	@Test  | 
 | 51 | +	void parseMultipleMetaSourceAnnotationParameterWithAliasFor() throws Exception {  | 
 | 52 | +		Method method = MessageService.class.getDeclaredMethod("save", String.class);  | 
 | 53 | +		PreAuthorize preAuthorize = this.scanner.scan(method, method.getDeclaringClass());  | 
 | 54 | +		assertThat(preAuthorize.value()).isEqualTo("check(#name)");  | 
 | 55 | +	}  | 
 | 56 | + | 
 | 57 | +	@Documented  | 
 | 58 | +	@Retention(RetentionPolicy.RUNTIME)  | 
 | 59 | +	@Target({ ElementType.TYPE, ElementType.METHOD })  | 
 | 60 | +	@PreAuthorize("check({object})")  | 
 | 61 | +	@interface HasPermission {  | 
 | 62 | + | 
 | 63 | +		String object();  | 
 | 64 | + | 
 | 65 | +	}  | 
 | 66 | + | 
 | 67 | +	@Documented  | 
 | 68 | +	@Retention(RetentionPolicy.RUNTIME)  | 
 | 69 | +	@Target({ ElementType.TYPE, ElementType.METHOD })  | 
 | 70 | +	@HasPermission(object = "{value}")  | 
 | 71 | +	@interface HasReadPermission {  | 
 | 72 | + | 
 | 73 | +		String value();  | 
 | 74 | + | 
 | 75 | +	}  | 
 | 76 | + | 
 | 77 | +	@Retention(RetentionPolicy.RUNTIME)  | 
 | 78 | +	@Target({ ElementType.TYPE, ElementType.METHOD })  | 
 | 79 | +	@HasPermission(object = "{value}")  | 
 | 80 | +	@interface HasWritePermission {  | 
 | 81 | + | 
 | 82 | +		@AliasFor(annotation = HasPermission.class, value = "object")  | 
 | 83 | +		String value();  | 
 | 84 | + | 
 | 85 | +	}  | 
 | 86 | + | 
 | 87 | +	private interface MessageService {  | 
 | 88 | + | 
 | 89 | +		@HasReadPermission("#name")  | 
 | 90 | +		String sayHello(String name);  | 
 | 91 | + | 
 | 92 | +		@HasWritePermission("#name")  | 
 | 93 | +		void save(String name);  | 
 | 94 | + | 
 | 95 | +	}  | 
 | 96 | + | 
 | 97 | +}  | 
0 commit comments