|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2021 the original author or authors. |
|
|
|
* Copyright 2002-2023 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -18,6 +18,8 @@ package org.springframework.security.authorization.method; |
|
|
|
|
|
|
|
|
|
|
|
import java.lang.annotation.Retention; |
|
|
|
import java.lang.annotation.Retention; |
|
|
|
import java.lang.annotation.RetentionPolicy; |
|
|
|
import java.lang.annotation.RetentionPolicy; |
|
|
|
|
|
|
|
import java.util.Collection; |
|
|
|
|
|
|
|
import java.util.Set; |
|
|
|
import java.util.function.Supplier; |
|
|
|
import java.util.function.Supplier; |
|
|
|
|
|
|
|
|
|
|
|
import jakarta.annotation.security.DenyAll; |
|
|
|
import jakarta.annotation.security.DenyAll; |
|
|
|
@ -30,11 +32,14 @@ import org.springframework.security.access.intercept.method.MockMethodInvocation |
|
|
|
import org.springframework.security.authentication.TestAuthentication; |
|
|
|
import org.springframework.security.authentication.TestAuthentication; |
|
|
|
import org.springframework.security.authentication.TestingAuthenticationToken; |
|
|
|
import org.springframework.security.authentication.TestingAuthenticationToken; |
|
|
|
import org.springframework.security.authorization.AuthorizationDecision; |
|
|
|
import org.springframework.security.authorization.AuthorizationDecision; |
|
|
|
|
|
|
|
import org.springframework.security.authorization.AuthorizationManager; |
|
|
|
import org.springframework.security.core.Authentication; |
|
|
|
import org.springframework.security.core.Authentication; |
|
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
|
|
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
|
|
|
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
|
|
|
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
|
|
|
|
|
|
|
import static org.mockito.Mockito.mock; |
|
|
|
|
|
|
|
import static org.mockito.Mockito.verify; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Tests for {@link Jsr250AuthorizationManager}. |
|
|
|
* Tests for {@link Jsr250AuthorizationManager}. |
|
|
|
@ -63,6 +68,27 @@ public class Jsr250AuthorizationManagerTests { |
|
|
|
assertThat(manager).extracting("rolePrefix").isEqualTo("CUSTOM_"); |
|
|
|
assertThat(manager).extracting("rolePrefix").isEqualTo("CUSTOM_"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void setAuthoritiesAuthorizationManagerWhenNullThenException() { |
|
|
|
|
|
|
|
Jsr250AuthorizationManager manager = new Jsr250AuthorizationManager(); |
|
|
|
|
|
|
|
assertThatIllegalArgumentException().isThrownBy(() -> manager.setAuthoritiesAuthorizationManager(null)) |
|
|
|
|
|
|
|
.withMessage("authoritiesAuthorizationManager cannot be null"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void setAuthoritiesAuthorizationManagerWhenNotNullThenVerifyUsage() throws Exception { |
|
|
|
|
|
|
|
AuthorizationManager<Collection<String>> authoritiesAuthorizationManager = mock(AuthorizationManager.class); |
|
|
|
|
|
|
|
Jsr250AuthorizationManager manager = new Jsr250AuthorizationManager(); |
|
|
|
|
|
|
|
manager.setAuthoritiesAuthorizationManager(authoritiesAuthorizationManager); |
|
|
|
|
|
|
|
MockMethodInvocation methodInvocation = new MockMethodInvocation(new ClassLevelAnnotations(), |
|
|
|
|
|
|
|
ClassLevelAnnotations.class, "rolesAllowedAdmin"); |
|
|
|
|
|
|
|
Supplier<Authentication> authentication = () -> new TestingAuthenticationToken("user", "password", |
|
|
|
|
|
|
|
"ROLE_ADMIN"); |
|
|
|
|
|
|
|
AuthorizationDecision decision = manager.check(authentication, methodInvocation); |
|
|
|
|
|
|
|
assertThat(decision).isNull(); |
|
|
|
|
|
|
|
verify(authoritiesAuthorizationManager).check(authentication, Set.of("ROLE_ADMIN")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void checkDoSomethingWhenNoJsr250AnnotationsThenNullDecision() throws Exception { |
|
|
|
public void checkDoSomethingWhenNoJsr250AnnotationsThenNullDecision() throws Exception { |
|
|
|
MockMethodInvocation methodInvocation = new MockMethodInvocation(new TestClass(), TestClass.class, |
|
|
|
MockMethodInvocation methodInvocation = new MockMethodInvocation(new TestClass(), TestClass.class, |
|
|
|
@ -123,7 +149,7 @@ public class Jsr250AuthorizationManagerTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void checkMultipleAnnotationsWhenInvokedThenAnnotationConfigurationException() throws Exception { |
|
|
|
public void checkMultipleMethodAnnotationsWhenInvokedThenAnnotationConfigurationException() throws Exception { |
|
|
|
Supplier<Authentication> authentication = () -> new TestingAuthenticationToken("user", "password", |
|
|
|
Supplier<Authentication> authentication = () -> new TestingAuthenticationToken("user", "password", |
|
|
|
"ROLE_ANONYMOUS"); |
|
|
|
"ROLE_ANONYMOUS"); |
|
|
|
MockMethodInvocation methodInvocation = new MockMethodInvocation(new TestClass(), TestClass.class, |
|
|
|
MockMethodInvocation methodInvocation = new MockMethodInvocation(new TestClass(), TestClass.class, |
|
|
|
@ -133,6 +159,16 @@ public class Jsr250AuthorizationManagerTests { |
|
|
|
.isThrownBy(() -> manager.check(authentication, methodInvocation)); |
|
|
|
.isThrownBy(() -> manager.check(authentication, methodInvocation)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void checkMultipleClassAnnotationsWhenInvokedThenAnnotationConfigurationException() throws Exception { |
|
|
|
|
|
|
|
Supplier<Authentication> authentication = () -> new TestingAuthenticationToken("user", "password", "ROLE_USER"); |
|
|
|
|
|
|
|
MockMethodInvocation methodInvocation = new MockMethodInvocation(new ClassLevelIllegalAnnotations(), |
|
|
|
|
|
|
|
ClassLevelIllegalAnnotations.class, "inheritedAnnotations"); |
|
|
|
|
|
|
|
Jsr250AuthorizationManager manager = new Jsr250AuthorizationManager(); |
|
|
|
|
|
|
|
assertThatExceptionOfType(AnnotationConfigurationException.class) |
|
|
|
|
|
|
|
.isThrownBy(() -> manager.check(authentication, methodInvocation)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void checkRequiresAdminWhenClassAnnotationsThenMethodAnnotationsTakePrecedence() throws Exception { |
|
|
|
public void checkRequiresAdminWhenClassAnnotationsThenMethodAnnotationsTakePrecedence() throws Exception { |
|
|
|
Supplier<Authentication> authentication = () -> new TestingAuthenticationToken("user", "password", "ROLE_USER"); |
|
|
|
Supplier<Authentication> authentication = () -> new TestingAuthenticationToken("user", "password", "ROLE_USER"); |
|
|
|
@ -247,6 +283,15 @@ public class Jsr250AuthorizationManagerTests { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@MyIllegalRolesAllowed |
|
|
|
|
|
|
|
public static class ClassLevelIllegalAnnotations { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void inheritedAnnotations() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public interface InterfaceAnnotationsOne { |
|
|
|
public interface InterfaceAnnotationsOne { |
|
|
|
|
|
|
|
|
|
|
|
@RolesAllowed("ADMIN") |
|
|
|
@RolesAllowed("ADMIN") |
|
|
|
@ -274,4 +319,11 @@ public class Jsr250AuthorizationManagerTests { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@DenyAll |
|
|
|
|
|
|
|
@RolesAllowed("USER") |
|
|
|
|
|
|
|
@Retention(RetentionPolicy.RUNTIME) |
|
|
|
|
|
|
|
public @interface MyIllegalRolesAllowed { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|