Skip to content

Commit df61e29

Browse files
committed
Sort Default Advisors and Added Advisors
This commit ensures that the default advisors and added advisors are sorted in the event that this component is not being published as a Spring bean. Issue gh-16819
1 parent 5b7baee commit df61e29

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

core/src/main/java/org/springframework/security/authorization/method/AuthorizationAdvisorProxyFactory.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ public static AuthorizationAdvisorProxyFactory withDefaults() {
109109
advisors.add(AuthorizationManagerAfterMethodInterceptor.postAuthorize());
110110
advisors.add(new PreFilterAuthorizationMethodInterceptor());
111111
advisors.add(new PostFilterAuthorizationMethodInterceptor());
112-
return new AuthorizationAdvisorProxyFactory(advisors);
112+
AuthorizationAdvisorProxyFactory factory = new AuthorizationAdvisorProxyFactory(advisors);
113+
AnnotationAwareOrderComparator.sort(factory.advisors);
114+
return factory;
113115
}
114116

115117
/**
@@ -124,7 +126,9 @@ public static AuthorizationAdvisorProxyFactory withReactiveDefaults() {
124126
advisors.add(AuthorizationManagerAfterReactiveMethodInterceptor.postAuthorize());
125127
advisors.add(new PreFilterAuthorizationReactiveMethodInterceptor());
126128
advisors.add(new PostFilterAuthorizationReactiveMethodInterceptor());
127-
return new AuthorizationAdvisorProxyFactory(advisors);
129+
AuthorizationAdvisorProxyFactory factory = new AuthorizationAdvisorProxyFactory(advisors);
130+
AnnotationAwareOrderComparator.sort(factory.advisors);
131+
return factory;
128132
}
129133

130134
@Override
@@ -160,6 +164,7 @@ public Object proxy(Object target) {
160164
return proxied;
161165
}
162166
ProxyFactory factory = new ProxyFactory(target);
167+
AnnotationAwareOrderComparator.sort(this.advisors);
163168
for (Advisor advisor : this.advisors) {
164169
factory.addAdvisors(advisor);
165170
}

core/src/test/java/org/springframework/security/authorization/AuthorizationAdvisorProxyFactoryTests.java

+28-1
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.
@@ -38,6 +38,7 @@
3838
import org.junit.jupiter.api.Test;
3939

4040
import org.springframework.aop.Pointcut;
41+
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
4142
import org.springframework.security.access.AccessDeniedException;
4243
import org.springframework.security.access.prepost.PreAuthorize;
4344
import org.springframework.security.authentication.TestAuthentication;
@@ -336,6 +337,32 @@ public void setTargetVisitorIgnoreValueTypesThenIgnores() {
336337
assertThat(factory.proxy(35)).isEqualTo(35);
337338
}
338339

340+
// gh-16819
341+
@Test
342+
void advisorsWhenWithDefaultsThenAreSorted() {
343+
AuthorizationAdvisorProxyFactory proxyFactory = AuthorizationAdvisorProxyFactory.withDefaults();
344+
AnnotationAwareOrderComparator comparator = AnnotationAwareOrderComparator.INSTANCE;
345+
AuthorizationAdvisor previous = null;
346+
for (AuthorizationAdvisor advisor : proxyFactory) {
347+
boolean ordered = previous == null || comparator.compare(previous, advisor) < 0;
348+
assertThat(ordered).isTrue();
349+
previous = advisor;
350+
}
351+
}
352+
353+
// gh-16819
354+
@Test
355+
void advisorsWhenWithReactiveDefaultsThenAreSorted() {
356+
AuthorizationAdvisorProxyFactory proxyFactory = AuthorizationAdvisorProxyFactory.withReactiveDefaults();
357+
AnnotationAwareOrderComparator comparator = AnnotationAwareOrderComparator.INSTANCE;
358+
AuthorizationAdvisor previous = null;
359+
for (AuthorizationAdvisor advisor : proxyFactory) {
360+
boolean ordered = previous == null || comparator.compare(previous, advisor) < 0;
361+
assertThat(ordered).isTrue();
362+
previous = advisor;
363+
}
364+
}
365+
339366
private Authentication authenticated(String user, String... authorities) {
340367
return TestAuthentication.authenticated(TestAuthentication.withUsername(user).authorities(authorities).build());
341368
}

0 commit comments

Comments
 (0)