【问题标题】:How do I properly set up Tuckey's UrlRewriteFilter?如何正确设置 Tuckey 的 UrlRewriteFilter?
【发布时间】:2012-01-14 09:08:57
【问题描述】:

所以我希望使用 Tuckey 的 UrlRewriteFilter 来重定向尝试访问旧版本 Web 服务产品的用户。简单的东西,比如将 www.blah.com/oldversion/blah/blah 重定向到 www.blah.com/newversion/blah/blah

我花了一些时间尝试根据 Tuckey 的 website 上有些模糊的说明正确设置它,但遇到了一些困难。

我的文件位于正确的位置。 urlrewrite.xml 位于 webapp 包的 WEB-INF 文件夹中,与 web.xml 相邻。 urlrewrite-3.2.0.jar 在 WEB-INF 的 lib 文件夹中。

这是我的 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<filter>
   <filter-name>UrlRewriteFilter</filter-name>
   <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
   <filter-name>UrlRewriteFilter</filter-name>
   <url-pattern>/*</url-pattern>
   <dispatcher>REQUEST</dispatcher>
   <dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter>
    <filter-name>Jersey Web Application</filter-name>
    <filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
    <init-param>
        <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
        <param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
    </init-param>
    <!--
    <init-param>
        <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
        <param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
    </init-param>
    -->
    <init-param>
        <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
        <param-value>project's.security.filter</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>project's.base.package</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>Jersey Web Application</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<ejb-local-ref>
    REFERENCED EJB
</ejb-local-ref>

<ejb-local-ref>
    REFERENCED EJB
</ejb-local-ref>
</web-app>

这是我的 urlrewrite.xml:

<?xml version="1.0" encoding="utf-8"?>
<!--
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN"
    "http://tuckey.org/res/dtds/urlrewrite3.2.dtd">-->


<!--

Configuration file for UrlRewriteFilter
http://tuckey.org/urlrewrite/

-->
<urlrewrite>

<rule>
    <from>^/2.0/rest/$</from>
    <to type="redirect">/3.2-SNAPSHOT/rest/$1</to>
</rule>
<rule>
    <note>
        The rule means that requests to /test/status/ will be redirected to /rewrite-status
        the url will be rewritten.
    </note>
    <from>/test/status/</from>
    <to type="redirect">%{context-path}/rewrite-status</to>
</rule>


<outbound-rule>
    <note>
        The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url)
        the url /rewrite-status will be rewritten to /test/status/.

        The above rule and this outbound-rule means that end users should never see the
        url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks
        in your pages.
    </note>
    <from>/rewrite-status</from>
    <to>/test/status/</to>
</outbound-rule>

</urlrewrite>

当我构建和部署项目时,这些与 UrlRewriteFilter 相关的行会打印在服务器日志中:

[#|2011-12-06T10:28:56.924-0500|INFO|glassfish3.1.1|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=24;_ThreadName=Thread-2;|PWC1412: WebModule[null] ServletContext.log():org.tuckey.web.filters.urlrewrite.UrlRewriteFilter INFO: destroy called|#]
...
[#|2011-12-06T10:29:04.069-0500|INFO|glassfish3.1.1|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=24;_ThreadName=Thread-2;|PWC1412: WebModule[null] ServletContext.log():org.tuckey.web.filters.urlrewrite.UrlRewriteFilter INFO: loaded (conf ok)|#]

当我尝试访问 2.0 的 url 时,它应该将我重定向到 3.2-SNAPSHOT。相反,我收到 404 错误。 server.log 中没有显示任何内容,并且在我的 java 错误日志中,我看到 UrlRewriteFilter 包从未被触及。所以我认为我的设置一定有问题。

如果您需要更多信息,请告诉我,感谢您的时间和帮助。

【问题讨论】:

    标签: java web-services url-rewriting tuckey-urlrewrite-filter


    【解决方案1】:

    问题最终与我的 ear 包的 pom.xml 有关。在那里,应用程序的上下文根被设置为包含我试图更改为的 URI 片段。因为只有在上下文根匹配时才会访问应用程序,所以当然不能使用 tukey。

    例如:用户输入网址 www.blank.com/2.0/etc,我想重定向到 www.blank.com/3.0/etc

    不正确的耳朵 pom sn-p:

    <webModule>
        <groupId>com.etc</groupId>
        <artifactId>webservice-rest-webapp</artifactId>
        <contextRoot>/3.0/</contextRoot>
    </webModule>
    

    正确的耳朵 pom sn-p:

    <webModule>
        <groupId>com.etc</groupId>
        <artifactId>webservice-rest-webapp</artifactId>
        <contextRoot>/</contextRoot>
    </webModule>
    

    因为我不再指定上下文根,我当然必须修改整个程序中各种 URI 的路径参数以包含版本号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      • 2011-04-10
      • 1970-01-01
      • 2014-09-07
      • 2018-06-22
      相关资源
      最近更新 更多