Skip to content

Commit d7132b4

Browse files
committed
Method Security templates support use deep non-aliased attributes
Closes gh-16498 Signed-off-by: DingHao <dh.hiekn@gmail.com>
1 parent 8e2a4bf commit d7132b4

File tree

2 files changed

+98
-13
lines changed

2 files changed

+98
-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,76 @@
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.security.access.prepost.PreAuthorize;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
32+
/**
33+
* Tests for {@link ExpressionTemplateSecurityAnnotationScanner}
34+
*
35+
* @author DingHao
36+
*/
37+
public class ExpressionTemplateSecurityAnnotationScannerTests {
38+
39+
private ExpressionTemplateSecurityAnnotationScanner<PreAuthorize> scanner = new ExpressionTemplateSecurityAnnotationScanner<>(
40+
PreAuthorize.class, new AnnotationTemplateExpressionDefaults());
41+
42+
@Test
43+
void parseMultipleMetaSourceAnnotationParameter() throws Exception {
44+
Method method = MessageService.class.getDeclaredMethod("sayHello", String.class);
45+
PreAuthorize preAuthorize = this.scanner.scan(method, method.getDeclaringClass());
46+
assertThat(preAuthorize.value()).isEqualTo("check(#name)");
47+
}
48+
49+
@Documented
50+
@Retention(RetentionPolicy.RUNTIME)
51+
@Target({ ElementType.TYPE, ElementType.METHOD })
52+
@PreAuthorize("check({object})")
53+
@interface HasPermission {
54+
55+
String object();
56+
57+
}
58+
59+
@Documented
60+
@Retention(RetentionPolicy.RUNTIME)
61+
@Target({ ElementType.TYPE, ElementType.METHOD })
62+
@HasPermission(object = "{value}")
63+
@interface HasReadPermission {
64+
65+
String value();
66+
67+
}
68+
69+
private interface MessageService {
70+
71+
@HasReadPermission("#name")
72+
String sayHello(String name);
73+
74+
}
75+
76+
}

0 commit comments

Comments
 (0)