Skip to content

Commit 141c79b

Browse files
committed
Default resolution of non-annotated Principal argument
Closes gh-25981
1 parent 885a504 commit 141c79b

File tree

4 files changed

+182
-1
lines changed

4 files changed

+182
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2002-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.web.servlet.mvc.method.annotation;
17+
18+
import java.security.Principal;
19+
20+
import javax.servlet.http.HttpServletRequest;
21+
22+
import org.springframework.core.MethodParameter;
23+
import org.springframework.lang.Nullable;
24+
import org.springframework.web.bind.support.WebDataBinderFactory;
25+
import org.springframework.web.context.request.NativeWebRequest;
26+
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
27+
import org.springframework.web.method.support.ModelAndViewContainer;
28+
29+
/**
30+
* Resolves an argument of type {@link Principal}, similar to
31+
* {@link ServletRequestMethodArgumentResolver} but irrespective of whether the
32+
* argument is annotated or not. This is doen to enable custom argument
33+
* resolution of a {@link Principal} argument (with custom annotation).
34+
*
35+
* @author Rossen Stoyanchev
36+
* @since 5.3.1
37+
*/
38+
public class PrincipalMethodArgumentResolver implements HandlerMethodArgumentResolver {
39+
40+
41+
@Override
42+
public boolean supportsParameter(MethodParameter parameter) {
43+
Class<?> paramType = parameter.getParameterType();
44+
return Principal.class.isAssignableFrom(paramType);
45+
}
46+
47+
@Override
48+
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
49+
NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {
50+
51+
Class<?> paramType = parameter.getParameterType();
52+
53+
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
54+
if (request == null) {
55+
throw new IllegalStateException(
56+
"Current request is not of type [HttpServletRequest]: " + webRequest);
57+
}
58+
59+
Principal principal = request.getUserPrincipal();
60+
if (principal != null && !paramType.isInstance(principal)) {
61+
throw new IllegalStateException(
62+
"Current user principal is not of type [" + paramType.getName() + "]: " + principal);
63+
}
64+
65+
return principal;
66+
}
67+
68+
}

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java

+1
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,7 @@ private List<HandlerMethodArgumentResolver> getDefaultInitBinderArgumentResolver
715715
}
716716

717717
// Catch-all
718+
resolvers.add(new PrincipalMethodArgumentResolver());
718719
resolvers.add(new RequestParamMethodArgumentResolver(getBeanFactory(), true));
719720

720721
return resolvers;

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletRequestMethodArgumentResolver.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@
5050
* <li>{@link MultipartRequest}
5151
* <li>{@link HttpSession}
5252
* <li>{@link PushBuilder} (as of Spring 5.0 on Servlet 4.0)
53-
* <li>{@link Principal}
53+
* <li>{@link Principal} but only if not annotated in order to allow custom
54+
* resolvers to resolve it, and the falling back on
55+
* {@link PrincipalMethodArgumentResolver}.
5456
* <li>{@link InputStream}
5557
* <li>{@link Reader}
5658
* <li>{@link HttpMethod} (as of Spring 4.0)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright 2002-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.servlet.mvc.method.annotation;
18+
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
import java.lang.reflect.Method;
24+
import java.security.Principal;
25+
26+
import javax.servlet.ServletRequest;
27+
28+
import org.junit.jupiter.api.BeforeEach;
29+
import org.junit.jupiter.api.Test;
30+
31+
import org.springframework.core.MethodParameter;
32+
import org.springframework.web.context.request.ServletWebRequest;
33+
import org.springframework.web.method.support.ModelAndViewContainer;
34+
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
35+
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
36+
37+
import static org.assertj.core.api.Assertions.assertThat;
38+
39+
/**
40+
* Unit tests for {@link PrincipalMethodArgumentResolver}.
41+
*
42+
* @author Rossen Stoyanchev
43+
*/
44+
public class PrincipalMethodArgumentResolverTests {
45+
46+
private PrincipalMethodArgumentResolver resolver;
47+
48+
private ModelAndViewContainer mavContainer;
49+
50+
private MockHttpServletRequest servletRequest;
51+
52+
private ServletWebRequest webRequest;
53+
54+
private Method method;
55+
56+
57+
@BeforeEach
58+
public void setup() throws Exception {
59+
resolver = new PrincipalMethodArgumentResolver();
60+
mavContainer = new ModelAndViewContainer();
61+
servletRequest = new MockHttpServletRequest("GET", "");
62+
webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse());
63+
64+
method = getClass().getMethod("supportedParams", ServletRequest.class, Principal.class);
65+
}
66+
67+
68+
@Test
69+
public void principal() throws Exception {
70+
Principal principal = () -> "Foo";
71+
servletRequest.setUserPrincipal(principal);
72+
73+
MethodParameter principalParameter = new MethodParameter(method, 1);
74+
assertThat(resolver.supportsParameter(principalParameter)).as("Principal not supported").isTrue();
75+
76+
Object result = resolver.resolveArgument(principalParameter, null, webRequest, null);
77+
assertThat(result).as("Invalid result").isSameAs(principal);
78+
}
79+
80+
@Test
81+
public void principalAsNull() throws Exception {
82+
MethodParameter principalParameter = new MethodParameter(method, 1);
83+
assertThat(resolver.supportsParameter(principalParameter)).as("Principal not supported").isTrue();
84+
85+
Object result = resolver.resolveArgument(principalParameter, null, webRequest, null);
86+
assertThat(result).as("Invalid result").isNull();
87+
}
88+
89+
@Test // gh-25780
90+
public void annotatedPrincipal() throws Exception {
91+
Principal principal = () -> "Foo";
92+
servletRequest.setUserPrincipal(principal);
93+
Method principalMethod = getClass().getMethod("supportedParamsWithAnnotatedPrincipal", Principal.class);
94+
95+
MethodParameter principalParameter = new MethodParameter(principalMethod, 0);
96+
assertThat(resolver.supportsParameter(principalParameter)).isTrue();
97+
}
98+
99+
100+
@SuppressWarnings("unused")
101+
public void supportedParams(ServletRequest p0, Principal p1) {}
102+
103+
@Target({ ElementType.PARAMETER })
104+
@Retention(RetentionPolicy.RUNTIME)
105+
public @interface AuthenticationPrincipal {}
106+
107+
@SuppressWarnings("unused")
108+
public void supportedParamsWithAnnotatedPrincipal(@AuthenticationPrincipal Principal p) {}
109+
110+
}

0 commit comments

Comments
 (0)