title: Custom web app authentication with WildFly
body: <p>I need to create a custom web app authentication for my Java EE application running on a WildFly 9 server. I have found few information about how to do it in <a href="http://undertow.io/undertow-docs/undertow-docs-1.2.0/index.html#adding-a-custom-authentication-mechanism" rel="nofollow">the undertow website documentation</a>.</p>
  
  <p>What I have done so far is to create a <code>io.undertow.servlet.ServletExtension</code> implementation:</p>
  
  <pre><code>package mypackage;
  
  import java.util.Map;
  import javax.servlet.ServletContext;
  import org.jboss.as.domain.http.server.security.AnonymousMechanism;
  import io.undertow.security.api.AuthenticationMechanism;
  import io.undertow.security.api.AuthenticationMechanismFactory;
  import io.undertow.server.handlers.form.FormParserFactory;
  import io.undertow.servlet.ServletExtension;
  import io.undertow.servlet.api.DeploymentInfo;
  
  public class MyServletExtension implements ServletExtension {
  
      @Override
      public void handleDeployment(DeploymentInfo deploymentInfo, ServletContext servletContext) {
          deploymentInfo.addAuthenticationMechanism("MY-AUTH", new AuthenticationMechanismFactory() {
              @Override
              public AuthenticationMechanism create(String mechanismName, FormParserFactory formParserFactory, Map&lt;String, String&gt; properties) {
                  return new AnonymousMechanism();
              }
          });
      }
  }
  </code></pre>
  
  <p>Then I added a file named io.undertow.servlet.ServletExtension in src/main/resources/META-INF/services/ with the content: <code>mypackage.MyServletExtension</code>.</p>
  
  <p>Finally, I have modified my web.xml file like this:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"&gt;
  
      &lt;display-name&gt;my-app&lt;/display-name&gt;
  
      &lt;security-constraint&gt;
          &lt;web-resource-collection&gt;
              &lt;web-resource-name&gt;restricted resources&lt;/web-resource-name&gt;
              &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
          &lt;/web-resource-collection&gt;
          &lt;auth-constraint&gt;
              &lt;role-name&gt;*&lt;/role-name&gt;
          &lt;/auth-constraint&gt;
      &lt;/security-constraint&gt;
  
      &lt;security-role&gt;
          &lt;role-name&gt;*&lt;/role-name&gt;
      &lt;/security-role&gt;
  
      &lt;login-config&gt;
          &lt;auth-method&gt;MY-AUTH&lt;/auth-method&gt;
      &lt;/login-config&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>In my pom.xml file I have those dependencies:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;dependency&gt;
      &lt;groupId&gt;io.undertow&lt;/groupId&gt;
      &lt;artifactId&gt;undertow-core&lt;/artifactId&gt;
      &lt;version&gt;1.2.9.Final&lt;/version&gt;
      &lt;scope&gt;provided&lt;/scope&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;io.undertow&lt;/groupId&gt;
      &lt;artifactId&gt;undertow-servlet&lt;/artifactId&gt;
      &lt;version&gt;1.2.9.Final&lt;/version&gt;
      &lt;scope&gt;provided&lt;/scope&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>But at server startup I have this error: </p>
  
  <pre><code>09:40:20,192 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 27) MSC000001: Failed to start service jboss.undertow.deployment.default-server.default-host./my-app-web: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./my-app-web: java.lang.LinkageError: Failed to link mypackage/MyServletExtension (Module "deployment.my-app-0.0.1-SNAPSHOT.ear.my-app-ejb.jar:main" from Service Module Loader)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:85)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [rt.jar:1.8.0_60]
      at java.util.concurrent.FutureTask.run(FutureTask.java:266) [rt.jar:1.8.0_60]
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_60]
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_60]
      at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_60]
      at org.jboss.threads.JBossThread.run(JBossThread.java:320) [jboss-threads-2.2.0.Final.jar:2.2.0.Final]
  Caused by: java.lang.LinkageError: Failed to link mypackage/MyServletExtension (Module "deployment.my-app-0.0.1-SNAPSHOT.ear.my-app-ejb.jar:main" from Service Module Loader)
      at org.jboss.modules.ModuleClassLoader.defineClass(ModuleClassLoader.java:437) [jboss-modules.jar:1.4.3.Final]
      at org.jboss.modules.ModuleClassLoader.loadClassLocal(ModuleClassLoader.java:269) [jboss-modules.jar:1.4.3.Final]
      at org.jboss.modules.ModuleClassLoader$1.loadClassLocal(ModuleClassLoader.java:77) [jboss-modules.jar:1.4.3.Final]
      at org.jboss.modules.Module.loadModuleClass(Module.java:560) [jboss-modules.jar:1.4.3.Final]
      at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:197) [jboss-modules.jar:1.4.3.Final]
      at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:455) [jboss-modules.jar:1.4.3.Final]
      at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:404) [jboss-modules.jar:1.4.3.Final]
      at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:385) [jboss-modules.jar:1.4.3.Final]
      at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:130) [jboss-modules.jar:1.4.3.Final]
      at java.lang.Class.forName0(Native Method) [rt.jar:1.8.0_60]
      at java.lang.Class.forName(Class.java:348) [rt.jar:1.8.0_60]
      at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:370) [rt.jar:1.8.0_60]
      at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404) [rt.jar:1.8.0_60]
      at java.util.ServiceLoader$1.next(ServiceLoader.java:480) [rt.jar:1.8.0_60]
      at io.undertow.servlet.core.DeploymentManagerImpl.handleExtensions(DeploymentManagerImpl.java:243)
      at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:150)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:100)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:82)
      ... 6 more
  Caused by: java.lang.NoClassDefFoundError: io/undertow/servlet/ServletExtension
      at java.lang.ClassLoader.defineClass1(Native Method) [rt.jar:1.8.0_60]
      at java.lang.ClassLoader.defineClass(ClassLoader.java:760) [rt.jar:1.8.0_60]
      at org.jboss.modules.ModuleClassLoader.doDefineOrLoadClass(ModuleClassLoader.java:353) [jboss-modules.jar:1.4.3.Final]
      at org.jboss.modules.ModuleClassLoader.defineClass(ModuleClassLoader.java:432) [jboss-modules.jar:1.4.3.Final]
      ... 23 more
  Caused by: java.lang.ClassNotFoundException: io.undertow.servlet.ServletExtension from [Module "deployment.my-app-0.0.1-SNAPSHOT.ear.my-app-ejb.jar:main" from Service Module Loader]
      at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:205) [jboss-modules.jar:1.4.3.Final]
      at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:455) [jboss-modules.jar:1.4.3.Final]
      at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:404) [jboss-modules.jar:1.4.3.Final]
      at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:385) [jboss-modules.jar:1.4.3.Final]
      at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:130) [jboss-modules.jar:1.4.3.Final]
      ... 27 more
  </code></pre>
  
  <p>When I change the scope of dependencies from provided to compile, I have another exception: </p>
  
  <pre><code>10:08:01,387 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 29) MSC000001: Failed to start service jboss.undertow.deployment.default-server.default-host./my-app-web: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./my-app-web: java.util.ServiceConfigurationError: io.undertow.servlet.ServletExtension: Provider mypackage.MyServletExtension not a subtype
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:85)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [rt.jar:1.8.0_60]
      at java.util.concurrent.FutureTask.run(FutureTask.java:266) [rt.jar:1.8.0_60]
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_60]
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_60]
      at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_60]
      at org.jboss.threads.JBossThread.run(JBossThread.java:320) [jboss-threads-2.2.0.Final.jar:2.2.0.Final]
  Caused by: java.util.ServiceConfigurationError: io.undertow.servlet.ServletExtension: Provider mypackage.MyServletExtension not a subtype
      at java.util.ServiceLoader.fail(ServiceLoader.java:239) [rt.jar:1.8.0_60]
      at java.util.ServiceLoader.access$300(ServiceLoader.java:185) [rt.jar:1.8.0_60]
      at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:376) [rt.jar:1.8.0_60]
      at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404) [rt.jar:1.8.0_60]
      at java.util.ServiceLoader$1.next(ServiceLoader.java:480) [rt.jar:1.8.0_60]
      at io.undertow.servlet.core.DeploymentManagerImpl.handleExtensions(DeploymentManagerImpl.java:243)
      at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:150)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:100)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:82)
      ... 6 more
  </code></pre>
  
  <p>Can someone tell me what is wrong with my configuration ?</p>