Class AuthorizationAdvisorProxyFactory
- All Implemented Interfaces:
Iterable<AuthorizationAdvisor>,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.beans.factory.SmartInitializingSingleton,AuthorizationProxyFactory
For example, consider a non-Spring-managed object Foo:
class Foo {
@PreAuthorize("hasAuthority('bar:read')")
String bar() { ... }
}
Use AuthorizationAdvisorProxyFactory to wrap the instance in Spring Security's
PreAuthorize method interceptor
like so:
AuthorizationProxyFactory proxyFactory = AuthorizationAdvisorProxyFactory.withDefaults();
Foo foo = new Foo();
foo.bar(); // passes
Foo securedFoo = proxyFactory.proxy(foo);
securedFoo.bar(); // access denied!
- Since:
- 6.3
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceAn interface to handle how theAuthorizationAdvisorProxyFactoryshould step through the target's object hierarchy. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAdvisor(AuthorizationAdvisor advisor) Deprecated.please provide all advisors in the constructorvoiditerator()<T> @Nullable Tproxy(@Nullable T target) Proxy an object to enforce authorization advice.voidsetAdvisors(Collection<AuthorizationAdvisor> advisors) Deprecated.voidsetAdvisors(AuthorizationAdvisor... advisors) Deprecated.voidUse this visitor to navigate the proxy target's hierarchy.Construct anAuthorizationAdvisorProxyFactorywith the defaults needed for wrapping objects in Spring Security's pre-post method security support.Construct anAuthorizationAdvisorProxyFactorywith the defaults needed for wrapping objects in Spring Security's pre-post reactive method security support.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
AuthorizationAdvisorProxyFactory
Construct anAuthorizationAdvisorProxyFactorywith the provided advisors.- Parameters:
advisors- the advisors to use- Since:
- 6.4
-
-
Method Details
-
withDefaults
Construct anAuthorizationAdvisorProxyFactorywith the defaults needed for wrapping objects in Spring Security's pre-post method security support.- Returns:
- an
AuthorizationAdvisorProxyFactoryfor adding pre-post method security support
-
withReactiveDefaults
Construct anAuthorizationAdvisorProxyFactorywith the defaults needed for wrapping objects in Spring Security's pre-post reactive method security support.- Returns:
- an
AuthorizationAdvisorProxyFactoryfor adding pre-post reactive method security support
-
afterSingletonsInstantiated
public void afterSingletonsInstantiated()- Specified by:
afterSingletonsInstantiatedin interfaceorg.springframework.beans.factory.SmartInitializingSingleton
-
proxy
public <T> @Nullable T proxy(@Nullable T target) Proxy an object to enforce authorization advice.Proxies any instance of a non-final class or a class that implements more than one interface.
If
targetis anIterator,Collection,Array,Map,Stream, orOptional, then the element or value type is proxied.If
targetis aClass, thenProxyFactory.getProxyClass(java.lang.ClassLoader)is invoked instead.- Specified by:
proxyin interfaceAuthorizationProxyFactory- Type Parameters:
T- the type of the object being proxied- Parameters:
target- the instance to proxy- Returns:
- the proxied instance
-
setAdvisors
Deprecated.Add advisors that should be included to each proxy created.All advisors are re-sorted by their advisor order.
- Parameters:
advisors- the advisors to add
-
setAdvisors
Deprecated.Add advisors that should be included to each proxy created.All advisors are re-sorted by their advisor order.
- Parameters:
advisors- the advisors to add
-
addAdvisor
Deprecated.please provide all advisors in the constructorAdd an advisor that should be included to each proxy created.This method sorts the advisors based on the order in
Ordered.getOrder(). You can use the values inAuthorizationInterceptorsOrderto ensure advisors are located where you need them.- Parameters:
advisor-- Since:
- 6.4
-
setTargetVisitor
Use this visitor to navigate the proxy target's hierarchy.This can be helpful when you want a specialized behavior for a type or set of types. For example, if you want to have this factory skip primitives and wrappers, then you can do:
AuthorizationAdvisorProxyFactory proxyFactory = new AuthorizationAdvisorProxyFactory(); proxyFactory.setTargetVisitor(TargetVisitor.defaultsSkipValueTypes());The default
AuthorizationAdvisorProxyFactory.TargetVisitorproxiesClassinstances as well as instances contained in reactive types (if reactor is present), collection types, and other container types likeOptionalandSupplier.If you want to add support for another container type, you can do so in the following way:
TargetVisitor functions = (factory, target) -> { if (target instanceof Function function) { return (input) -> factory.proxy(function.apply(input)); } return null; }; AuthorizationAdvisorProxyFactory proxyFactory = new AuthorizationAdvisorProxyFactory(); proxyFactory.setTargetVisitor(TargetVisitor.of(functions, TargetVisitor.defaultsSkipValueTypes()));- Parameters:
visitor- the visitor to use to introduce specialized behavior for a type- See Also:
-
iterator
- Specified by:
iteratorin interfaceIterable<AuthorizationAdvisor>
-