Skip to content

Commit fce2eb1

Browse files
committed
Add AuthorizationProxy Interface
Closes gh-15747
1 parent 4855287 commit fce2eb1

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.security.authorization.method;
1818

1919
import java.lang.reflect.Array;
20+
import java.lang.reflect.Method;
2021
import java.lang.reflect.Modifier;
2122
import java.util.ArrayList;
2223
import java.util.Collection;
@@ -37,10 +38,13 @@
3738
import java.util.function.Supplier;
3839
import java.util.stream.Stream;
3940

41+
import org.aopalliance.aop.Advice;
42+
import org.aopalliance.intercept.MethodInvocation;
4043
import reactor.core.publisher.Flux;
4144
import reactor.core.publisher.Mono;
4245

4346
import org.springframework.aop.Advisor;
47+
import org.springframework.aop.Pointcut;
4448
import org.springframework.aop.framework.AopInfrastructureBean;
4549
import org.springframework.aop.framework.ProxyFactory;
4650
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
@@ -168,9 +172,12 @@ public Object proxy(Object target) {
168172
return proxied;
169173
}
170174
ProxyFactory factory = new ProxyFactory(target);
175+
AuthorizationProxyMethodInterceptor unwrapper = new AuthorizationProxyMethodInterceptor();
176+
factory.addAdvisors(unwrapper);
171177
for (Advisor advisor : this.advisors) {
172178
factory.addAdvisors(advisor);
173179
}
180+
factory.addInterface(AuthorizationProxy.class);
174181
factory.setOpaque(true);
175182
factory.setProxyTargetClass(!Modifier.isFinal(target.getClass().getModifiers()));
176183
return factory.getProxy();
@@ -572,4 +579,34 @@ private Flux<?> proxyFlux(AuthorizationProxyFactory proxyFactory, Flux<?> flux)
572579

573580
}
574581

582+
private static final class AuthorizationProxyMethodInterceptor implements AuthorizationAdvisor {
583+
584+
private static final Method GET_TARGET_METHOD = ClassUtils.getMethod(AuthorizationProxy.class,
585+
"toAuthorizedTarget");
586+
587+
@Override
588+
public Object invoke(MethodInvocation invocation) throws Throwable {
589+
if (invocation.getMethod().equals(GET_TARGET_METHOD)) {
590+
return invocation.getThis();
591+
}
592+
return invocation.proceed();
593+
}
594+
595+
@Override
596+
public Pointcut getPointcut() {
597+
return Pointcut.TRUE;
598+
}
599+
600+
@Override
601+
public Advice getAdvice() {
602+
return this;
603+
}
604+
605+
@Override
606+
public int getOrder() {
607+
return 0;
608+
}
609+
610+
}
611+
575612
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.authorization.method;
18+
19+
import org.springframework.aop.RawTargetAccess;
20+
21+
/**
22+
* An interface that is typically implemented by Spring Security's AOP support to identify
23+
* an instance as being proxied by Spring Security.
24+
*
25+
* <p>
26+
* Also provides a way to access the underlying target object, handy for working with the
27+
* object without invoking the authorization rules.
28+
*
29+
* <p>
30+
* This can be helpful when taking working with a proxied object and needing to hand it to
31+
* a layer of the application that should not invoke the rules, like a Spring Data
32+
* repository:
33+
*
34+
* <pre>
35+
* MyObject object = this.objectRepository.findById(123L); // now an authorized proxy
36+
* object.setProtectedValue(...); // only works if authorized
37+
* if (object instanceof AuthorizationProxy proxy) {
38+
* // Spring Data wants to be able to persist the entire object
39+
* // so we'll remove the proxy
40+
* object = (MyObject) proxy.toAuthorizedTarget();
41+
* }
42+
* this.objectRepository.save(object);
43+
* </pre>
44+
*
45+
* @author Josh Cummings
46+
* @since 6.4
47+
*/
48+
public interface AuthorizationProxy extends RawTargetAccess {
49+
50+
/**
51+
* Access underlying target object
52+
* @return the target object
53+
*/
54+
Object toAuthorizedTarget();
55+
56+
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.springframework.security.authorization.method.AuthorizationAdvisor;
4747
import org.springframework.security.authorization.method.AuthorizationAdvisorProxyFactory;
4848
import org.springframework.security.authorization.method.AuthorizationAdvisorProxyFactory.TargetVisitor;
49+
import org.springframework.security.authorization.method.AuthorizationProxy;
4950
import org.springframework.security.core.Authentication;
5051
import org.springframework.security.core.context.SecurityContextHolder;
5152

@@ -350,6 +351,15 @@ public void serializeWhenAuthorizationProxyObjectThenOnlyIncludesProxiedProperti
350351
assertThat(properties).hasSize(3).containsKeys("id", "firstName", "lastName");
351352
}
352353

354+
@Test
355+
public void proxyWhenDefaultsThenInstanceOfAuthorizationProxy() {
356+
AuthorizationAdvisorProxyFactory factory = AuthorizationAdvisorProxyFactory.withDefaults();
357+
Flight flight = proxy(factory, this.flight);
358+
assertThat(flight).isInstanceOf(AuthorizationProxy.class);
359+
Flight target = (Flight) ((AuthorizationProxy) flight).toAuthorizedTarget();
360+
assertThat(target).isSameAs(this.flight);
361+
}
362+
353363
private Authentication authenticated(String user, String... authorities) {
354364
return TestAuthentication.authenticated(TestAuthentication.withUsername(user).authorities(authorities).build());
355365
}

0 commit comments

Comments
 (0)