title: Wildfly 8 Basic Authentication
body: <p>I got a Java-Webapp running on Wildfly 8. I try to secure my restful webservice with resteasy Annotations. I use the command line tool curl to test the rest api. </p>
  
  <p>The basic authentication setup seems to work. Http requests to Webservices with annotation "@PermitAll" work fine. Curl says:</p>
  
  <pre><code>~ % curl -v http://localhost:8080/ItilityServer-web/rest/account 
  &gt; GET /ItilityServer-web/rest/account HTTP/1.1
  &gt; User-Agent: curl/7.40.0
  &gt; Host: localhost:8080
  &gt; Accept: */*
  &gt; 
  &lt; HTTP/1.1 200 OK
  &lt; Connection: keep-alive
  &lt; X-Powered-By: Undertow/1
  &lt; Server: WildFly/8
  &lt; Content-Length: 0
  &lt; Date: Wed, 28 Jan 2015 10:47:11 GMT
  &lt; 
  * Connection #0 to host localhost left intact
  </code></pre>
  
  <p>But http requests containing valid username and password get rejected with status code 401 not authorized. Wildfly logs the error unmatched password:</p>
  
  <pre><code>2015-01-28 11:42:43,565 TRACE [org.jboss.security] (default task-5) PBOX000263: Executing query SELECT a.password FROM Account a WHERE a.name = ? with username hans
  2015-01-28 11:42:43,566 DEBUG [org.jboss.security] (default task-5) PBOX000283: Bad password for username hans
  </code></pre>
  
  <p>But this is not true. The decrypted authorization details "aGFuczpoZWxtaWhlbG1paGVsbWk=" are hans:helmihelmihelmi and this username and password are stored in my db. The same jpql-queries as the security-domain uses result in an unsername "hans" and his password "helmihelmihelmi".</p>
  
  <p>Here my setup:</p>
  
  <p>web.xml</p>
  
  <pre><code>&lt;web-app xmlns="http://java.sun.com/xml/ns/javaee"
       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"&gt;
  
  &lt;context-param&gt;
      &lt;param-name&gt;resteasy.role.based.security&lt;/param-name&gt;
      &lt;param-value&gt;true&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;login-config&gt;
      &lt;auth-method&gt;BASIC&lt;/auth-method&gt;
      &lt;realm-name&gt;Application&lt;/realm-name&gt;
  &lt;/login-config&gt;
  
  &lt;security-role&gt;
      &lt;role-name&gt;store&lt;/role-name&gt;
  &lt;/security-role&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>(I don't know what security realms are, so I just left this property in the login-config tag)</p>
  
  <p>My security domain in standalone.xml</p>
  
  <pre><code>&lt;security-domain name="DBLogin" cache-type="default"&gt;
      &lt;authentication&gt;
          &lt;login-module code="Database" flag="required"&gt;
              &lt;module-option name="dsJndiName" value="java:jboss/datasources/ExampleDS"/&gt;
              &lt;module-option name="principalsQuery" value="SELECT a.password FROM Account a WHERE a.name = ?"/&gt;
              &lt;module-option name="rolesQuery" value="SELECT a.userRole FROM Account a WHERE a.name = ?"/&gt;
              &lt;module-option name="hashAlgorithm" value="SHA-256"/&gt;
              &lt;module-option name="hashEncoding" value="Base64"/&gt;
              &lt;module-option name="hashCharset" value="UTF-8"/&gt;
              &lt;module-option name="unauthenticatedIdentity" value="guest"/&gt;
          &lt;/login-module&gt;
      &lt;/authentication&gt;
  &lt;/security-domain&gt;
  </code></pre>
  
  <p>Restful webservice</p>
  
  <pre><code>@GET
  @RolesAllowed(AuthRole.STORE)
  @Produces(MediaType.APPLICATION_JSON)
  public Response getAccountByName() {
      Response.ResponseBuilder builder = Response.ok();
      return builder.build();
  }
  </code></pre>
  
  <p>and import.xml to create user on startup</p>
  
  <pre><code>insert into Account(id, name, email, password, user_role) values (0, 'hans', 'john.smith@mailinator.com', 'helmihelmihelmi', 'store') 
  insert into Store(id, name, zipcode, street, housenumber, town, account_id) values(0, 'Edeka', 72622, 'stephanstraße', 10, 'Reudern', 0);
  </code></pre>
  
  <p>Don't know how to find a solution, because I don't even know the problem. Hope somebody can help.</p>