2012-05-13 13 views
2

パスとGET変数を保存して、Webアプリケーションから別のサーバにリダイレクトする方法を試しています。spring mvcパスとすべての子ドメインを別のドメインにリダイレクトする

例えば

www.myapp.com/foo 
foo.com 

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

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

私はidealyちょうどこのような何かが、おそらくApacheのを介して仮想ホストで実行する必要があります

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

答えて

1

私は、フィルタを使用して終了:

はここにいくつかのドキュメントへのリンクです。

infrastructurallyこれが最も簡単な方法

フィルタの実装のようだ:私は

可能にするために2 URLパターンを追加するために必要な

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> 

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

のようなものを使用したいと思います。

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

+0

理想的には、この方程式にapache httpdを含める必要はありません – kabal

+0

すべてのSpring MVCはサーブレット用のWebフレームワークを提供するため、これを行う方法がSpring MVCだけであるとは思いません。サーブレットコンテナレベル(つまり、Tomcat、またはApacheがプロキシ経由でTomcatにリダイレクトする)で何かを行う必要があります。 – dardo

1

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); 
     } 
    } 
} 

これは明らかに、ハンドラチェーン内のコンテンツハンドラが有効になる前に行う必要があります。

関連する問題