Spring 重定向指南

4,193 阅读8分钟
原文链接: www.oschina.net

1. 概述

本文将重点介绍在 Spring 中实现重定向(Redirect),并将讨论每个策略背后的原因。

2. 为什么要重定向?

让我们先来考虑在 Spring 应用程序中为什么您可能需要做一个重定向的原因。

当然有很多可能的例子和原因。 一个简单的可能是 POST 表单数据,围绕双重提交问题,或者只是将执行流委托给另一个控制器方法。

附注一点,典型的 Post / Redirect / Get 模式并不能充分解决双重提交问题 - 在初始提交完成之前刷新页面的问题可能仍然会导致双重提交。

3、使用  RedirectView 重定向

我们从这个简单的方法开始 - 直接来一个例子:

@Controller
@RequestMapping("/")
public class RedirectController {
     
    @GetMapping("/redirectWithRedirectView")
    public RedirectView redirectWithUsingRedirectView(RedirectAttributes attributes) {
        attributes.addFlashAttribute("flashAttribute", "redirectWithRedirectView");
        attributes.addAttribute("attribute", "redirectWithRedirectView");
        return new RedirectView("redirectedUrl");
    }
}

在背后,RedirectView 会触发 HttpServletResponse.sendRedirect() - 这将执行实际的重定向。

注意这里我们是如何注入重定向属性到方法里面的 - 由框架完成这部分繁重的工作,让我们能够与这些属性交互。

我们添加 attribute 到模型RedirectAttributes中 - 将其作为 HTTP 查询参数(Query parameter)暴露。 该模型包含的对象 - 通常是字符串或可以被转换成字符串的对象。

现在让我们来测试我们的重定向功能 - 用一个简单的 curl 命令来帮助实现:

curl -i http://localhost:8080/spring-rest/redirectWithRedirectView

结果将是:

HTTP/1.1 302 Found
Server: Apache-Coyote/1.1
Location: 
  http://localhost:8080/spring-rest/redirectedUrl?attribute=redirectWithRedirectView

4. 使用redirect:前缀进行重定向

前面一个方法使用RedirectView,因为一些原因它并不是最优的。

首先,我们现在是耦合于Spring API的,因为我们在我们的代码里直接地使用RedirectView

其次,我们需要从一开始就知道,当实现控制器操作的时候,它的结果将总是重定向的,但情况并非总是如此。

更好的选择是使用redirect:前缀——重定向视图名称像其它逻辑视图名称一样被注入到控制器中。控制器甚至不知道重定向正在发生。

它看起来像是这样的:

@Controller
@RequestMapping("/")
public class RedirectController {
     
    @GetMapping("/redirectWithRedirectPrefix")
    public ModelAndView redirectWithUsingRedirectPrefix(ModelMap model) {
        model.addAttribute("attribute", "redirectWithRedirectPrefix");
        return new ModelAndView("redirect:/redirectedUrl", model);
    }
}

当视图名称跟redirect:一起返回的时候,UrlBasedViewResolver类(以及它的所有子类)会将其识别为一个需要进行重定向的特殊指示。视图名称剩下的部分会被当作重定向URL。

这里有一个地方需要注意——当我们在这里使用redirect:/redirectedUrl逻辑视图的时候,我们正在做一个跟当前Servlet上下文相关的重定向。

如果需要重定向到一个绝对URL,我们可以使用像这样的名称:redirect: http://localhost:8080/spring-redirect/redirectedUrl

所以现在,当我们执行curl命令:

curl -i http://localhost:8080/spring-rest/redirectWithRedirectPrefix

我们会立刻得到一个重定向:

HTTP/1.1 302 Found
Server: Apache-Coyote/1.1
Location: 
  http://localhost:8080/spring-rest/redirectedUrl?attribute=redirectWithRedirectPrefix

5. 使用forward前缀转发:

我们现在看看如何做一些略有不同的事——一个转发。
在看代码之前,我们先来看一下对转发与重定向的语义的快速、高层概括

  • 重定向将以包含302响应码和Location头的新URL进行响应;然后浏览器/客户端将再次向新的URL发出请求

  • 转发完全在服务器端发生; Servlet容器将相同的请求转发到目标URL;浏览器中的URL无须改变

现在我们来看看代码:

@Controller
@RequestMapping("/")
public class RedirectController {
     
    @GetMapping("/forwardWithForwardPrefix")
    public ModelAndView redirectWithUsingForwardPrefix(ModelMap model) {
        model.addAttribute("attribute", "forwardWithForwardPrefix");
        return new ModelAndView("forward:/redirectedUrl", model);
    }
}

redirect:一样,forward:前缀将由UrlBasedViewResolver及其子类解析。在内部,这将创建一个InternalResourceView,它为新视图执行一个RequestDispatcher.forward()操作。
当我们用curl执行该命令时:

curl -I http://localhost:8080/spring-rest/forwardWithForwardPrefix

我们会得到HTTP 405 (不允许的方法):

HTTP/1.1 405 Method Not Allowed
Server: Apache-Coyote/1.1
Allow: GET
Content-Type: text/html;charset=utf-8

与我们在重定向解决方案中的两个请求相比,在这种情况下,我们只有一个请求从浏览器/客户端发送到服务器端。当然,以前由重定向添加的属性也不需要了。

6. 包含RedirectAttributes的属性

接下来 - 让我们看看在一个重定向中传递属性 - 充分利用框架中的RedirectAttribures:

@GetMapping("/redirectWithRedirectAttributes")
public RedirectView redirectWithRedirectAttributes(RedirectAttributes attributes) {
  
    attributes.addFlashAttribute("flashAttribute", "redirectWithRedirectAttributes");
    attributes.addAttribute("attribute", "redirectWithRedirectAttributes");
    return new RedirectView("redirectedUrl");
}

如前所述,我们可以直接在方法中插入属性对象 - 这使得该机制非常容易使用。
还要注意,我们也添加一个Flash属性 - 这是一个不会被添加到URL中的属性。我们可以通过这种属性来实现——我们稍后可以在重定向的最终目标的方法中使用@ModelAttribute(“flashAttribute”)来访问flash属性:

@GetMapping("/redirectedUrl")
public ModelAndView redirection(
  ModelMap model, 
  @ModelAttribute("flashAttribute") Object flashAttribute) {
      
     model.addAttribute("redirectionAttribute", flashAttribute);
     return new ModelAndView("redirection", model);
 }

因此,圆满完工——如果你需要使用curl测试该功能:

curl -i http://localhost:8080/spring-rest/redirectWithRedirectAttributes

我们将会被重定向到新的位置:

HTTP/1.1 302 Found
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=4B70D8FADA2FD6C22E73312C2B57E381; Path=/spring-rest/; HttpOnly
Location: http://localhost:8080/spring-rest/redirectedUrl;
  jsessionid=4B70D8FADA2FD6C22E73312C2B57E381?attribute=redirectWithRedirectAttributes

这样,使用RedirectAttribures代替ModelMap,赋予我们仅在重定向操作中涉及的两种方法之间共享一些属性的能力。

7. 没有前缀的另一种配置

现在让我们探索另一种配置——没有前缀的重定向。

为了实现这一点,我们需要使用org.springframework.web.servlet.view.XmlViewResolver

<bean class="org.springframework.web.servlet.view.XmlViewResolver">
    <property name="location">
        <value>/WEB-INF/spring-views.xml</value>
    </property>
    <property name="order" value="0" />
</bean>

代替我们在之前配置里使用的org.springframework.web.servlet.view.InternalResourceViewResolver

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
</bean>

我们还需要在配置里面定义一个RedirectView bean:

<bean id="RedirectedUrl" class="org.springframework.web.servlet.view.RedirectView">
    <property name="url" value="redirectedUrl" />
</bean>

现在我们可以通过id来引用这个新的bean来触发重定向:

@Controller
@RequestMapping("/")
public class RedirectController {
     
    @GetMapping("/redirectWithXMLConfig")
    public ModelAndView redirectWithUsingXMLConfig(ModelMap model) {
        model.addAttribute("attribute", "redirectWithXMLConfig");
        return new ModelAndView("RedirectedUrl", model);
    }
}

为了测试它,我们再次使用curl命令:

curl -i http://localhost:8080/spring-rest/redirectWithRedirectView

结果会是:

HTTP/1.1 302 Found
Server: Apache-Coyote/1.1
Location: 
  http://localhost:8080/spring-rest/redirectedUrl?attribute=redirectWithRedirectView

8. 重定向HTTP POST请求 Request

对于类似银行付款这样的用例,我们可能需要重定向HTTP POST请求。根据返回的HTTP状态码,POST请求可以重定向到HTTP GET或POST上。
根据HTTP 1.1协议参考,状态码301(永久移除)和302(已找到)允许请求方法从POST更改为GET。该规范还定义了不允许将请求方法从POST更改为GET的相关的307(临时重定向)和308(永久重定向)状态码。
现在,我们来看看将post请求重定向到另一个post请求的代码:

@PostMapping("/redirectPostToPost")
public ModelAndView redirectPostToPost(HttpServletRequest request) {
    request.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.TEMPORARY_REDIRECT);
    return new ModelAndView("redirect:/redirectedPostToPost");
}

@PostMapping("/redirectedPostToPost")
public ModelAndView redirectedPostToPost() {
    return new ModelAndView("redirection");
}

现在,让我们使用curl命令来测试下重定向的POST:

curl -L --verbose -X POST http://localhost:8080/spring-rest/redirectPostToPost

我们正在被重定向到目标地址:

> POST /redirectedPostToPost HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.49.0
> Accept: */*
> 
< HTTP/1.1 200 
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Tue, 08 Aug 2017 07:33:00 GMT
 
{"id":1,"content":"redirect completed"}

9. 结论

本文介绍了在Spring中实现重定向的三种不同方法,在执行这些重定向时如何处理/传递属性以及如何处理HTTP POST请求的重定向。