Skip to content

Commit 6014ca9

Browse files
committed
ServletRequestMethodArgumentResolver validates argument type match
Issue: SPR-15214 (cherry picked from commit e44533f)
1 parent d401057 commit 6014ca9

File tree

1 file changed

+44
-21
lines changed

1 file changed

+44
-21
lines changed

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

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,7 +31,6 @@
3131
import org.springframework.lang.UsesJava8;
3232
import org.springframework.web.bind.support.WebDataBinderFactory;
3333
import org.springframework.web.context.request.NativeWebRequest;
34-
import org.springframework.web.context.request.ServletWebRequest;
3534
import org.springframework.web.context.request.WebRequest;
3635
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
3736
import org.springframework.web.method.support.ModelAndViewContainer;
@@ -46,12 +45,12 @@
4645
* <li>{@link MultipartRequest}
4746
* <li>{@link HttpSession}
4847
* <li>{@link Principal}
48+
* <li>{@link InputStream}
49+
* <li>{@link Reader}
50+
* <li>{@link HttpMethod} (as of Spring 4.0)</li>
4951
* <li>{@link Locale}
5052
* <li>{@link TimeZone} (as of Spring 4.0)
5153
* <li>{@link java.time.ZoneId} (as of Spring 4.0 and Java 8)</li>
52-
* <li>{@link InputStream}
53-
* <li>{@link Reader}
54-
* <li>{@link org.springframework.http.HttpMethod} (as of Spring 4.0)</li>
5554
* </ul>
5655
*
5756
* @author Arjen Poutsma
@@ -69,12 +68,12 @@ public boolean supportsParameter(MethodParameter parameter) {
6968
MultipartRequest.class.isAssignableFrom(paramType) ||
7069
HttpSession.class.isAssignableFrom(paramType) ||
7170
Principal.class.isAssignableFrom(paramType) ||
72-
Locale.class == paramType ||
73-
TimeZone.class == paramType ||
74-
"java.time.ZoneId".equals(paramType.getName()) ||
7571
InputStream.class.isAssignableFrom(paramType) ||
7672
Reader.class.isAssignableFrom(paramType) ||
77-
HttpMethod.class == paramType);
73+
HttpMethod.class == paramType ||
74+
Locale.class == paramType ||
75+
TimeZone.class == paramType ||
76+
"java.time.ZoneId".equals(paramType.getName()));
7877
}
7978

8079
@Override
@@ -83,6 +82,10 @@ public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer m
8382

8483
Class<?> paramType = parameter.getParameterType();
8584
if (WebRequest.class.isAssignableFrom(paramType)) {
85+
if (!paramType.isInstance(webRequest)) {
86+
throw new IllegalStateException(
87+
"Current request is not of type [" + paramType.getName() + "]: " + webRequest);
88+
}
8689
return webRequest;
8790
}
8891

@@ -96,13 +99,39 @@ public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer m
9699
return nativeRequest;
97100
}
98101
else if (HttpSession.class.isAssignableFrom(paramType)) {
99-
return request.getSession();
102+
HttpSession session = request.getSession();
103+
if (!paramType.isInstance(session)) {
104+
throw new IllegalStateException(
105+
"Current session is not of type [" + paramType.getName() + "]: " + session);
106+
}
107+
return session;
100108
}
101-
else if (HttpMethod.class == paramType) {
102-
return ((ServletWebRequest) webRequest).getHttpMethod();
109+
else if (InputStream.class.isAssignableFrom(paramType)) {
110+
InputStream inputStream = request.getInputStream();
111+
if (!paramType.isInstance(inputStream)) {
112+
throw new IllegalStateException(
113+
"Request input stream is not of type [" + paramType.getName() + "]: " + inputStream);
114+
}
115+
return inputStream;
116+
}
117+
else if (Reader.class.isAssignableFrom(paramType)) {
118+
Reader reader = request.getReader();
119+
if (!paramType.isInstance(reader)) {
120+
throw new IllegalStateException(
121+
"Request body reader is not of type [" + paramType.getName() + "]: " + reader);
122+
}
123+
return reader;
103124
}
104125
else if (Principal.class.isAssignableFrom(paramType)) {
105-
return request.getUserPrincipal();
126+
Principal userPrincipal = request.getUserPrincipal();
127+
if (!paramType.isInstance(userPrincipal)) {
128+
throw new IllegalStateException(
129+
"Current user principal is not of type [" + paramType.getName() + "]: " + userPrincipal);
130+
}
131+
return userPrincipal;
132+
}
133+
else if (HttpMethod.class == paramType) {
134+
return HttpMethod.resolve(request.getMethod());
106135
}
107136
else if (Locale.class == paramType) {
108137
return RequestContextUtils.getLocale(request);
@@ -114,16 +143,10 @@ else if (TimeZone.class == paramType) {
114143
else if ("java.time.ZoneId".equals(paramType.getName())) {
115144
return ZoneIdResolver.resolveZoneId(request);
116145
}
117-
else if (InputStream.class.isAssignableFrom(paramType)) {
118-
return request.getInputStream();
119-
}
120-
else if (Reader.class.isAssignableFrom(paramType)) {
121-
return request.getReader();
122-
}
123146
else {
124-
// should never happen...
147+
// Should never happen...
125148
throw new UnsupportedOperationException(
126-
"Unknown parameter type: " + paramType + " in method: " + parameter.getMethod());
149+
"Unknown parameter type [" + paramType.getName() + "] in " + parameter.getMethod());
127150
}
128151
}
129152

0 commit comments

Comments
 (0)