Skip to content

Commit 4ef604e

Browse files
Feedback
1 parent 560d4a6 commit 4ef604e

File tree

5 files changed

+208
-15
lines changed

5 files changed

+208
-15
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
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;
38+
import org.springframework.security.aot.hint.PrePostAuthorizeHintsRegistrar;
3939
import org.springframework.security.aot.hint.SecurityHintsRegistrar;
4040
import org.springframework.security.authorization.AuthorizationEventPublisher;
4141
import org.springframework.security.authorization.ObservationAuthorizationManager;
@@ -195,8 +195,8 @@ static MethodInterceptor postFilterAuthorizationMethodInterceptor(
195195

196196
@Bean
197197
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
198-
static SecurityHintsRegistrar prePostAuthorizeExpressionBeanHintsRegistrar() {
199-
return new PrePostAuthorizeExpressionBeanHintsRegistrar();
198+
static SecurityHintsRegistrar prePostAuthorizeExpressionHintsRegistrar() {
199+
return new PrePostAuthorizeHintsRegistrar();
200200
}
201201

202202
@Override

core/src/main/java/org/springframework/security/aot/hint/PrePostAuthorizeExpressionBeanHintsRegistrar.java

+42-9
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818

1919
import java.lang.reflect.Method;
2020
import java.util.Arrays;
21+
import java.util.Collections;
2122
import java.util.HashSet;
23+
import java.util.List;
2224
import java.util.Set;
23-
import java.util.stream.Collectors;
2425

2526
import org.springframework.aot.hint.MemberCategory;
2627
import org.springframework.aot.hint.RuntimeHints;
2728
import org.springframework.aot.hint.TypeReference;
2829
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
29-
import org.springframework.beans.factory.support.RegisteredBean;
3030
import org.springframework.expression.spel.SpelNode;
3131
import org.springframework.expression.spel.ast.BeanReference;
3232
import org.springframework.expression.spel.standard.SpelExpression;
@@ -36,11 +36,30 @@
3636
import org.springframework.security.authorization.method.AuthorizeReturnObject;
3737
import org.springframework.security.core.annotation.SecurityAnnotationScanner;
3838
import org.springframework.security.core.annotation.SecurityAnnotationScanners;
39+
import org.springframework.util.Assert;
3940

4041
/**
41-
* A {@link SecurityHintsRegistrar} that scans all beans for methods that use
42+
* A {@link SecurityHintsRegistrar} that scans all provided classes for methods that use
4243
* {@link PreAuthorize} or {@link PostAuthorize} and registers hints for the beans used
43-
* within the expressions.
44+
* within the security expressions.
45+
*
46+
* <p>
47+
* It will also scan return types of methods annotated with {@link AuthorizeReturnObject}.
48+
*
49+
* <p>
50+
* This may be used by an application to register specific Security-adjacent classes that
51+
* were otherwise missed by Spring Security's reachability scans.
52+
*
53+
* <p>
54+
* Remember to register this as an infrastructural bean like so:
55+
*
56+
* <pre>
57+
* &#064;Bean
58+
* &#064;Role(BeanDefinition.ROLE_INFRASTRUCTURE)
59+
* static SecurityHintsRegistrar registerThese() {
60+
* return new PrePostAuthorizeExpressionBeanHintsRegistrar(MyClass.class);
61+
* }
62+
* </pre>
4463
*
4564
* @author Marcus da Coregio
4665
* @since 6.4
@@ -59,14 +78,24 @@ public final class PrePostAuthorizeExpressionBeanHintsRegistrar implements Secur
5978

6079
private final SpelExpressionParser expressionParser = new SpelExpressionParser();
6180

81+
private final Set<Class<?>> visitedClasses = new HashSet<>();
82+
83+
private final List<Class<?>> toVisit;
84+
85+
public PrePostAuthorizeExpressionBeanHintsRegistrar(Class<?>... toVisit) {
86+
this(Arrays.asList(toVisit));
87+
}
88+
89+
public PrePostAuthorizeExpressionBeanHintsRegistrar(List<Class<?>> toVisit) {
90+
Assert.notEmpty(toVisit, "toVisit cannot be empty");
91+
Assert.noNullElements(toVisit, "toVisit cannot contain null elements");
92+
this.toVisit = toVisit;
93+
}
94+
6295
@Override
6396
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-
6897
Set<String> expressions = new HashSet<>();
69-
for (Class<?> bean : beans) {
98+
for (Class<?> bean : this.toVisit) {
7099
expressions.addAll(extractSecurityExpressions(bean));
71100
}
72101
Set<String> beanNamesToRegister = new HashSet<>();
@@ -83,6 +112,10 @@ public void registerHints(RuntimeHints hints, ConfigurableListableBeanFactory be
83112
}
84113

85114
private Set<String> extractSecurityExpressions(Class<?> clazz) {
115+
if (this.visitedClasses.contains(clazz)) {
116+
return Collections.emptySet();
117+
}
118+
this.visitedClasses.add(clazz);
86119
Set<String> expressions = new HashSet<>();
87120
for (Method method : clazz.getDeclaredMethods()) {
88121
PreAuthorize preAuthorize = this.preAuthorizeScanner.scan(method, clazz);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.util.Arrays;
20+
import java.util.List;
21+
import java.util.stream.Collectors;
22+
23+
import org.springframework.aot.hint.RuntimeHints;
24+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
25+
import org.springframework.beans.factory.support.RegisteredBean;
26+
import org.springframework.security.access.prepost.PostAuthorize;
27+
import org.springframework.security.access.prepost.PreAuthorize;
28+
29+
/**
30+
* A {@link SecurityHintsRegistrar} that scans all beans for methods that use
31+
* {@link PreAuthorize} or {@link PostAuthorize} and registers appropriate hints for the
32+
* annotations.
33+
*
34+
* @author Marcus da Coregio
35+
* @since 6.4
36+
* @see SecurityHintsAotProcessor
37+
* @see PrePostAuthorizeExpressionBeanHintsRegistrar
38+
*/
39+
public final class PrePostAuthorizeHintsRegistrar implements SecurityHintsRegistrar {
40+
41+
@Override
42+
public void registerHints(RuntimeHints hints, ConfigurableListableBeanFactory beanFactory) {
43+
List<Class<?>> beans = Arrays.stream(beanFactory.getBeanDefinitionNames())
44+
.map((beanName) -> RegisteredBean.of(beanFactory, beanName).getBeanClass())
45+
.collect(Collectors.toList());
46+
new PrePostAuthorizeExpressionBeanHintsRegistrar(beans).registerHints(hints, beanFactory);
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,21 @@
2222
import org.springframework.aot.hint.MemberCategory;
2323
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
2424
import org.springframework.aot.test.generate.TestGenerationContext;
25+
import org.springframework.beans.factory.config.BeanDefinition;
2526
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
2627
import org.springframework.beans.factory.support.RootBeanDefinition;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Role;
2730
import org.springframework.security.access.prepost.PostAuthorize;
2831
import org.springframework.security.access.prepost.PreAuthorize;
2932
import org.springframework.security.authorization.method.AuthorizeReturnObject;
3033

3134
import static org.assertj.core.api.Assertions.assertThat;
35+
import static org.assertj.core.api.Assertions.assertThatNoException;
3236

33-
class PrePostAuthorizeExpressionBeanHintsRegistrarTests {
37+
class PrePostAuthorizeHintsRegistrarTests {
3438

35-
private final PrePostAuthorizeExpressionBeanHintsRegistrar registrar = new PrePostAuthorizeExpressionBeanHintsRegistrar();
39+
private final PrePostAuthorizeHintsRegistrar registrar = new PrePostAuthorizeHintsRegistrar();
3640

3741
private final GenerationContext generationContext = new TestGenerationContext();
3842

@@ -158,6 +162,11 @@ void registerHintsWhenSecurityAnnotationsInsideAuthorizeReturnObjectOnClassThenR
158162
.accepts(this.generationContext.getRuntimeHints());
159163
}
160164

165+
@Test
166+
void registerHintsWhenCyclicDependencyThenNoStackOverflowException() {
167+
assertThatNoException().isThrownBy(() -> process(AService.class));
168+
}
169+
161170
private void process(Class<?>... beanClasses) {
162171
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
163172
for (Class<?> beanClass : beanClasses) {
@@ -312,4 +321,41 @@ String getFullName() {
312321

313322
}
314323

324+
static class AService {
325+
326+
@AuthorizeReturnObject
327+
A getA() {
328+
return new A();
329+
}
330+
331+
}
332+
333+
static class A {
334+
335+
@AuthorizeReturnObject
336+
B getB() {
337+
return null;
338+
}
339+
340+
}
341+
342+
static class B {
343+
344+
@AuthorizeReturnObject
345+
A getA() {
346+
return null;
347+
}
348+
349+
}
350+
351+
static class UserSecurityHintsRegistrar {
352+
353+
@Bean
354+
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
355+
static SecurityHintsRegistrar registerTheseToo() {
356+
return new PrePostAuthorizeExpressionBeanHintsRegistrar(User.class);
357+
}
358+
359+
}
360+
315361
}

docs/modules/ROOT/pages/servlet/authorization/method-security.adoc

+66-1
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,6 @@ If it finds a method that uses `@AuthorizeReturnObject`, it will recursively sea
15361536

15371537
For example, consider the following Spring Boot application:
15381538

1539-
.Custom MethodSecurityExpressionHandler
15401539
[tabs]
15411540
======
15421541
Java::
@@ -1633,6 +1632,72 @@ class User(private val fullName: String) {
16331632
<5> Finding another `@AuthorizeReturnObject` it will look again into the method's return type
16341633
<6> Now, a `@PostAuthorize` is found with yet another bean name used: `myOtherAuthz`; the runtime hints are registered for the bean class as well
16351634

1635+
There will be many times when Spring Security cannot determine the actual return type of the method ahead of time since it may be hidden in an erased generic type.
1636+
1637+
Consider the following service:
1638+
1639+
[tabs]
1640+
======
1641+
Java::
1642+
+
1643+
[source,java,role="primary"]
1644+
----
1645+
@Service
1646+
public class AccountService {
1647+
1648+
@AuthorizeReturnObject
1649+
public List<Account> getAllAccounts() {
1650+
// ...
1651+
}
1652+
1653+
}
1654+
----
1655+
1656+
Kotlin::
1657+
+
1658+
[source,kotlin,role="secondary"]
1659+
----
1660+
@Service
1661+
class AccountService {
1662+
1663+
@AuthorizeReturnObject
1664+
fun getAllAccounts(): List<Account> {
1665+
// ...
1666+
}
1667+
1668+
}
1669+
----
1670+
======
1671+
1672+
In this case, the generic type is erased and so it isn’t apparent to Spring Security ahead-of-time that `Account` needs to be visited in order to check for `@PreAuthorize` and `@PostAuthorize`.
1673+
1674+
To address this, you can publish a javadoc:org.springframework.security.aot.hint.PrePostAuthorizeExpressionBeanHintsRegistrar[`PrePostAuthorizeExpressionBeanHintsRegistrar`] like so:
1675+
1676+
[tabs]
1677+
======
1678+
Java::
1679+
+
1680+
[source,java,role="primary"]
1681+
----
1682+
@Bean
1683+
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
1684+
static SecurityHintsRegistrar registerTheseToo() {
1685+
return new PrePostAuthorizeExpressionBeanHintsRegistrar(Account.class);
1686+
}
1687+
----
1688+
1689+
Kotlin::
1690+
+
1691+
[source,kotlin,role="secondary"]
1692+
----
1693+
@Bean
1694+
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
1695+
fun registerTheseToo(): SecurityHintsRegistrar {
1696+
return PrePostAuthorizeExpressionBeanHintsRegistrar(Account::class.java)
1697+
}
1698+
----
1699+
======
1700+
16361701
[[use-aspectj]]
16371702
== Authorizing with AspectJ
16381703

0 commit comments

Comments
 (0)