【问题标题】:remove project name prefix from spring redirect url从 spring 重定向 url 中删除项目名称前缀
【发布时间】:2019-03-18 19:34:06
【问题描述】:

我使用nginx反向代理连接tomcat,nginx配置为:

server {
  listen      80;
  listen [::]:80;
  server_name magnet.s-m.local;

  location / {
      proxy_pass http://tomcat:8080/magnet/;
      proxy_cookie_path /magnet /;
      proxy_redirect     off;
      proxy_set_header   Host $host;
      proxy_set_header   X-Real-IP $remote_addr;
      proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Host $server_name;
  }
}

一切都可以,但是当我想重定向用户时,spring 将项目名称添加到重定向路径。

@RequestMapping(value = "/login",method = RequestMethod.POST)
public String loginCheck(HttpSession session, @RequestParam("username") String user, @RequestParam("password") String password){
    session.setAttribute("username",user);
    return "redirect:/home";
}

此代码重定向到http://magnet.s-m.local/magnet/home,但我想重定向http://magnet.s-m.local/home

如果我使用RedirectView 效果很好,但使用redirect:/home 更好,因为如果登录失败,我可以决定重定向或加载 jsp 文件。

【问题讨论】:

    标签: java spring nginx redirect jakarta-ee


    【解决方案1】:

    尝试从您的 nginx 配置中删除 proxy_redirectproxy_set_header Host 参数:

    location / {
        proxy_pass http://tomcat:8080/magnet/;
        proxy_cookie_path /magnet /;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $server_name;
    }
    

    您也可以更详细地指定proxy_redirect,但它应该被激活。

    【讨论】:

      猜你喜欢
      • 2016-07-24
      • 1970-01-01
      • 2018-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多