Skip to content

Commit 8748594

Browse files
committed
Deprecate JSONP and disable it by default in Jackson view
Issue: SPR-16798
1 parent 75a6f3b commit 8748594

File tree

11 files changed

+61
-15
lines changed

11 files changed

+61
-15
lines changed

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-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.
@@ -88,6 +88,7 @@ public void setPrefixJson(boolean prefixJson) {
8888

8989

9090
@Override
91+
@SuppressWarnings("deprecation")
9192
protected void writePrefix(JsonGenerator generator, Object object) throws IOException {
9293
if (this.jsonPrefix != null) {
9394
generator.writeRaw(this.jsonPrefix);
@@ -101,6 +102,7 @@ protected void writePrefix(JsonGenerator generator, Object object) throws IOExce
101102
}
102103

103104
@Override
105+
@SuppressWarnings("deprecation")
104106
protected void writeSuffix(JsonGenerator generator, Object object) throws IOException {
105107
String jsonpFunction =
106108
(object instanceof MappingJacksonValue ? ((MappingJacksonValue) object).getJsonpFunction() : null);

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-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.
@@ -108,14 +108,20 @@ public FilterProvider getFilters() {
108108

109109
/**
110110
* Set the name of the JSONP function name.
111+
* @deprecated Will be removed as of Spring Framework 5.1, use
112+
* <a href="https://docs.spring.io/spring/docs/4.3.x/spring-framework-reference/html/cors.html">CORS</a> instead.
111113
*/
114+
@Deprecated
112115
public void setJsonpFunction(String functionName) {
113116
this.jsonpFunction = functionName;
114117
}
115118

116119
/**
117120
* Return the configured JSONP function name.
121+
* @deprecated Will be removed as of Spring Framework 5.1, use
122+
* <a href="https://docs.spring.io/spring/docs/4.3.x/spring-framework-reference/html/cors.html">CORS</a> instead.
118123
*/
124+
@Deprecated
119125
public String getJsonpFunction() {
120126
return this.jsonpFunction;
121127
}

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/4.3.x/spring-framework-reference/html/cors.html">CORS</a> instead.
4850
*/
51+
@Deprecated
4952
public abstract class AbstractJsonpResponseBodyAdvice extends AbstractMappingJacksonResponseBodyAdvice {
5053

5154
/**

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

+12-3
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.
@@ -17,7 +17,6 @@
1717
package org.springframework.web.servlet.view.json;
1818

1919
import java.io.IOException;
20-
import java.util.Arrays;
2120
import java.util.Collections;
2221
import java.util.HashMap;
2322
import java.util.LinkedHashSet;
@@ -58,6 +57,7 @@
5857
* @author Sebastien Deleuze
5958
* @since 3.1.2
6059
*/
60+
@SuppressWarnings("deprecation")
6161
public class MappingJackson2JsonView extends AbstractJackson2View {
6262

6363
/**
@@ -68,7 +68,10 @@ public class MappingJackson2JsonView extends AbstractJackson2View {
6868

6969
/**
7070
* Default content type for JSONP: "application/javascript".
71+
* @deprecated Will be removed as of Spring Framework 5.1, use
72+
* <a href="https://docs.spring.io/spring/docs/4.3.x/spring-framework-reference/html/cors.html">CORS</a> instead.
7173
*/
74+
@Deprecated
7275
public static final String DEFAULT_JSONP_CONTENT_TYPE = "application/javascript";
7376

7477
/**
@@ -83,7 +86,7 @@ public class MappingJackson2JsonView extends AbstractJackson2View {
8386

8487
private boolean extractValueFromSingleKeyModel = false;
8588

86-
private Set<String> jsonpParameterNames = new LinkedHashSet<String>(Arrays.asList("jsonp", "callback"));
89+
private Set<String> jsonpParameterNames = new LinkedHashSet<String>();
8790

8891

8992
/**
@@ -168,7 +171,10 @@ public void setExtractValueFromSingleKeyModel(boolean extractValueFromSingleKeyM
168171
* <p>The parameter names configured by default are "jsonp" and "callback".
169172
* @since 4.1
170173
* @see <a href="http://en.wikipedia.org/wiki/JSONP">JSONP Wikipedia article</a>
174+
* @deprecated Will be removed as of Spring Framework 5.1, use
175+
* <a href="https://docs.spring.io/spring/docs/4.3.x/spring-framework-reference/html/cors.html">CORS</a> instead.
171176
*/
177+
@Deprecated
172178
public void setJsonpParameterNames(Set<String> jsonpParameterNames) {
173179
this.jsonpParameterNames = jsonpParameterNames;
174180
}
@@ -198,7 +204,10 @@ private String getJsonpParameterValue(HttpServletRequest request) {
198204
* Invalid parameter values are ignored.
199205
* @param value the query param value, never {@code null}
200206
* @since 4.1.8
207+
* @deprecated Will be removed as of Spring Framework 5.1, use
208+
* <a href="https://docs.spring.io/spring/docs/4.3.x/spring-framework-reference/html/cors.html">CORS</a> instead.
201209
*/
210+
@Deprecated
202211
protected boolean isValidJsonpQueryParam(String value) {
203212
return CALLBACK_PARAM_PATTERN.matcher(value).matches();
204213
}

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-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.
@@ -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<String>(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
}

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-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.
@@ -27,6 +27,8 @@
2727
/**
2828
* SockJS transport types.
2929
*
30+
* <p>JSONP support will be removed as of Spring Framework 5.1, use others transports instead.
31+
*
3032
* @author Rossen Stoyanchev
3133
* @author Sebastien Deleuze
3234
* @since 4.0
@@ -39,8 +41,10 @@ public enum TransportType {
3941

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

44+
@Deprecated
4245
JSONP("jsonp", HttpMethod.GET, "jsessionid", "no_cache"),
4346

47+
@Deprecated
4448
JSONP_SEND("jsonp_send", HttpMethod.POST, "jsessionid", "no_cache"),
4549

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

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-2014 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.
@@ -78,6 +78,7 @@ public DefaultSockJsService(TaskScheduler scheduler, Collection<TransportHandler
7878
}
7979

8080

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

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-2014 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.
@@ -40,7 +40,9 @@
4040
*
4141
* @author Rossen Stoyanchev
4242
* @since 4.0
43+
* @deprecated Will be removed as of Spring Framework 5.1, use others transports instead.
4344
*/
45+
@Deprecated
4446
public class JsonpPollingTransportHandler extends AbstractHttpSendingTransportHandler {
4547

4648
@Override

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-2014 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.
@@ -36,7 +36,9 @@
3636
* A {@link TransportHandler} that receives messages over HTTP.
3737
*
3838
* @author Rossen Stoyanchev
39+
* @deprecated Will be removed as of Spring Framework 5.1, use others transports instead.
3940
*/
41+
@Deprecated
4042
public class JsonpReceivingTransportHandler extends AbstractHttpReceivingTransportHandler {
4143

4244
private final FormHttpMessageConverter formConverter = new FormHttpMessageConverter();

src/asciidoc/web-mvc.adoc

+6
Original file line numberDiff line numberDiff line change
@@ -2291,6 +2291,12 @@ For controllers relying on view resolution, JSONP is automatically enabled when
22912291
request has a query parameter named `jsonp` or `callback`. Those names can be
22922292
customized through `jsonpParameterNames` property.
22932293

2294+
[NOTE]
2295+
====
2296+
As of Spring Framework 4.3.18, JSONP support is deprecated and will be removed as of
2297+
Spring Framework 5.1, <<cors,CORS>> should be used instead.
2298+
====
2299+
22942300

22952301
[[mvc-ann-async]]
22962302
=== Asynchronous Request Processing

src/asciidoc/web-view.adoc

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

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

26902691

26912692

0 commit comments

Comments
 (0)