Java 使用 Spring restTemplate 遵循 302 重定向?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29418583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:58:09  来源:igfitidea点击:

Follow 302 redirect using Spring restTemplate?

javaspringredirectresttemplate

提问by techie.brandon

  RestTemplate restTemplate = new RestTemplate();

  final MappingHymanson2XmlHttpMessageConverter converter = new MappingHymanson2XmlHttpMessageConverter();
  final List<MediaType> supportedMediaTypes = new LinkedList<MediaType>(converter.getSupportedMediaTypes());
  supportedMediaTypes.add(MediaType.ALL);
  converter.setSupportedMediaTypes(supportedMediaTypes);
  restTemplate.getMessageConverters().add(converter);  


  ResponseEntity<MyDTO[]> response = restTemplate.getForEntity(urlBase, MyDTO[].class);

  HttpHeaders headers = response.getHeaders();
  URI location = headers.getLocation(); // Has my redirect URI

  response.getBody(); //Always null

I was under the impression that a 302 would automatically be followed. Am I incorrect in this assumption? I now need to pick off this location and re-request?

我的印象是会自动遵循 302。我在这个假设中不正确吗?我现在需要选择这个位置并重新请求?

回答by fateddy

Using the default ClientHttpRequestFactoryimplementation - which is the SimpleClientHttpRequestFactory- the default behaviour is to follow the URL of the location header (for responses with status codes 3xx) - but only if the initial request was a GETrequest.

使用默认ClientHttpRequestFactory实现 - 即SimpleClientHttpRequestFactory- 默认行为是遵循位置标头的 URL(对于带有状态代码的响应3xx) - 但前提是初始请求是GET请求。

Details can be found in this class - searching for the following method:

可以在这个类中找到详细信息 - 搜索以下方法:

protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {

    ...

    if ("GET".equals(httpMethod)) {
        connection.setInstanceFollowRedirects(true);
    }

Here the relevant doc comment of HttpURLConnection.setInstanceFollowRedirectsmethod:

这里是HttpURLConnection.setInstanceFollowRedirects方法的相关文档注释:

Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed by this {@code HttpURLConnection} instance.

The default value comes from followRedirects, which defaults to true.

设置此 {@code HttpURLConnection} 实例是否应自动跟随 HTTP 重定向(响应代码为 3xx 的请求)。

默认值来自 followRedirects,默认为 true。

回答by Mrkvozrout

When using the CommonsClientHttpRequestFactory(which uses HttpClient v3underneath) you can override the postProcessCommonsHttpMethodmethod and set to follow redirects.

使用CommonsClientHttpRequestFactory(在下面使用HttpClient v3)时,您可以覆盖postProcessCommonsHttpMethod方法并设置为跟随重定向。

public class FollowRedirectsCommonsClientHttpRequestFactory extends CommonsClientHttpRequestFactory {

  @Override
  protected void postProcessCommonsHttpMethod(HttpMethodBase httpMethod) {
    httpMethod.setFollowRedirects(true);
  }
}

You can then use it like this (with optional, possibly preconfigured, HttpClient instance) and requests will follow the locationheaders in response:

然后你可以像这样使用它(带有可选的,可能是预配置的 HttpClient 实例),请求将跟随location响应头:

RestTemplate restTemplate = new RestTemplate(
      new FollowRedirectsCommonsClientHttpRequestFactory());

回答by Vladimir Mitev

Are you trying to redirect from one protocol to another, e.g. from http to https or vise versa? If so the automatic redirect won't work. See this comment: URLConnection Doesn't Follow Redirect

您是否尝试从一种协议重定向到另一种协议,例如从 http 重定向到 https 或反之亦然?如果是这样,自动重定向将不起作用。请参阅此评论:URLConnection 不遵循重定向

After discussion among Java Networking engineers, it is felt that we shouldn't automatically follow redirect from one protocol to another, for instance, from http to https and vise versa, doing so may have serious security consequences

经过Java Networking工程师的讨论,我们认为我们不应该自动遵循从一种协议重定向到另一种协议,例如从http到https,反之亦然,这样做可能会产生严重的安全后果

from https://bugs.java.com/bugdatabase/view_bug.do?bug_id=4620571

来自https://bugs.java.com/bugdatabase/view_bug.do?bug_id=4620571

Otherwise if you debug the RestTemplatecode you will see that by default HttpURLConnectionis set properly with getInstanceFollowRedirects() == true.

否则,如果您调试RestTemplate代码,您将看到默认情况下HttpURLConnection使用getInstanceFollowRedirects() == true.