【问题标题】:Redirection in spring MVC with loadbalacer使用负载均衡器在 Spring MVC 中进行重定向
【发布时间】:2014-08-28 19:37:11
【问题描述】:

我有一个 Spring MVC 应用程序,它托管在服务器 S 上。来自客户端 C 的所有调用都通过负载均衡器 L 转到 S。负载均衡器内部用于服务器 S 的 url 是 internalurl.com 和客户端的 url使用的是 xyz.com。

问题是,当我在我的 mozilla 浏览器上点击一个 url 说 xyz.com/admin/ 时,我希望它会被重定向到 xyz.com/admin/home.html。但是相反,它重定向到 internalurl.com/admin/home.html 并且由于 internalurl.com 是负载平衡器的内部,我在浏览器上收到“网络访问消息:找不到网站”和“internalurl.com/ admin/home.html”在我的浏览器地址栏中

对于重定向,我使用的是:

  public void doFilter(ServletRequest req, ServletResponse res,
                     FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;

    String strReferer = request.getHeader("Referer");

    String strConetextPath = request.getContextPath();

    if (strReferer != null && !strReferer.contains(strConetextPath)) {
        response.sendRedirect("/requestError.html");
    } 
    // redirect to home.html in case user tries to access /admin/
    // in case user is not already logged-in, user will be redirected to login.html
    else if (request.getRequestURI().equalsIgnoreCase("/admin/")) {
        response.sendRedirect("home.html");
    }else
        chain.doFilter(req, res);
}

提前致谢。

【问题讨论】:

    标签: spring spring-mvc filter url-redirection http-redirect


    【解决方案1】:

    这根本不是 SPring MVC 的问题。它应在代理/负载平衡器处处理。

    对于发生在内部服务器级别的任何重定向,都将具有内部服务器 URL,客户端/浏览器将不知道该 URL。因为客户端只知道 Web 服务器(正如你提到的 XYZ.com)。

    这是代理或负载均衡器使用反向代理概念处理的典型问题。

    例如:在 Apache HTTP 的情况下,我们使用 ProxyPassReverse 配置,这将在响应浏览器之前将所有内部服务器 url 标头转换为指向 XYZ.com。这将确保浏览器只调用 xyz.com

    希望对你有帮助。

    谢谢,JP

    【讨论】:

    • 嗨,JP,在您发表评论后,我检查了反向代理概念,对我来说,这似乎可能是由于负载平衡器配置。事实证明,这条评论提供了丰富的信息。 stackoverflow.com/a/8141490/2701227
    猜你喜欢
    • 2017-06-21
    • 2017-07-07
    • 2017-03-10
    • 1970-01-01
    • 2013-12-25
    • 2017-01-20
    • 2020-02-25
    • 1970-01-01
    • 2020-05-26
    相关资源
    最近更新 更多