Přeskočit na hlavní obsah

Three ways to redirect HTTP requests to HTTPs in WildFly and JBoss EAP

WildFly application server (and JBoss EAP) supports several simple ways how to redirect the communication from plain HTTP to TLS protected HTTPs. This article presents 3 ways. Two are on the application level and the last one is on the server level valid for requests to all deployments.

1. Request confidentiality in the deployment descriptor

The first way is based on the Servlet specification. You need to specify which URLs should be protected in the web.xml deployment descriptor. It's the same approach as the one used for specifying which URLs require authentication/authorization. Just instead of requesting an assigned role, you request a transport-guarantee.

Sample content of the WEB-INF/web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
            <web-resource-name>All confidential</web-resource-name>

As we used the /* URL pattern, all the URLs will request switching from http to https.

2. Request the redirect from Undertow handler in your application

The second approach is similar to the first one. It also depends on the configuration file within the application. Instead of using a Java EE specification, it employs Undertow features. Undertow web server is one of the core components in the WildFly. This approach is lacking detailed description in the WildFly/EAP documentation, but the great starting point is Undertow documentation itself - section Predicates Attributes and Handlers.

What we want to do is to define a handler (a rule) which will all incoming requests having scheme==http redirect to the same URL but with the https scheme used.

So let's simply define the configuration file with the rule WEB-INF/undertow-handlers.conf:

equals(%{SCHEME},"http") -> redirect(https://%v:8443%U%q)

The left equals(...) part is a predicate and the right redirect(...) part is a handler. String templates starting with the percent sign % are called exchange attributes. They may have a long or a short form:

%{LOCAL_SERVER_NAME}  %v     Local server name
%{REQUEST_URL}        %U     Requested URL path
%{QUERY_STRING}       %q     Query string (prepended with a '?' if it exists,
                             otherwise an empty string)
%{SCHEME}                    Request scheme

Some attributes (e.g. %{SCHEME}) are missing in the documentation, you can find them in the code or in the JavaDoc.

As you can see, there is a small disadvantage of this solution. We have to know the https port mapping upfront.

3. Request redirect in the Undertow subsystem

Sometime you may want to have HTTPs used for all the applications. In such cases use the way of configuring the Undertow subsystem within the WildFly configuration. The method is the same as in point 2, just the place where the predicates and handlers (rules) is defined is a different one. The Undertow subsystem in WildFly provides a model for the build-in predicates and handlers. You may use the following JBoss CLI commands:

/subsystem=undertow/configuration=filter/rewrite=http-to-https:add( \
  redirect=true, \

/subsystem=undertow/server=default-server/host=default-host/filter-ref=http-to-https:add( \

So we specified the new rewrite handler with redirect=true. Then we mapped this handler in a new filter-ref resource using it just for requests matching the given predicate.

And that's it. This time we simply resolved the "whats-the-port" issue by using the default https port expression in Wildfly - ${jboss.https.port:8443}.

Bonus - Nginx proxy

Sometime you may want to use another web server. Then you can proxy requests to the WildFly and solve HTTPs redirects completely out of WildFly. Sample config in the Nginx:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    return 301 https://$host$request_uri;
server {
    listen 443 ssl;
    server_name my-server.my-company.com;
    server_tokens off;
    location / {


Populární příspěvky z tohoto blogu

Ignore the boring SSH error message - Host identification has changed!

The problem If you work with virtual machines in clouds, or you run an SSH server in Docker containers, then you've probably met the following error message during making ssh connection: (I'm connecting through SSH to a docker container) ~$ ssh -p 8822 root@localhost @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that a host key has just been changed. The fingerprint for the ECDSA key sent by the remote host is SHA256:smYv5yA0n9/YrBgJMUCk5dYPWGj7bTpU40M9aFBQ72Y. Please contact your system administrator. Add correct host key in /home/jcacek/.ssh/known_hosts to get rid of this message. Offending ECDSA key in /home/jcacek/.ssh/known_hosts:107 remove with: ssh-keygen -f "/home/jcacek/.ssh/know

Enable Elytron in WildFly

Steps to enable Elytron in WildFly nightly builds. There is an ongoing effort to bring a new security subsystem Elytron to WildFly and JBoss EAP. For some time a custom server profile named standalone-elytron.xml  existed beside other profiles in standalone/configuration directory. It was possible to use it for playing with Elytron. The custom Elytron profile was removed now.  The Elytron subsystem is newly introduced to all standard server profiles. The thing is, the Elytron is not used by default and users have to enable it in the subsystems themselves. Let's look into how you can enable it. Get WildFly nightly build # Download WildFly nightly build wget --user=guest --password=guest https://ci.wildfly.org/httpAuth/repository/downloadAll/WF_Nightly/.lastSuccessful/artifacts.zip # unzip build artifacts zip. It contains WildFly distribution ZIP unzip artifacts.zip # get the WildFly distribution ZIP name as property WILDFLY_DIST_ZIP=$(ls wildfly-*-SNAPSHOT.zip) # un