在Spring MVC中redirect到控制器操作的外部URL

我注意到下面的代码将用户redirect到项目中的URL,

@RequestMapping(method = RequestMethod.POST) public String processForm(HttpServletRequest request, LoginForm loginForm, BindingResult result, ModelMap model) { String redirectUrl = "yahoo.com"; return "redirect:" + redirectUrl; } 

而以下按预期正确redirect,但需要http://或https://

 @RequestMapping(method = RequestMethod.POST) public String processForm(HttpServletRequest request, LoginForm loginForm, BindingResult result, ModelMap model) { String redirectUrl = "http://www.yahoo.com"; return "redirect:" + redirectUrl; } 

我想redirect总是redirect到指定的URL,无论是否有有效协议,也不想redirect到视图。 我怎样才能做到这一点?

谢谢,

你可以用两种方法做到这一点。

第一:

  @RequestMapping(value = "/redirect", method = RequestMethod.GET) public void method(HttpServletResponse httpServletResponse) { httpServletResponse.setHeader("Location", projectUrl); } 

第二:

  @RequestMapping(value = "/redirect", method = RequestMethod.GET) public ModelAndView method() { return new ModelAndView("redirect:" + projectUrl); } 

纵观UrlBasedViewResolver和RedirectView的实际实现,如果您的redirect目标以/开头,redirect将始终为contextRelative。 所以也发送一个//yahoo.com/path/to/resource不会得到一个协议相对redirect。

所以为了达到你的目的,你可以做一些事情:

 @RequestMapping(method = RequestMethod.POST) public String processForm(HttpServletRequest request, LoginForm loginForm, BindingResult result, ModelMap model) { String redirectUrl = request.getScheme() + "://www.yahoo.com"; return "redirect:" + redirectUrl; } 

你可以使用RedirectView 。 从JavaDoc复制:

查看redirect到绝对,上下文相关或当前请求相对URL

例:

 @RequestMapping("/to-be-redirected") public RedirectView localRedirect() { RedirectView redirectView = new RedirectView(); redirectView.setUrl("http://www.yahoo.com"); return redirectView; } 

你也可以使用ResponseEntity ,例如

 @RequestMapping("/to-be-redirected") public ResponseEntity<Object> redirectToExternalUrl() throws URISyntaxException { URI yahoo = new URI("http://www.yahoo.com"); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setLocation(yahoo); return new ResponseEntity<>(httpHeaders, HttpStatus.SEE_OTHER); } 

当然,还有别人提到的返回redirect:http://www.yahoo.com

另一种方法是使用sendRedirect方法:

 @RequestMapping( value = "/", method = RequestMethod.GET) public void redirectToTwitter(HttpServletResponse httpServletResponse) throws IOException { httpServletResponse.sendRedirect("https://twitter.com"); } 

对于外部url,您必须使用“ http://www.yahoo.com ”作为redirecturl。

这在Spring参考文档的redirect:前缀中进行了解释。

redirect:/ MyApp的/一些/资源

会相对于当前的Servlet上下文redirect,而一个名字如

redirect: http : //myhost.com/some/arbitrary/path

将redirect到一个绝对的URL

对我来说工作得很好:

 @RequestMapping (value = "/{id}", method = RequestMethod.GET) public ResponseEntity<Object> redirectToExternalUrl() throws URISyntaxException { URI uri = new URI("http://www.google.com"); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setLocation(uri); return new ResponseEntity<>(httpHeaders, HttpStatus.SEE_OTHER); } 

你有没有尝试RedirectView你可以提供contextRelative参数?

总之, "redirect:yahoo.com""redirect://yahoo.com"将借给yahoo.com

作为"redirect:yahoo.com"将借给你your-context/yahoo.com即ex- localhost:8080/yahoo.com