我注意到以下代码将用户重定向到项目内的 URL,
@RequestMapping(method = RequestMethod.POST)
public String processForm(HttpServletRequest request, LoginForm loginForm,
BindingResult result, ModelMap model)
{
String redirectUrl = "yahoo.com";
return "redirect:" + redirectUrl;
}
而以下内容按预期正确重定向,但需要 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;
}
我希望重定向始终重定向到指定的 URL,无论它是否具有有效的协议并且不想重定向到视图。我怎样才能做到这一点?
谢谢,
你可以用两种方法来做到这一点。
第一的:
@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public void method(HttpServletResponse httpServletResponse) {
httpServletResponse.setHeader("Location", projectUrl);
httpServletResponse.setStatus(302);
}
第二:
@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public ModelAndView method() {
return new ModelAndView("redirect:" + projectUrl);
}
您可以使用 RedirectView
。从 JavaDoc 复制:
重定向到绝对、上下文相对或当前请求相对 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
。
您可以使用 ResponseEntity
以非常简洁的方式执行此操作,如下所示:
@GetMapping
ResponseEntity<Void> redirect() {
return ResponseEntity.status(HttpStatus.FOUND)
.location(URI.create("http://www.yahoo.com"))
.build();
}
URI.create("http://www.yahoo.com")
?
查看 UrlBasedViewResolver 和 RedirectView 的实际实现,如果您的重定向目标以 / 开头,则重定向将始终是 contextRelative。因此,发送 //yahoo.com/path/to/resource 也无助于获得协议相对重定向。
因此,要实现您正在尝试的内容,您可以执行以下操作:
@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;
}
另一种方法是使用 sendRedirect
方法:
@RequestMapping(
value = "/",
method = RequestMethod.GET)
public void redirectToTwitter(HttpServletResponse httpServletResponse) throws IOException {
httpServletResponse.sendRedirect("https://twitter.com");
}
对我来说工作正常:
@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);
}
对于外部网址,您必须使用“http://www.yahoo.com”作为重定向网址。
这在 Spring 参考文档的 redirect: prefix 中有说明。
重定向:/myapp/some/resource
将相对于当前 Servlet 上下文进行重定向,而名称如
重定向:http://myhost.com/some/arbitrary/path
将重定向到绝对 URL
http://www.yahoo.com
、http://www.google.com
等作为他们解决方案的示例输入。如果他们使用 www.yahoo.com
,无论是 ResponseEntity
还是 redirect:
,它都会中断。尝试使用 Spring boot 2.5.2 和 Chrome、Firefox 和 Safari。
您是否尝试过可以提供 contextRelative 参数的 RedirectView?
/
开头(或不以 /
开头)的路径很有用,以检查它是否应该与 webapp 上下文相关。重定向请求仍将针对同一主机。
这对我有用,并解决了“对预检请求的响应未通过访问控制检查......”问题。
控制器
RedirectView doRedirect(HttpServletRequest request){
String orgUrl = request.getRequestURL()
String redirectUrl = orgUrl.replaceAll(".*/test/","http://xxxx.com/test/")
RedirectView redirectView = new RedirectView()
redirectView.setUrl(redirectUrl)
redirectView.setStatusCode(HttpStatus.TEMPORARY_REDIRECT)
return redirectView
}
并启用安全性
@EnableWebSecurity
class SecurityConfigurer extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable()
}
}
简而言之,"redirect://yahoo.com"
会将您借给 yahoo.com
。
"redirect:yahoo.com"
将借给您 your-context/yahoo.com
即为 ex- localhost:8080/yahoo.com
@ResponseStatus(HttpStatus.FOUND)