我最终使用了过滤器。
从基础设施上看,这似乎是最简单的方法
过滤器实现:
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