|
21 | 21 | import java.lang.annotation.RetentionPolicy;
|
22 | 22 | import java.lang.annotation.Target;
|
23 | 23 | import java.lang.reflect.Method;
|
| 24 | +import java.util.function.Function; |
24 | 25 |
|
25 | 26 | import org.junit.After;
|
26 | 27 | import org.junit.Before;
|
27 | 28 | import org.junit.Test;
|
28 | 29 |
|
29 | 30 | import org.springframework.core.MethodParameter;
|
| 31 | +import org.springframework.expression.AccessException; |
| 32 | +import org.springframework.expression.BeanResolver; |
30 | 33 | import org.springframework.security.authentication.TestingAuthenticationToken;
|
31 | 34 | import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
32 | 35 | import org.springframework.security.core.authority.AuthorityUtils;
|
|
44 | 47 | */
|
45 | 48 | public class AuthenticationPrincipalArgumentResolverTests {
|
46 | 49 |
|
| 50 | + private final BeanResolver beanResolver = ((context, beanName) -> { |
| 51 | + if (!"test".equals(beanName)) { |
| 52 | + throw new AccessException("Could not resolve bean reference against BeanFactory"); |
| 53 | + } |
| 54 | + return (Function<CustomUserPrincipal, String>) (principal) -> principal.property; |
| 55 | + }); |
| 56 | + |
47 | 57 | private Object expectedPrincipal;
|
48 | 58 |
|
49 | 59 | private AuthenticationPrincipalArgumentResolver resolver;
|
50 | 60 |
|
51 | 61 | @Before
|
52 | 62 | public void setup() {
|
53 | 63 | this.resolver = new AuthenticationPrincipalArgumentResolver();
|
| 64 | + this.resolver.setBeanResolver(this.beanResolver); |
54 | 65 | }
|
55 | 66 |
|
56 | 67 | @After
|
@@ -127,6 +138,14 @@ public void resolveArgumentSpel() throws Exception {
|
127 | 138 | assertThat(this.resolver.resolveArgument(showUserSpel(), null, null, null)).isEqualTo(this.expectedPrincipal);
|
128 | 139 | }
|
129 | 140 |
|
| 141 | + @Test |
| 142 | + public void resolveArgumentSpelBean() throws Exception { |
| 143 | + CustomUserPrincipal principal = new CustomUserPrincipal(); |
| 144 | + setAuthenticationPrincipal(principal); |
| 145 | + this.expectedPrincipal = principal.property; |
| 146 | + assertThat(this.resolver.resolveArgument(showUserSpelBean(), null, null, null)).isEqualTo(this.expectedPrincipal); |
| 147 | + } |
| 148 | + |
130 | 149 | @Test
|
131 | 150 | public void resolveArgumentSpelCopy() throws Exception {
|
132 | 151 | CopyUserPrincipal principal = new CopyUserPrincipal("property");
|
@@ -195,6 +214,10 @@ private MethodParameter showUserSpel() {
|
195 | 214 | return getMethodParameter("showUserSpel", String.class);
|
196 | 215 | }
|
197 | 216 |
|
| 217 | + private MethodParameter showUserSpelBean() { |
| 218 | + return getMethodParameter("showUserSpelBean", String.class); |
| 219 | + } |
| 220 | + |
198 | 221 | private MethodParameter showUserSpelCopy() {
|
199 | 222 | return getMethodParameter("showUserSpelCopy", CopyUserPrincipal.class);
|
200 | 223 | }
|
@@ -258,6 +281,10 @@ public void showUserAnnotation(@AuthenticationPrincipal Object user) {
|
258 | 281 | public void showUserSpel(@AuthenticationPrincipal(expression = "property") String user) {
|
259 | 282 | }
|
260 | 283 |
|
| 284 | + public void showUserSpelBean(@AuthenticationPrincipal( |
| 285 | + expression = "@test.apply(#this)") String user) { |
| 286 | + } |
| 287 | + |
261 | 288 | public void showUserSpelCopy(@AuthenticationPrincipal(
|
262 | 289 | expression = "new org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolverTests$CopyUserPrincipal(#this)") CopyUserPrincipal user) {
|
263 | 290 | }
|
|
0 commit comments