Search code examples
javaurl-rewritingjbossjboss7.xundertow

How to rewrite a RewriteValve for Undertow / JBoss 7.2 EAP?


I'm migrating from JBoss 6.4.3 to JBoss 7.2 and saw a Valves are no longer supported warning during deployment. This came from a jboss-web.xml file with:

<valve>
    <class-name>org.jboss.web.rewrite.RewriteValve</class-name>
</valve>

...and a corresponding rewrite.properties file:

RewriteCond %{HTTP:X-Forwarded-Proto} http
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

Could anyone advise how to rewrite this (no pun intended) for Undertow?


Solution

  • You can create a rewrite filter in the Undertow subsystem, and then reference it in the server host within the configuration file (standalone.xml or domain.xml - depending on the mode in which you start the application server).

    I can think of two options, which might help you:

    1. Using the JBoss Application Server Client (should be placed in path/to/jboss-7.2/bin/)

      • Creating the rewrite filter with the custom name redirect-http-to-https:
      ./jboss-cli.sh --connect --command="/subsystem=undertow/configuration=filter/rewrite=redirect-http-to-https:add(redirect=\"true\",target=\"https://%{LOCAL_SERVER_NAME}%{REQUEST_URL}\")"
      
      • Using/referencing the filter redirect-http-to-https:
      ./jboss-cli.sh --connect --command="/subsystem=undertow/server=default-server/host=default-host/filter-ref=redirect-http-to-https:add(predicate=\"equals(%p,80)\")"
      
    2. Manually editing the respective config file (e.g. standalone.xml)

    <subsystem xmlns="urn:jboss:domain:undertow:11.0" default-server="default-server" [...]>
                <buffer-cache name="default"/>
        <server name="default-server">
            [...]
            <host name="default-host" alias="localhost">
                <filter-ref name="redirect-http-to-https" predicate="equals(%p,80)"/>
            </host>
        </server>
        [...]
        <filters>
            <rewrite name="redirect-http-to-https" target="https://%{LOCAL_SERVER_NAME}%{REQUEST_URL}" redirect="true"/>
        </filters>
    </subsystem>
    

    Note: For the Undertow exchange attributes (e.g. LOCAL_SERVER_NAME) refer to Undertow documentation. Further, the part predicate=\"equals(%p,80)\" in the filter-refchecks the requested port (%p -> just another Undertow exchange attribute) and if it equals to 80, then it triggers our redirect filter redirect-http-to-https - you can change the port 80 as per your need.