How to make Spring Security application to run behind a proxy?

前端 未结 3 847
独厮守ぢ
独厮守ぢ 2021-01-04 20:28

We have an application build on Java 1.6 with Spring 3.0.3 that use Spring Security 3.0.5 and implements REST API using Spring Web with RestEasy 2.1.0. I need to place this

3条回答
  •  误落风尘
    2021-01-04 20:56

    Spring Security uses the following logic when sending a redirect:

    public void sendRedirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException {
        String redirectUrl = calculateRedirectUrl(request.getContextPath(), url);
        redirectUrl = response.encodeRedirectURL(redirectUrl);
    
        if (logger.isDebugEnabled()) {
            logger.debug("Redirecting to '" + redirectUrl + "'");
        }
    
        response.sendRedirect(redirectUrl);
    }
    

    The sendRedirect method is required to behave in the following way:

    This method can accept relative URLs; the servlet container must convert the relative URL to an absolute URL before sending the response to the client.

    That means you will by deafult always get an absolute URL, no matter what's the configuration or context setting.

    You have multiple options:

    • configure your container or application server to be aware of the public URL (for example by using AJP instead of HTTP reverse proxy, or passing HTTP headers with the public URL which is supported by some application servers), e.g. documentation for Tomcat
    • configure your HTTP reverse proxy to perform correct rewriting, e.g. see ProxyPassReverse in Apache mod_proxy documentation
    • implement a custom org.springframework.security.web.RedirectStrategy where you will manually set the Location response header and HTTP 302 status code, this should allow you to send context relative redirect as you want

提交回复
热议问题