4141import reactor .core .publisher .Mono ;
4242
4343import org .springframework .aop .Advisor ;
44+ import org .springframework .aop .framework .AopInfrastructureBean ;
4445import org .springframework .aop .framework .ProxyFactory ;
4546import org .springframework .core .annotation .AnnotationAwareOrderComparator ;
4647import org .springframework .lang .NonNull ;
7576 * @since 6.3
7677 */
7778public final class AuthorizationAdvisorProxyFactory
78- implements AuthorizationProxyFactory , Iterable <AuthorizationAdvisor > {
79+ implements AuthorizationProxyFactory , Iterable <AuthorizationAdvisor >, AopInfrastructureBean {
7980
8081 private static final boolean isReactivePresent = ClassUtils .isPresent ("reactor.core.publisher.Mono" , null );
8182
@@ -90,10 +91,18 @@ public final class AuthorizationAdvisorProxyFactory
9091
9192 private TargetVisitor visitor = DEFAULT_VISITOR ;
9293
93- private AuthorizationAdvisorProxyFactory (List <AuthorizationAdvisor > advisors ) {
94+ /**
95+ * Construct an {@link AuthorizationAdvisorProxyFactory} with the provided advisors.
96+ *
97+ * <p>
98+ * The list may be empty, in the case where advisors are added later using
99+ * {@link #addAdvisor}.
100+ * @param advisors the advisors to use
101+ * @since 6.4
102+ */
103+ public AuthorizationAdvisorProxyFactory (List <AuthorizationAdvisor > advisors ) {
94104 this .advisors = new ArrayList <>(advisors );
95- this .advisors .add (new AuthorizeReturnObjectMethodInterceptor (this ));
96- setAdvisors (this .advisors );
105+ AnnotationAwareOrderComparator .sort (this .advisors );
97106 }
98107
99108 /**
@@ -108,7 +117,9 @@ public static AuthorizationAdvisorProxyFactory withDefaults() {
108117 advisors .add (AuthorizationManagerAfterMethodInterceptor .postAuthorize ());
109118 advisors .add (new PreFilterAuthorizationMethodInterceptor ());
110119 advisors .add (new PostFilterAuthorizationMethodInterceptor ());
111- return new AuthorizationAdvisorProxyFactory (advisors );
120+ AuthorizationAdvisorProxyFactory proxyFactory = new AuthorizationAdvisorProxyFactory (advisors );
121+ proxyFactory .addAdvisor (new AuthorizeReturnObjectMethodInterceptor (proxyFactory ));
122+ return proxyFactory ;
112123 }
113124
114125 /**
@@ -123,7 +134,9 @@ public static AuthorizationAdvisorProxyFactory withReactiveDefaults() {
123134 advisors .add (AuthorizationManagerAfterReactiveMethodInterceptor .postAuthorize ());
124135 advisors .add (new PreFilterAuthorizationReactiveMethodInterceptor ());
125136 advisors .add (new PostFilterAuthorizationReactiveMethodInterceptor ());
126- return new AuthorizationAdvisorProxyFactory (advisors );
137+ AuthorizationAdvisorProxyFactory proxyFactory = new AuthorizationAdvisorProxyFactory (advisors );
138+ proxyFactory .addAdvisor (new AuthorizeReturnObjectMethodInterceptor (proxyFactory ));
139+ return proxyFactory ;
127140 }
128141
129142 /**
@@ -167,7 +180,9 @@ public Object proxy(Object target) {
167180 * <p>
168181 * All advisors are re-sorted by their advisor order.
169182 * @param advisors the advisors to add
183+ * @deprecated Please use {@link #addAdvisor} instead
170184 */
185+ @ Deprecated
171186 public void setAdvisors (AuthorizationAdvisor ... advisors ) {
172187 this .advisors = new ArrayList <>(List .of (advisors ));
173188 AnnotationAwareOrderComparator .sort (this .advisors );
@@ -179,12 +194,30 @@ public void setAdvisors(AuthorizationAdvisor... advisors) {
179194 * <p>
180195 * All advisors are re-sorted by their advisor order.
181196 * @param advisors the advisors to add
197+ * @deprecated Please use {@link #addAdvisor} instead
182198 */
199+ @ Deprecated
183200 public void setAdvisors (Collection <AuthorizationAdvisor > advisors ) {
184201 this .advisors = new ArrayList <>(advisors );
185202 AnnotationAwareOrderComparator .sort (this .advisors );
186203 }
187204
205+ /**
206+ * Add an advisor that should be included to each proxy created.
207+ *
208+ * <p>
209+ * This method sorts the advisors based on the order in
210+ * {@link AuthorizationAdvisor#getOrder}. You can use the values in
211+ * {@link AuthorizationInterceptorsOrder}to ensure advisors are located where you need
212+ * them.
213+ * @param advisor
214+ * @since 6.4
215+ */
216+ public void addAdvisor (AuthorizationAdvisor advisor ) {
217+ this .advisors .add (advisor );
218+ AnnotationAwareOrderComparator .sort (this .advisors );
219+ }
220+
188221 /**
189222 * Use this visitor to navigate the proxy target's hierarchy.
190223 *
0 commit comments