spring mvc 重定向路径和所有子节点到另一个域

Posted

技术标签:

【中文标题】spring mvc 重定向路径和所有子节点到另一个域【英文标题】:spring mvc redirect path and all children to another domain 【发布时间】:2012-05-21 01:55:29 【问题描述】:

我正在尝试解决如何从我的 web 应用重定向到另一台服务器,保留所有路径和 GET 变量。

例如

www.myapp.com/foo
foo.com

www.myapp.com/foo/bar
foo.com/bar

www.myapp.com/foo?bar=1
foo.com?bar=1

理想情况下,我只想使用类似的东西

<mvc:view-controller path="/foo/**" view-name="redirect:http://foo.com**" />

【问题讨论】:

【参考方案1】:

我最终使用了过滤器。

从基础设施上看,这似乎是最简单的方法

过滤器实现:

public class DomainRedirectFilter extends OncePerRequestFilter 

    private String destinationDomain;
    private String sourceServletPath;

    @Override
    protected void doFilterInternal(HttpServletRequest request, 
             HttpServletResponse response, FilterChain filterChain)
             throws ServletException, IOException 
        String path = request.getServletPath();
        path = StringUtils.replace(path, getSourceServletPath(), "");
        if (request.getQueryString() != null) 
            path += '?' + request.getQueryString();
        

        response.setHeader( "Location", getDestinationDomain() + path );
        response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
        response.setHeader( "Connection", "close" );
    

web.xml

<filter>
    <filter-name>fooDomainRedirectFilter</filter-name>
    <filter-class>com.abc.mvc.util.DomainRedirectFilter</filter-class>
    <init-param>
        <param-name>destinationDomain</param-name>
        <param-value>http://foo.abc.com</param-value>
    </init-param>
    <init-param>
        <param-name>sourceServletPath</param-name>
        <param-value>/foo</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>fooDomainRedirectFilter</filter-name>
    <url-pattern>/foo/*</url-pattern>
    <url-pattern>/foo</url-pattern>
</filter-mapping>

我需要添加 2 个 url 模式以允许

/foo
/foo?id=1
/foo/bar
/foo/bar?id=1

【讨论】:

【参考方案2】:

如果您使用Jetty 之类的名称,您也可以使用Handler 来执行此操作。

public class DomainRedirectHandler extends HandlerWrapper 

    @Override
    public void handle(String target, Request baseRequest, HttpServletRequest request,
            HttpServletResponse response) throws IOException, ServletException 

        String hostName = request.getHeader("Host");
        if (hostName == null) 
            getHandler().handle(target, baseRequest, request, response);
            return;
        

        // see if the host header has a domain name that we are redirecting
        hostName = hostName.toLowerCase();
        int index = hostName.indexOf(':');
        if (index >= 0) 
            // cut off the optional port suffix
            hostName = hostName.substring(0, index);
        

        if (hostName.equals("some.domain.com")) 
            response.sendRedirect("https://some.other.domain.com");
         else 
            getHandler().handle(target, baseRequest, request, response);
        
    

这显然需要在处理程序链中的内容处理程序之前生效。

【讨论】:

【参考方案3】:

这样的事情可能应该通过 Apache 使用虚拟主机来完成。

这里是一些文档的链接:

http://httpd.apache.org/docs/2.0/vhosts/examples.html

【讨论】:

理想情况下,我们不希望在这个等式中涉及 apache httpd 我不相信仅使用 Spring MVC 就可以做到这一点,因为 Spring MVC 所做的只是为 servlet 提供 Web 框架。必须在 servlet 容器级别(即 Tomcat,或 Apache 通过代理重定向到 Tomcat)做一些事情。

以上是关于spring mvc 重定向路径和所有子节点到另一个域的主要内容,如果未能解决你的问题,请参考以下文章