Skip to content

Commit 560d4a6

Browse files
Provide Runtime Hints for Beans used in Pre/PostAuthorize Expressions
Closes gh-14652
1 parent 3bb1647 commit 560d4a6

File tree

4 files changed

+553
-0
lines changed

4 files changed

+553
-0
lines changed

config/src/main/java/org/springframework/security/config/annotation/method/configuration/PrePostMethodSecurityConfiguration.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler;
3636
import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler;
3737
import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
38+
import org.springframework.security.aot.hint.PrePostAuthorizeExpressionBeanHintsRegistrar;
39+
import org.springframework.security.aot.hint.SecurityHintsRegistrar;
3840
import org.springframework.security.authorization.AuthorizationEventPublisher;
3941
import org.springframework.security.authorization.ObservationAuthorizationManager;
4042
import org.springframework.security.authorization.method.AuthorizationManagerAfterMethodInterceptor;
@@ -191,6 +193,12 @@ static MethodInterceptor postFilterAuthorizationMethodInterceptor(
191193
() -> _prePostMethodSecurityConfiguration.getObject().postFilterMethodInterceptor);
192194
}
193195

196+
@Bean
197+
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
198+
static SecurityHintsRegistrar prePostAuthorizeExpressionBeanHintsRegistrar() {
199+
return new PrePostAuthorizeExpressionBeanHintsRegistrar();
200+
}
201+
194202
@Override
195203
public void setImportMetadata(AnnotationMetadata importMetadata) {
196204
EnableMethodSecurity annotation = importMetadata.getAnnotations().get(EnableMethodSecurity.class).synthesize();
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright 2002-2024 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.aot.hint;
18+
19+
import java.lang.reflect.Method;
20+
import java.util.Arrays;
21+
import java.util.HashSet;
22+
import java.util.Set;
23+
import java.util.stream.Collectors;
24+
25+
import org.springframework.aot.hint.MemberCategory;
26+
import org.springframework.aot.hint.RuntimeHints;
27+
import org.springframework.aot.hint.TypeReference;
28+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
29+
import org.springframework.beans.factory.support.RegisteredBean;
30+
import org.springframework.expression.spel.SpelNode;
31+
import org.springframework.expression.spel.ast.BeanReference;
32+
import org.springframework.expression.spel.standard.SpelExpression;
33+
import org.springframework.expression.spel.standard.SpelExpressionParser;
34+
import org.springframework.security.access.prepost.PostAuthorize;
35+
import org.springframework.security.access.prepost.PreAuthorize;
36+
import org.springframework.security.authorization.method.AuthorizeReturnObject;
37+
import org.springframework.security.core.annotation.SecurityAnnotationScanner;
38+
import org.springframework.security.core.annotation.SecurityAnnotationScanners;
39+
40+
/**
41+
* A {@link SecurityHintsRegistrar} that scans all beans for methods that use
42+
* {@link PreAuthorize} or {@link PostAuthorize} and registers hints for the beans used
43+
* within the expressions.
44+
*
45+
* @author Marcus da Coregio
46+
* @since 6.4
47+
* @see SecurityHintsAotProcessor
48+
*/
49+
public final class PrePostAuthorizeExpressionBeanHintsRegistrar implements SecurityHintsRegistrar {
50+
51+
private final SecurityAnnotationScanner<PreAuthorize> preAuthorizeScanner = SecurityAnnotationScanners
52+
.requireUnique(PreAuthorize.class);
53+
54+
private final SecurityAnnotationScanner<PostAuthorize> postAuthorizeScanner = SecurityAnnotationScanners
55+
.requireUnique(PostAuthorize.class);
56+
57+
private final SecurityAnnotationScanner<AuthorizeReturnObject> authorizeReturnObjectScanner = SecurityAnnotationScanners
58+
.requireUnique(AuthorizeReturnObject.class);
59+
60+
private final SpelExpressionParser expressionParser = new SpelExpressionParser();
61+
62+
@Override
63+
public void registerHints(RuntimeHints hints, ConfigurableListableBeanFactory beanFactory) {
64+
Set<? extends Class<?>> beans = Arrays.stream(beanFactory.getBeanDefinitionNames())
65+
.map((beanName) -> RegisteredBean.of(beanFactory, beanName).getBeanClass())
66+
.collect(Collectors.toSet());
67+
68+
Set<String> expressions = new HashSet<>();
69+
for (Class<?> bean : beans) {
70+
expressions.addAll(extractSecurityExpressions(bean));
71+
}
72+
Set<String> beanNamesToRegister = new HashSet<>();
73+
for (String expression : expressions) {
74+
beanNamesToRegister.addAll(extractBeanNames(expression));
75+
}
76+
for (String toRegister : beanNamesToRegister) {
77+
Class<?> type = beanFactory.getType(toRegister, false);
78+
if (type == null) {
79+
continue;
80+
}
81+
hints.reflection().registerType(TypeReference.of(type), MemberCategory.INVOKE_DECLARED_METHODS);
82+
}
83+
}
84+
85+
private Set<String> extractSecurityExpressions(Class<?> clazz) {
86+
Set<String> expressions = new HashSet<>();
87+
for (Method method : clazz.getDeclaredMethods()) {
88+
PreAuthorize preAuthorize = this.preAuthorizeScanner.scan(method, clazz);
89+
PostAuthorize postAuthorize = this.postAuthorizeScanner.scan(method, clazz);
90+
if (preAuthorize != null) {
91+
expressions.add(preAuthorize.value());
92+
}
93+
if (postAuthorize != null) {
94+
expressions.add(postAuthorize.value());
95+
}
96+
AuthorizeReturnObject authorizeReturnObject = this.authorizeReturnObjectScanner.scan(method, clazz);
97+
if (authorizeReturnObject != null) {
98+
expressions.addAll(extractSecurityExpressions(method.getReturnType()));
99+
}
100+
}
101+
return expressions;
102+
}
103+
104+
private Set<String> extractBeanNames(String rawExpression) {
105+
SpelExpression expression = this.expressionParser.parseRaw(rawExpression);
106+
SpelNode node = expression.getAST();
107+
Set<String> beanNames = new HashSet<>();
108+
resolveBeanNames(beanNames, node);
109+
return beanNames;
110+
}
111+
112+
private void resolveBeanNames(Set<String> beanNames, SpelNode node) {
113+
if (node instanceof BeanReference br) {
114+
beanNames.add(br.getName());
115+
}
116+
int childCount = node.getChildCount();
117+
if (childCount == 0) {
118+
return;
119+
}
120+
for (int i = 0; i < childCount; i++) {
121+
resolveBeanNames(beanNames, node.getChild(i));
122+
}
123+
}
124+
125+
}

0 commit comments

Comments
 (0)