Skip to content

Commit 10ed500

Browse files
kse-musicjzheaux
authored andcommitted
Method Security templates support use deep non-aliased attributes
Closes gh-16498 Signed-off-by: DingHao <dh.hiekn@gmail.com>
1 parent bc012ef commit 10ed500

File tree

2 files changed

+119
-13
lines changed

2 files changed

+119
-13
lines changed

core/src/main/java/org/springframework/security/core/annotation/ExpressionTemplateSecurityAnnotationScanner.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -62,6 +62,7 @@
6262
*
6363
* @param <A> the annotation to search for and synthesize
6464
* @author Josh Cummings
65+
* @author DingHao
6566
* @since 6.4
6667
*/
6768
final class ExpressionTemplateSecurityAnnotationScanner<A extends Annotation>
@@ -116,27 +117,35 @@ private MergedAnnotation<A> resolvePlaceholders(MergedAnnotation<A> mergedAnnota
116117
PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("{", "}", null, null,
117118
this.templateDefaults.isIgnoreUnknown());
118119
Map<String, Object> properties = new HashMap<>(mergedAnnotation.asMap());
119-
Map<String, Object> metaAnnotationProperties = mergedAnnotation.getMetaSource().asMap();
120-
Map<String, String> stringProperties = new HashMap<>();
121-
for (Map.Entry<String, Object> property : metaAnnotationProperties.entrySet()) {
122-
String key = property.getKey();
123-
Object value = property.getValue();
124-
String asString = (value instanceof String) ? (String) value
125-
: conversionService.convert(value, String.class);
126-
stringProperties.put(key, asString);
127-
}
128-
Map<String, Object> annotationProperties = mergedAnnotation.asMap();
129-
for (Map.Entry<String, Object> annotationProperty : annotationProperties.entrySet()) {
120+
Map<String, String> metaAnnotationProperties = extractMetaAnnotationProperties(mergedAnnotation);
121+
for (Map.Entry<String, Object> annotationProperty : mergedAnnotation.asMap().entrySet()) {
130122
if (!(annotationProperty.getValue() instanceof String expression)) {
131123
continue;
132124
}
133-
String value = helper.replacePlaceholders(expression, stringProperties::get);
125+
String value = helper.replacePlaceholders(expression, metaAnnotationProperties::get);
134126
properties.put(annotationProperty.getKey(), value);
135127
}
136128
AnnotatedElement annotatedElement = (AnnotatedElement) mergedAnnotation.getSource();
137129
return MergedAnnotation.of(annotatedElement, this.type, properties);
138130
}
139131

132+
private Map<String, String> extractMetaAnnotationProperties(MergedAnnotation<A> mergedAnnotation) {
133+
Map<String, String> stringProperties = new HashMap<>();
134+
Map<String, Object> metaAnnotationProperties = new HashMap<>();
135+
MergedAnnotation<?> metaSource = mergedAnnotation.getMetaSource();
136+
while (metaSource != null) {
137+
metaAnnotationProperties.putAll(metaSource.asMap());
138+
metaSource = metaSource.getMetaSource();
139+
}
140+
for (Map.Entry<String, Object> property : metaAnnotationProperties.entrySet()) {
141+
Object value = property.getValue();
142+
String valueString = (value instanceof String) ? (String) value
143+
: conversionService.convert(value, String.class);
144+
stringProperties.put(property.getKey(), valueString);
145+
}
146+
return stringProperties;
147+
}
148+
140149
static class ClassToStringConverter implements GenericConverter {
141150

142151
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

Comments
 (0)