|
@@ -23,8 +23,10 @@ import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
|
+import org.aopalliance.intercept.MethodInvocation;
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
+import org.springframework.context.support.GenericApplicationContext;
|
|
|
import org.springframework.core.annotation.AnnotationConfigurationException;
|
|
|
import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler;
|
|
|
import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler;
|
|
@@ -33,6 +35,7 @@ import org.springframework.security.access.prepost.PostAuthorize;
|
|
|
import org.springframework.security.authentication.TestAuthentication;
|
|
|
import org.springframework.security.authentication.TestingAuthenticationToken;
|
|
|
import org.springframework.security.authorization.AuthorizationDecision;
|
|
|
+import org.springframework.security.authorization.AuthorizationResult;
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
@@ -156,6 +159,34 @@ public class PostAuthorizeAuthorizationManagerTests {
|
|
|
.isThrownBy(() -> manager.check(authentication, result));
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void checkWhenHandlerDeniedNoApplicationContextThenReflectivelyConstructs() throws Exception {
|
|
|
+ PostAuthorizeAuthorizationManager manager = new PostAuthorizeAuthorizationManager();
|
|
|
+ assertThat(handleDeniedInvocationResult("methodOne", manager)).isNull();
|
|
|
+ assertThatExceptionOfType(IllegalArgumentException.class)
|
|
|
+ .isThrownBy(() -> handleDeniedInvocationResult("methodTwo", manager));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void checkWhenHandlerDeniedApplicationContextThenLooksForBean() throws Exception {
|
|
|
+ GenericApplicationContext context = new GenericApplicationContext();
|
|
|
+ context.registerBean(NoDefaultConstructorHandler.class, () -> new NoDefaultConstructorHandler(new Object()));
|
|
|
+ context.refresh();
|
|
|
+ PostAuthorizeAuthorizationManager manager = new PostAuthorizeAuthorizationManager();
|
|
|
+ manager.setApplicationContext(context);
|
|
|
+ assertThat(handleDeniedInvocationResult("methodTwo", manager)).isNull();
|
|
|
+ assertThatExceptionOfType(IllegalStateException.class)
|
|
|
+ .isThrownBy(() -> handleDeniedInvocationResult("methodOne", manager));
|
|
|
+ }
|
|
|
+
|
|
|
+ private Object handleDeniedInvocationResult(String methodName, PostAuthorizeAuthorizationManager manager)
|
|
|
+ throws Exception {
|
|
|
+ MethodInvocation invocation = new MockMethodInvocation(new UsingHandleDeniedAuthorization(),
|
|
|
+ UsingHandleDeniedAuthorization.class, methodName);
|
|
|
+ MethodInvocationResult result = new MethodInvocationResult(invocation, null);
|
|
|
+ return manager.handleDeniedInvocationResult(result, null);
|
|
|
+ }
|
|
|
+
|
|
|
public static class TestClass implements InterfaceAnnotationsOne, InterfaceAnnotationsTwo {
|
|
|
|
|
|
public void doSomething() {
|
|
@@ -234,4 +265,44 @@ public class PostAuthorizeAuthorizationManagerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ public static final class UsingHandleDeniedAuthorization {
|
|
|
+
|
|
|
+ @HandleAuthorizationDenied(handlerClass = NullHandler.class)
|
|
|
+ @PostAuthorize("denyAll()")
|
|
|
+ public String methodOne() {
|
|
|
+ return "ok";
|
|
|
+ }
|
|
|
+
|
|
|
+ @HandleAuthorizationDenied(handlerClass = NoDefaultConstructorHandler.class)
|
|
|
+ @PostAuthorize("denyAll()")
|
|
|
+ public String methodTwo() {
|
|
|
+ return "ok";
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final class NullHandler implements MethodAuthorizationDeniedHandler {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object handleDeniedInvocation(MethodInvocation methodInvocation,
|
|
|
+ AuthorizationResult authorizationResult) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final class NoDefaultConstructorHandler implements MethodAuthorizationDeniedHandler {
|
|
|
+
|
|
|
+ private NoDefaultConstructorHandler(Object parameter) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object handleDeniedInvocation(MethodInvocation methodInvocation,
|
|
|
+ AuthorizationResult authorizationResult) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|