Skip to content

Commit b80c13b

Browse files
committed
Deprecate JSONP and disable it by default in Jackson view
Issue: SPR-16798
1 parent 7bfd683 commit b80c13b

File tree

11 files changed

+62
-15
lines changed

11 files changed

+62
-15
lines changed

Diff for: spring-web/src/main/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -91,6 +91,7 @@ public void setPrefixJson(boolean prefixJson) {
9191

9292

9393
@Override
94+
@SuppressWarnings("deprecation")
9495
protected void writePrefix(JsonGenerator generator, Object object) throws IOException {
9596
if (this.jsonPrefix != null) {
9697
generator.writeRaw(this.jsonPrefix);
@@ -104,6 +105,7 @@ protected void writePrefix(JsonGenerator generator, Object object) throws IOExce
104105
}
105106

106107
@Override
108+
@SuppressWarnings("deprecation")
107109
protected void writeSuffix(JsonGenerator generator, Object object) throws IOException {
108110
String jsonpFunction =
109111
(object instanceof MappingJacksonValue ? ((MappingJacksonValue) object).getJsonpFunction() : null);

Diff for: spring-web/src/main/java/org/springframework/http/converter/json/MappingJacksonValue.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -115,14 +115,20 @@ public FilterProvider getFilters() {
115115

116116
/**
117117
* Set the name of the JSONP function name.
118+
* @deprecated Will be removed as of Spring Framework 5.1, use
119+
* <a href="https://docs.spring.io/spring/docs/5.0.x/spring-framework-reference/web.html#mvc-cors">CORS</a> instead.
118120
*/
121+
@Deprecated
119122
public void setJsonpFunction(@Nullable String functionName) {
120123
this.jsonpFunction = functionName;
121124
}
122125

123126
/**
124127
* Return the configured JSONP function name.
128+
* @deprecated Will be removed as of Spring Framework 5.1, use
129+
* <a href="https://docs.spring.io/spring/docs/5.0.x/spring-framework-reference/web.html#mvc-cors">CORS</a> instead.
125130
*/
131+
@Deprecated
126132
@Nullable
127133
public String getJsonpFunction() {
128134
return this.jsonpFunction;

Diff for: spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractJsonpResponseBodyAdvice.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -45,7 +45,10 @@
4545
*
4646
* @author Rossen Stoyanchev
4747
* @since 4.1
48+
* @deprecated Will be removed as of Spring Framework 5.1, use
49+
* <a href="https://docs.spring.io/spring/docs/5.0.x/spring-framework-reference/web.html#mvc-cors">CORS</a> instead.
4850
*/
51+
@Deprecated
4952
public abstract class AbstractJsonpResponseBodyAdvice extends AbstractMappingJacksonResponseBodyAdvice {
5053

5154
/**

Diff for: spring-webmvc/src/main/java/org/springframework/web/servlet/view/json/MappingJackson2JsonView.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -59,6 +59,7 @@
5959
* @author Sebastien Deleuze
6060
* @since 3.1.2
6161
*/
62+
@SuppressWarnings("deprecation")
6263
public class MappingJackson2JsonView extends AbstractJackson2View {
6364

6465
/**
@@ -69,7 +70,10 @@ public class MappingJackson2JsonView extends AbstractJackson2View {
6970

7071
/**
7172
* Default content type for JSONP: "application/javascript".
73+
* @deprecated Will be removed as of Spring Framework 5.1, use
74+
* <a href="https://docs.spring.io/spring/docs/5.0.x/spring-framework-reference/web.html#mvc-cors">CORS</a> instead.
7275
*/
76+
@Deprecated
7377
public static final String DEFAULT_JSONP_CONTENT_TYPE = "application/javascript";
7478

7579
/**
@@ -87,7 +91,7 @@ public class MappingJackson2JsonView extends AbstractJackson2View {
8791
private boolean extractValueFromSingleKeyModel = false;
8892

8993
@Nullable
90-
private Set<String> jsonpParameterNames = new LinkedHashSet<>(Arrays.asList("jsonp", "callback"));
94+
private Set<String> jsonpParameterNames = new LinkedHashSet<>();
9195

9296

9397
/**
@@ -170,10 +174,14 @@ public void setExtractValueFromSingleKeyModel(boolean extractValueFromSingleKeyM
170174
* Set JSONP request parameter names. Each time a request has one of those
171175
* parameters, the resulting JSON will be wrapped into a function named as
172176
* specified by the JSONP request parameter value.
173-
* <p>The parameter names configured by default are "jsonp" and "callback".
177+
* <p>As of Spring Framework 5.0.7, there is no parameter name configured
178+
* by default.
174179
* @since 4.1
175180
* @see <a href="http://en.wikipedia.org/wiki/JSONP">JSONP Wikipedia article</a>
181+
* @deprecated Will be removed as of Spring Framework 5.1, use
182+
* <a href="https://docs.spring.io/spring/docs/5.0.x/spring-framework-reference/web.html#mvc-cors">CORS</a> instead.
176183
*/
184+
@Deprecated
177185
public void setJsonpParameterNames(Set<String> jsonpParameterNames) {
178186
this.jsonpParameterNames = jsonpParameterNames;
179187
}
@@ -204,7 +212,10 @@ private String getJsonpParameterValue(HttpServletRequest request) {
204212
* Invalid parameter values are ignored.
205213
* @param value the query param value, never {@code null}
206214
* @since 4.1.8
215+
* @deprecated Will be removed as of Spring Framework 5.1, use
216+
* <a href="https://docs.spring.io/spring/docs/5.0.x/spring-framework-reference/web.html#mvc-cors">CORS</a> instead.
207217
*/
218+
@Deprecated
208219
protected boolean isValidJsonpQueryParam(String value) {
209220
return CALLBACK_PARAM_PATTERN.matcher(value).matches();
210221
}

Diff for: spring-webmvc/src/test/java/org/springframework/web/servlet/view/json/MappingJackson2JsonViewTests.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -17,9 +17,11 @@
1717
package org.springframework.web.servlet.view.json;
1818

1919
import java.io.IOException;
20+
import java.util.Arrays;
2021
import java.util.Date;
2122
import java.util.HashMap;
2223
import java.util.HashSet;
24+
import java.util.LinkedHashSet;
2325
import java.util.Map;
2426
import java.util.Set;
2527

@@ -324,11 +326,19 @@ public void renderSimpleBeanWithFilters() throws Exception {
324326

325327
@Test
326328
public void renderWithJsonp() throws Exception {
329+
testJsonp("jsonp", "callback", false);
330+
testJsonp("jsonp", "_callback", false);
331+
testJsonp("jsonp", "_Call.bAcK", false);
332+
testJsonp("jsonp", "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_.", false);
333+
testJsonp("jsonp", "<script>", false);
334+
testJsonp("jsonp", "!foo!bar", false);
335+
336+
this.view.setJsonpParameterNames(new LinkedHashSet<>(Arrays.asList("jsonp")));
337+
327338
testJsonp("jsonp", "callback", true);
328339
testJsonp("jsonp", "_callback", true);
329340
testJsonp("jsonp", "_Call.bAcK", true);
330341
testJsonp("jsonp", "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_.", true);
331-
332342
testJsonp("jsonp", "<script>", false);
333343
testJsonp("jsonp", "!foo!bar", false);
334344
}

Diff for: spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportType.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -28,6 +28,8 @@
2828
/**
2929
* SockJS transport types.
3030
*
31+
* <p>JSONP support will be removed as of Spring Framework 5.1, use others transports instead.
32+
*
3133
* @author Rossen Stoyanchev
3234
* @author Sebastien Deleuze
3335
* @since 4.0
@@ -40,8 +42,10 @@ public enum TransportType {
4042

4143
XHR_SEND("xhr_send", HttpMethod.POST, "cors", "jsessionid", "no_cache"),
4244

45+
@Deprecated
4346
JSONP("jsonp", HttpMethod.GET, "jsessionid", "no_cache"),
4447

48+
@Deprecated
4549
JSONP_SEND("jsonp_send", HttpMethod.POST, "jsessionid", "no_cache"),
4650

4751
XHR_STREAMING("xhr_streaming", HttpMethod.POST, "cors", "jsessionid", "no_cache"),

Diff for: spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/DefaultSockJsService.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -79,6 +79,7 @@ public DefaultSockJsService(TaskScheduler scheduler, Collection<TransportHandler
7979
}
8080

8181

82+
@SuppressWarnings("deprecation")
8283
private static Set<TransportHandler> getDefaultTransportHandlers(@Nullable Collection<TransportHandler> overrides) {
8384
Set<TransportHandler> result = new LinkedHashSet<>(8);
8485
result.add(new XhrPollingTransportHandler());

Diff for: spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/JsonpPollingTransportHandler.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -41,7 +41,9 @@
4141
*
4242
* @author Rossen Stoyanchev
4343
* @since 4.0
44+
* @deprecated Will be removed as of Spring Framework 5.1, use others transports instead.
4445
*/
46+
@Deprecated
4547
public class JsonpPollingTransportHandler extends AbstractHttpSendingTransportHandler {
4648

4749
@Override

Diff for: spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/JsonpReceivingTransportHandler.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -38,7 +38,9 @@
3838
* A {@link TransportHandler} that receives messages over HTTP.
3939
*
4040
* @author Rossen Stoyanchev
41+
* @deprecated Will be removed as of Spring Framework 5.1, use others transports instead.
4142
*/
43+
@Deprecated
4244
public class JsonpReceivingTransportHandler extends AbstractHttpReceivingTransportHandler {
4345

4446
private final FormHttpMessageConverter formConverter = new FormHttpMessageConverter();

Diff for: src/docs/asciidoc/web/webmvc-view.adoc

+4-3
Original file line numberDiff line numberDiff line change
@@ -2030,9 +2030,10 @@ annotations. When further control is needed, a custom `ObjectMapper` can be inje
20302030
through the `ObjectMapper` property for cases where custom JSON
20312031
serializers/deserializers need to be provided for specific types.
20322032

2033-
http://en.wikipedia.org/wiki/JSONP[JSONP] is supported and automatically enabled when
2034-
the request has a query parameter named `jsonp` or `callback`. The JSONP query parameter
2035-
name(s) could be customized through the `jsonpParameterNames` property.
2033+
As of Spring Framework 5.0.7, http://en.wikipedia.org/wiki/JSONP[JSONP] support is
2034+
deprecated and requires to customize the JSONP query parameter
2035+
name(s) through the `jsonpParameterNames` property. This support will be removed as of
2036+
Spring Framework 5.1, <<mvc-cors,CORS>> should be used instead.
20362037

20372038

20382039

Diff for: src/docs/asciidoc/web/webmvc.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -2670,6 +2670,11 @@ For controllers relying on view resolution, JSONP is automatically enabled when
26702670
request has a query parameter named `jsonp` or `callback`. Those names can be
26712671
customized through `jsonpParameterNames` property.
26722672

2673+
[NOTE]
2674+
====
2675+
As of Spring Framework 5.0.7, JSONP support is deprecated and will be removed as of
2676+
Spring Framework 5.1, <<mvc-cors,CORS>> should be used instead.
2677+
====
26732678

26742679

26752680
[[mvc-ann-modelattrib-methods]]

0 commit comments

Comments
 (0)