|
18 | 18 |
|
19 | 19 | import java.io.InputStream;
|
20 | 20 | import java.io.Reader;
|
| 21 | +import java.lang.annotation.ElementType; |
| 22 | +import java.lang.annotation.Retention; |
| 23 | +import java.lang.annotation.RetentionPolicy; |
| 24 | +import java.lang.annotation.Target; |
21 | 25 | import java.lang.reflect.Method;
|
22 | 26 | import java.security.Principal;
|
23 | 27 | import java.time.ZoneId;
|
@@ -122,6 +126,19 @@ public void principalAsNull() throws Exception {
|
122 | 126 | assertThat(result).as("Invalid result").isNull();
|
123 | 127 | }
|
124 | 128 |
|
| 129 | + // spring-security already provides the @AuthenticationPrincipal annotation to inject the Principal taken from SecurityContext.getAuthentication.getPrincipal() |
| 130 | + // but ServletRequestMethodArgumentResolver used to take precedence over @AuthenticationPrincipal resolver org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolver |
| 131 | + // and we used to get the wrong Principal in methods. See https://github.com/spring-projects/spring-framework/pull/25780 |
| 132 | + @Test |
| 133 | + public void annotatedPrincipal() throws Exception { |
| 134 | + Principal principal = () -> "Foo"; |
| 135 | + servletRequest.setUserPrincipal(principal); |
| 136 | + Method principalMethod = getClass().getMethod("supportedParamsWithAnnotatedPrincipal", Principal.class); |
| 137 | + |
| 138 | + MethodParameter principalParameter = new MethodParameter(principalMethod, 0); |
| 139 | + assertThat(resolver.supportsParameter(principalParameter)).as("Principal not supported").isFalse(); |
| 140 | + } |
| 141 | + |
125 | 142 | @Test
|
126 | 143 | public void locale() throws Exception {
|
127 | 144 | Locale locale = Locale.ENGLISH;
|
@@ -245,6 +262,14 @@ public PushBuilder newPushBuilder() {
|
245 | 262 | assertThat(result).as("Invalid result").isSameAs(pushBuilder);
|
246 | 263 | }
|
247 | 264 |
|
| 265 | + @Target({ ElementType.PARAMETER }) |
| 266 | + @Retention(RetentionPolicy.RUNTIME) |
| 267 | + public @interface PlaceHolder {} |
| 268 | + |
| 269 | + @SuppressWarnings("unused") |
| 270 | + public void supportedParamsWithAnnotatedPrincipal(@PlaceHolder Principal p) { |
| 271 | + |
| 272 | + } |
248 | 273 |
|
249 | 274 | @SuppressWarnings("unused")
|
250 | 275 | public void supportedParams(ServletRequest p0,
|
|
0 commit comments