Skip to content

Commit

Permalink
Add Supplier Support
Browse files Browse the repository at this point in the history
  • Loading branch information
jzheaux committed Mar 21, 2024
1 parent c622edc commit 776c8d6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.function.Supplier;
import java.util.stream.Stream;

import reactor.core.publisher.Flux;
Expand Down Expand Up @@ -91,7 +92,7 @@ public final class AuthorizationAdvisorProxyFactory
/**
* The default {@link TargetVisitor}, which will proxy {@link Class} instances as well
* as instances contained in reactive types (if reactor is present), collection types,
* and other container types like {@link Optional}
* and other container types like {@link Optional} and {@link Supplier}
*/
public static final TargetVisitor DEFAULT_VISITOR = isReactivePresent
? new DelegateVisitor(new ClassVisitor(), new ReactiveTypeVisitor(), new ContainerTypeVisitor())
Expand Down Expand Up @@ -330,6 +331,9 @@ public Object visit(AuthorizationAdvisorProxyFactory proxyFactory, Object target
if (target instanceof Optional<?> optional) {
return proxyOptional(proxyFactory, optional);
}
if (target instanceof Supplier<?> supplier) {
return proxySupplier(proxyFactory, supplier);
}
return null;
}

Expand Down Expand Up @@ -462,6 +466,10 @@ private Optional<?> proxyOptional(AuthorizationProxyFactory proxyFactory, Option
return optional.map(proxyFactory::proxy);
}

private Supplier<?> proxySupplier(AuthorizationProxyFactory proxyFactory, Supplier<?> supplier) {
return () -> proxyFactory.proxy(supplier.get());
}

}

private static class ReactiveTypeVisitor implements TargetVisitor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.function.Supplier;
import java.util.stream.Stream;

import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -242,6 +243,17 @@ public void proxyWhenPreAuthorizeForOptionalThenHonors() {
SecurityContextHolder.clearContext();
}

@Test
public void proxyWhenPreAuthorizeForSupplierThenHonors() {
SecurityContextHolder.getContext().setAuthentication(this.user);
AuthorizationAdvisorProxyFactory factory = AuthorizationAdvisorProxyFactory.withDefaults();
Supplier<Flight> flights = () -> this.flight;
assertThat(flights.get().getAltitude()).isEqualTo(35000d);
Supplier<Flight> secured = proxy(factory, flights);
assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(() -> secured.get().getAltitude());
SecurityContextHolder.clearContext();
}

@Test
public void proxyWhenPreAuthorizeForStreamThenHonors() {
SecurityContextHolder.getContext().setAuthentication(this.user);
Expand Down

0 comments on commit 776c8d6

Please sign in to comment.