Jetty 9.3.6: No LoginService for org.eclipse.jetty.security.authentication.BasicAuthenticator
<p>I am trying to provide HTTP BASIC authentication for my webapp running on Jetty 9.3.6. I followed the instructions in <a href="http://www.eclipse.org/jetty/documentation/9.2.6.v20141205/configuring-security-authentication.html" rel="nofollow">here</a> and it appeared like my authentication mechanism should be based out of JAAS. But my auth fails for a <code>No LoginService</code> error (see details below).</p>

<p>I followed the instructions in <a href="http://www.eclipse.org/jetty/documentation/9.2.6.v20141205/jaas-support.html" rel="nofollow">here</a> and ended up with this <code>src/main/webapp/WEB-INF/web.xml</code> in my gradle sources:</p>

<pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&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"
metadata-complete="false"
version="3.1"&gt;

  &lt;login-config&gt;
    &lt;auth-method&gt;BASIC&lt;/auth-method&gt;
    &lt;realm-name&gt;myRealm&lt;/realm-name&gt;
 &lt;/login-config&gt;

&lt;/web-app&gt;
</code></pre>

<p>And I had to also add a context descriptor which I place in <code>src/main/webapp/myWebApp.xml</code> (this unwraps in <code>${jetty.base}/webapp/</code> and the <code>web.xml</code> unwraps in <code>${jetty.base}/webapp/WEB-INF/</code> though they  are unwrapped when I deploy this war even though I choose to <code>extractWAR</code> below):</p>

<p>
</p>

<pre><code>&lt;Configure id='wac' class="org.eclipse.jetty.webapp.WebAppContext"&gt;

  &lt;Set name="contextPath"&gt;/myWebApp&lt;/Set&gt;
  &lt;Set name="war"&gt;/opt/jetty/someJettyBase/webapps/myWebApp.war&lt;/Set&gt;
  &lt;Set name="extractWAR"&gt;true&lt;/Set&gt;

  &lt;Set name="securityHandler"&gt;
    &lt;New class="org.eclipse.jetty.security.ConstraintSecurityHandler"&gt;
     &lt;Set name="loginService"&gt;
       &lt;New class="org.eclipse.jetty.jaas.JAASLoginService"&gt;
         &lt;Set name="name"&gt;myRealm&lt;/Set&gt;
         &lt;Set name="loginModuleName"&gt;xyz&lt;/Set&gt;
       &lt;/New&gt;
     &lt;/Set&gt;
    &lt;/New&gt;
  &lt;/Set&gt;

&lt;/Configure&gt;
</code></pre>

<p>To tie the JAAS to my login module, I do this in <code>${jetty.base}/etc/login.conf</code> in my jetty deployment:</p>

<pre><code>xyz {
com.webapp.MyLoginService required debug=true;
};
</code></pre>

<p>And lastly, I added <code>jaas</code> as a service to jetty using </p>

<pre><code>$ java -jar --add-to-startd=jaas
</code></pre>

<p>and confirmed that it worked (file in <code>${jetty.base}/startd/jaas.ini</code>).</p>

<p>Unfortunately, when I start Jetty, I see that it has not correctly worked, as I see this:</p>

<pre><code>19 Jan 2016 21:29:08.811 WARN  org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:526) Failed startup of context o.e.j.w.WebAppContext@27c20538{/myWebApp,file:///tmp/jetty-0.0.0.0-443-myWebApp.war-_myWebApp-any-8865406277141986908.dir/webapp/,STARTING}{/myWebApp.war}
java.lang.IllegalStateException: No LoginService for org.eclipse.jetty.security.authentication.BasicAuthenticator@5762806e in org.eclipse.jetty.security.ConstraintSecurityHandler@17c386de
    at org.eclipse.jetty.security.authentication.LoginAuthenticator.setConfiguration(LoginAuthenticator.java:76)
    at org.eclipse.jetty.security.SecurityHandler.doStart(SecurityHandler.java:354)
    at org.eclipse.jetty.security.ConstraintSecurityHandler.doStart(ConstraintSecurityHandler.java:448)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
</code></pre>

<p>MyLoginService.java:</p>

<pre><code>package com.webapp;
import javax.servlet.ServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.eclipse.jetty.security.LoginService;
import org.eclipse.jetty.security.IdentityService;
import org.eclipse.jetty.server.UserIdentity;
import org.eclipse.jetty.security.DefaultUserIdentity;

public class MyLoginService implements LoginService
{
    private static final Logger log = LoggerFactory.getLogger (MyLoginService.class.getName ());

    private IdentityService identityService;

    public MyLoginService (IdentityService identityService)
    {
        this.identityService = identityService;
    }

    @Override
    public String getName()
    {
      return "name";
    }

    @Override
    public UserIdentity login(String username, Object credentials, ServletRequest request)
    {
        log.debug ("MyLoginService.login: username received {}", username);
        return null;
    }

    @Override
    public boolean validate(UserIdentity user)
    {
        return false;
    }

    @Override
    public IdentityService getIdentityService()
    {
        return identityService;
     }

     @Override
     public void setIdentityService(IdentityService service)
      {
      }

     @Override
     public void logout(UserIdentity user)
     {
     }
}
</code></pre>

<p>Any insights into this is deeply appreciated.</p>

<p>What am I missing?</p>

<p>Thanks!</p>