|
17 | 17 | package org.springframework.security.messaging.context; |
18 | 18 |
|
19 | 19 | import java.lang.annotation.Annotation; |
| 20 | +import java.lang.reflect.AnnotatedElement; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.concurrent.ConcurrentHashMap; |
| 24 | +import java.util.function.Function; |
20 | 25 |
|
21 | 26 | import org.springframework.core.MethodParameter; |
22 | | -import org.springframework.core.annotation.AnnotationUtils; |
| 27 | +import org.springframework.core.annotation.MergedAnnotation; |
| 28 | +import org.springframework.core.annotation.MergedAnnotations; |
| 29 | +import org.springframework.core.annotation.RepeatableContainers; |
| 30 | +import org.springframework.core.convert.support.DefaultConversionService; |
23 | 31 | import org.springframework.expression.Expression; |
24 | 32 | import org.springframework.expression.ExpressionParser; |
25 | 33 | import org.springframework.expression.spel.standard.SpelExpressionParser; |
26 | 34 | import org.springframework.expression.spel.support.StandardEvaluationContext; |
| 35 | +import org.springframework.lang.NonNull; |
27 | 36 | import org.springframework.messaging.Message; |
28 | 37 | import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver; |
| 38 | +import org.springframework.security.authorization.method.AuthenticationPrincipalTemplateDefaults; |
29 | 39 | import org.springframework.security.core.Authentication; |
30 | 40 | import org.springframework.security.core.annotation.AuthenticationPrincipal; |
31 | 41 | import org.springframework.security.core.context.SecurityContextHolder; |
32 | 42 | import org.springframework.security.core.context.SecurityContextHolderStrategy; |
33 | 43 | import org.springframework.stereotype.Controller; |
34 | 44 | import org.springframework.util.Assert; |
35 | 45 | import org.springframework.util.ClassUtils; |
| 46 | +import org.springframework.util.PropertyPlaceholderHelper; |
36 | 47 | import org.springframework.util.StringUtils; |
37 | 48 |
|
38 | 49 | /** |
|
83 | 94 | * </pre> |
84 | 95 | * |
85 | 96 | * @author Rob Winch |
| 97 | + * @author DingHao |
86 | 98 | * @since 4.0 |
87 | 99 | */ |
88 | 100 | public final class AuthenticationPrincipalArgumentResolver implements HandlerMethodArgumentResolver { |
89 | 101 |
|
90 | 102 | private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder |
91 | 103 | .getContextHolderStrategy(); |
92 | 104 |
|
| 105 | + private final Map<MethodParameter, Annotation> cachedAttributes = new ConcurrentHashMap<>(); |
| 106 | + |
93 | 107 | private ExpressionParser parser = new SpelExpressionParser(); |
94 | 108 |
|
| 109 | + private AuthenticationPrincipalTemplateDefaults principalTemplateDefaults = new AuthenticationPrincipalTemplateDefaults(); |
| 110 | + |
95 | 111 | @Override |
96 | 112 | public boolean supportsParameter(MethodParameter parameter) { |
97 | 113 | return findMethodAnnotation(AuthenticationPrincipal.class, parameter) != null; |
@@ -133,26 +149,74 @@ public void setSecurityContextHolderStrategy(SecurityContextHolderStrategy secur |
133 | 149 | this.securityContextHolderStrategy = securityContextHolderStrategy; |
134 | 150 | } |
135 | 151 |
|
| 152 | + /** |
| 153 | + * Configure AuthenticationPrincipal template resolution |
| 154 | + * <p> |
| 155 | + * By default, this value is <code>null</code>, which indicates that templates should |
| 156 | + * not be resolved. |
| 157 | + * @param principalTemplateDefaults - whether to resolve AuthenticationPrincipal |
| 158 | + * templates parameters |
| 159 | + * @since 6.4 |
| 160 | + */ |
| 161 | + public void setTemplateDefaults(@NonNull AuthenticationPrincipalTemplateDefaults principalTemplateDefaults) { |
| 162 | + Assert.notNull(principalTemplateDefaults, "principalTemplateDefaults cannot be null"); |
| 163 | + this.principalTemplateDefaults = principalTemplateDefaults; |
| 164 | + } |
| 165 | + |
136 | 166 | /** |
137 | 167 | * Obtains the specified {@link Annotation} on the specified {@link MethodParameter}. |
138 | 168 | * @param annotationClass the class of the {@link Annotation} to find on the |
139 | 169 | * {@link MethodParameter} |
140 | 170 | * @param parameter the {@link MethodParameter} to search for an {@link Annotation} |
141 | 171 | * @return the {@link Annotation} that was found or null. |
142 | 172 | */ |
| 173 | + @SuppressWarnings("unchecked") |
143 | 174 | private <T extends Annotation> T findMethodAnnotation(Class<T> annotationClass, MethodParameter parameter) { |
| 175 | + return (T) this.cachedAttributes.computeIfAbsent(parameter, |
| 176 | + methodParameter -> findMethodAnnotation(annotationClass, methodParameter, |
| 177 | + this.principalTemplateDefaults)); |
| 178 | + } |
| 179 | + |
| 180 | + private static <T extends Annotation> T findMethodAnnotation(Class<T> annotationClass, MethodParameter parameter, |
| 181 | + AuthenticationPrincipalTemplateDefaults principalTemplateDefaults) { |
144 | 182 | T annotation = parameter.getParameterAnnotation(annotationClass); |
145 | 183 | if (annotation != null) { |
146 | 184 | return annotation; |
147 | 185 | } |
148 | | - Annotation[] annotationsToSearch = parameter.getParameterAnnotations(); |
149 | | - for (Annotation toSearch : annotationsToSearch) { |
150 | | - annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), annotationClass); |
151 | | - if (annotation != null) { |
152 | | - return annotation; |
| 186 | + return MergedAnnotations |
| 187 | + .from(parameter.getParameter(), MergedAnnotations.SearchStrategy.TYPE_HIERARCHY, |
| 188 | + RepeatableContainers.none()) |
| 189 | + .stream(annotationClass) |
| 190 | + .map(mapper(annotationClass, principalTemplateDefaults.isIgnoreUnknown(), "expression")) |
| 191 | + .findFirst() |
| 192 | + .orElse(null); |
| 193 | + } |
| 194 | + |
| 195 | + private static <T extends Annotation> Function<MergedAnnotation<T>, T> mapper(Class<T> annotationClass, |
| 196 | + boolean ignoreUnresolvablePlaceholders, String... attrs) { |
| 197 | + return (mergedAnnotation) -> { |
| 198 | + MergedAnnotation<?> metaSource = mergedAnnotation.getMetaSource(); |
| 199 | + if (metaSource == null) { |
| 200 | + return mergedAnnotation.synthesize(); |
153 | 201 | } |
154 | | - } |
155 | | - return null; |
| 202 | + PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("{", "}", null, null, |
| 203 | + ignoreUnresolvablePlaceholders); |
| 204 | + Map<String, String> stringProperties = new HashMap<>(); |
| 205 | + for (Map.Entry<String, Object> property : metaSource.asMap().entrySet()) { |
| 206 | + String key = property.getKey(); |
| 207 | + Object value = property.getValue(); |
| 208 | + String asString = (value instanceof String) ? (String) value |
| 209 | + : DefaultConversionService.getSharedInstance().convert(value, String.class); |
| 210 | + stringProperties.put(key, asString); |
| 211 | + } |
| 212 | + Map<String, Object> attrMap = mergedAnnotation.asMap(); |
| 213 | + Map<String, Object> properties = new HashMap<>(attrMap); |
| 214 | + for (String attr : attrs) { |
| 215 | + properties.put(attr, helper.replacePlaceholders((String) attrMap.get(attr), stringProperties::get)); |
| 216 | + } |
| 217 | + return MergedAnnotation.of((AnnotatedElement) mergedAnnotation.getSource(), annotationClass, properties) |
| 218 | + .synthesize(); |
| 219 | + }; |
156 | 220 | } |
157 | 221 |
|
158 | 222 | } |
0 commit comments