Skip to content

Commit

Permalink
Deprecate JSONP and disable it by default in Jackson view
Browse files Browse the repository at this point in the history
Issue: SPR-16798
  • Loading branch information
sdeleuze committed Jun 8, 2018
1 parent 75a6f3b commit 8748594
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -88,6 +88,7 @@ public void setPrefixJson(boolean prefixJson) {


@Override
@SuppressWarnings("deprecation")
protected void writePrefix(JsonGenerator generator, Object object) throws IOException {
if (this.jsonPrefix != null) {
generator.writeRaw(this.jsonPrefix);
Expand All @@ -101,6 +102,7 @@ protected void writePrefix(JsonGenerator generator, Object object) throws IOExce
}

@Override
@SuppressWarnings("deprecation")
protected void writeSuffix(JsonGenerator generator, Object object) throws IOException {
String jsonpFunction =
(object instanceof MappingJacksonValue ? ((MappingJacksonValue) object).getJsonpFunction() : null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -108,14 +108,20 @@ public FilterProvider getFilters() {

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

/**
* Return the configured JSONP function name.
* @deprecated Will be removed as of Spring Framework 5.1, use
* <a href="https://docs.spring.io/spring/docs/4.3.x/spring-framework-reference/html/cors.html">CORS</a> instead.
*/
@Deprecated
public String getJsonpFunction() {
return this.jsonpFunction;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -45,7 +45,10 @@
*
* @author Rossen Stoyanchev
* @since 4.1
* @deprecated Will be removed as of Spring Framework 5.1, use
* <a href="https://docs.spring.io/spring/docs/4.3.x/spring-framework-reference/html/cors.html">CORS</a> instead.
*/
@Deprecated
public abstract class AbstractJsonpResponseBodyAdvice extends AbstractMappingJacksonResponseBodyAdvice {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,6 @@
package org.springframework.web.servlet.view.json;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -58,6 +57,7 @@
* @author Sebastien Deleuze
* @since 3.1.2
*/
@SuppressWarnings("deprecation")
public class MappingJackson2JsonView extends AbstractJackson2View {

/**
Expand All @@ -68,7 +68,10 @@ public class MappingJackson2JsonView extends AbstractJackson2View {

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

/**
Expand All @@ -83,7 +86,7 @@ public class MappingJackson2JsonView extends AbstractJackson2View {

private boolean extractValueFromSingleKeyModel = false;

private Set<String> jsonpParameterNames = new LinkedHashSet<String>(Arrays.asList("jsonp", "callback"));
private Set<String> jsonpParameterNames = new LinkedHashSet<String>();


/**
Expand Down Expand Up @@ -168,7 +171,10 @@ public void setExtractValueFromSingleKeyModel(boolean extractValueFromSingleKeyM
* <p>The parameter names configured by default are "jsonp" and "callback".
* @since 4.1
* @see <a href="http://en.wikipedia.org/wiki/JSONP">JSONP Wikipedia article</a>
* @deprecated Will be removed as of Spring Framework 5.1, use
* <a href="https://docs.spring.io/spring/docs/4.3.x/spring-framework-reference/html/cors.html">CORS</a> instead.
*/
@Deprecated
public void setJsonpParameterNames(Set<String> jsonpParameterNames) {
this.jsonpParameterNames = jsonpParameterNames;
}
Expand Down Expand Up @@ -198,7 +204,10 @@ private String getJsonpParameterValue(HttpServletRequest request) {
* Invalid parameter values are ignored.
* @param value the query param value, never {@code null}
* @since 4.1.8
* @deprecated Will be removed as of Spring Framework 5.1, use
* <a href="https://docs.spring.io/spring/docs/4.3.x/spring-framework-reference/html/cors.html">CORS</a> instead.
*/
@Deprecated
protected boolean isValidJsonpQueryParam(String value) {
return CALLBACK_PARAM_PATTERN.matcher(value).matches();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,9 +17,11 @@
package org.springframework.web.servlet.view.json;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

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

@Test
public void renderWithJsonp() throws Exception {
testJsonp("jsonp", "callback", false);
testJsonp("jsonp", "_callback", false);
testJsonp("jsonp", "_Call.bAcK", false);
testJsonp("jsonp", "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_.", false);
testJsonp("jsonp", "<script>", false);
testJsonp("jsonp", "!foo!bar", false);

this.view.setJsonpParameterNames(new LinkedHashSet<String>(Arrays.asList("jsonp")));

testJsonp("jsonp", "callback", true);
testJsonp("jsonp", "_callback", true);
testJsonp("jsonp", "_Call.bAcK", true);
testJsonp("jsonp", "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_.", true);

testJsonp("jsonp", "<script>", false);
testJsonp("jsonp", "!foo!bar", false);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,8 @@
/**
* SockJS transport types.
*
* <p>JSONP support will be removed as of Spring Framework 5.1, use others transports instead.
*
* @author Rossen Stoyanchev
* @author Sebastien Deleuze
* @since 4.0
Expand All @@ -39,8 +41,10 @@ public enum TransportType {

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

@Deprecated
JSONP("jsonp", HttpMethod.GET, "jsessionid", "no_cache"),

@Deprecated
JSONP_SEND("jsonp_send", HttpMethod.POST, "jsessionid", "no_cache"),

XHR_STREAMING("xhr_streaming", HttpMethod.POST, "cors", "jsessionid", "no_cache"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -78,6 +78,7 @@ public DefaultSockJsService(TaskScheduler scheduler, Collection<TransportHandler
}


@SuppressWarnings("deprecation")
private static Set<TransportHandler> getDefaultTransportHandlers(Collection<TransportHandler> overrides) {
Set<TransportHandler> result = new LinkedHashSet<TransportHandler>(8);
result.add(new XhrPollingTransportHandler());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -40,7 +40,9 @@
*
* @author Rossen Stoyanchev
* @since 4.0
* @deprecated Will be removed as of Spring Framework 5.1, use others transports instead.
*/
@Deprecated
public class JsonpPollingTransportHandler extends AbstractHttpSendingTransportHandler {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,7 +36,9 @@
* A {@link TransportHandler} that receives messages over HTTP.
*
* @author Rossen Stoyanchev
* @deprecated Will be removed as of Spring Framework 5.1, use others transports instead.
*/
@Deprecated
public class JsonpReceivingTransportHandler extends AbstractHttpReceivingTransportHandler {

private final FormHttpMessageConverter formConverter = new FormHttpMessageConverter();
Expand Down
6 changes: 6 additions & 0 deletions src/asciidoc/web-mvc.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2291,6 +2291,12 @@ For controllers relying on view resolution, JSONP is automatically enabled when
request has a query parameter named `jsonp` or `callback`. Those names can be
customized through `jsonpParameterNames` property.

[NOTE]
====
As of Spring Framework 4.3.18, JSONP support is deprecated and will be removed as of
Spring Framework 5.1, <<cors,CORS>> should be used instead.
====


[[mvc-ann-async]]
=== Asynchronous Request Processing
Expand Down
7 changes: 4 additions & 3 deletions src/asciidoc/web-view.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2683,9 +2683,10 @@ annotations. When further control is needed, a custom `ObjectMapper` can be inje
through the `ObjectMapper` property for cases where custom JSON
serializers/deserializers need to be provided for specific types.

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



Expand Down

0 comments on commit 8748594

Please sign in to comment.