|
@@ -21,12 +21,15 @@ import java.lang.annotation.Retention;
|
|
|
import java.lang.annotation.RetentionPolicy;
|
|
|
import java.lang.annotation.Target;
|
|
|
import java.lang.reflect.Method;
|
|
|
+import java.util.function.Function;
|
|
|
|
|
|
import org.junit.After;
|
|
|
import org.junit.Before;
|
|
|
import org.junit.Test;
|
|
|
|
|
|
import org.springframework.core.MethodParameter;
|
|
|
+import org.springframework.expression.AccessException;
|
|
|
+import org.springframework.expression.BeanResolver;
|
|
|
import org.springframework.security.authentication.TestingAuthenticationToken;
|
|
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
|
|
import org.springframework.security.core.authority.AuthorityUtils;
|
|
@@ -44,6 +47,13 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
|
|
*/
|
|
|
public class AuthenticationPrincipalArgumentResolverTests {
|
|
|
|
|
|
+ private final BeanResolver beanResolver = ((context, beanName) -> {
|
|
|
+ if (!"test".equals(beanName)) {
|
|
|
+ throw new AccessException("Could not resolve bean reference against BeanFactory");
|
|
|
+ }
|
|
|
+ return (Function<CustomUserPrincipal, String>) (principal) -> principal.property;
|
|
|
+ });
|
|
|
+
|
|
|
private Object expectedPrincipal;
|
|
|
|
|
|
private AuthenticationPrincipalArgumentResolver resolver;
|
|
@@ -51,6 +61,7 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
|
|
@Before
|
|
|
public void setup() {
|
|
|
this.resolver = new AuthenticationPrincipalArgumentResolver();
|
|
|
+ this.resolver.setBeanResolver(this.beanResolver);
|
|
|
}
|
|
|
|
|
|
@After
|
|
@@ -127,6 +138,14 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
|
|
assertThat(this.resolver.resolveArgument(showUserSpel(), null, null, null)).isEqualTo(this.expectedPrincipal);
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void resolveArgumentSpelBean() throws Exception {
|
|
|
+ CustomUserPrincipal principal = new CustomUserPrincipal();
|
|
|
+ setAuthenticationPrincipal(principal);
|
|
|
+ this.expectedPrincipal = principal.property;
|
|
|
+ assertThat(this.resolver.resolveArgument(showUserSpelBean(), null, null, null)).isEqualTo(this.expectedPrincipal);
|
|
|
+ }
|
|
|
+
|
|
|
@Test
|
|
|
public void resolveArgumentSpelCopy() throws Exception {
|
|
|
CopyUserPrincipal principal = new CopyUserPrincipal("property");
|
|
@@ -195,6 +214,10 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
|
|
return getMethodParameter("showUserSpel", String.class);
|
|
|
}
|
|
|
|
|
|
+ private MethodParameter showUserSpelBean() {
|
|
|
+ return getMethodParameter("showUserSpelBean", String.class);
|
|
|
+ }
|
|
|
+
|
|
|
private MethodParameter showUserSpelCopy() {
|
|
|
return getMethodParameter("showUserSpelCopy", CopyUserPrincipal.class);
|
|
|
}
|
|
@@ -258,6 +281,10 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
|
|
public void showUserSpel(@AuthenticationPrincipal(expression = "property") String user) {
|
|
|
}
|
|
|
|
|
|
+ public void showUserSpelBean(@AuthenticationPrincipal(
|
|
|
+ expression = "@test.apply(#this)") String user) {
|
|
|
+ }
|
|
|
+
|
|
|
public void showUserSpelCopy(@AuthenticationPrincipal(
|
|
|
expression = "new org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolverTests$CopyUserPrincipal(#this)") CopyUserPrincipal user) {
|
|
|
}
|