title: JBoss Wildfly - database login module
body: <p>JBoss Wildfly 8.0.0-Final<br>
  JSF 2.2.4</p>
  
  <p>First I created login using the application-users.properties and application-roles.properties.
  Added user with add-user.bat</p>
  
  <p>Web.xml</p>
  
  <pre><code>&lt;security-constraint&gt;
      &lt;web-resource-collection&gt;
          &lt;web-resource-name&gt;Admin Resource&lt;/web-resource-name&gt;
          &lt;url-pattern&gt;/admin/*&lt;/url-pattern&gt;
      &lt;/web-resource-collection&gt;
      &lt;auth-constraint&gt;
          &lt;role-name&gt;admin&lt;/role-name&gt;
      &lt;/auth-constraint&gt;
      &lt;user-data-constraint&gt;
          &lt;transport-guarantee&gt;NONE&lt;/transport-guarantee&gt;
      &lt;/user-data-constraint&gt;
  &lt;/security-constraint&gt;
  
  &lt;login-config&gt;
      &lt;auth-method&gt;FORM&lt;/auth-method&gt;
      &lt;form-login-config&gt;
          &lt;form-login-page&gt;/login.xhtml&lt;/form-login-page&gt;
          &lt;form-error-page&gt;/error.xhtml&lt;/form-error-page&gt;
      &lt;/form-login-config&gt;
  &lt;/login-config&gt;
  
  &lt;security-role&gt;
      &lt;role-name&gt;admin&lt;/role-name&gt;
  &lt;/security-role&gt;
  </code></pre>
  
  <p>Standalone.xml</p>
  
  <pre><code>&lt;login-module code="Remoting" flag="optional"&gt;
  &lt;module-option name="password-stacking" value="useFirstPass"/&gt;
  &lt;/login-module&gt;
  &lt;login-module code="RealmDirect" flag="required"&gt;
  &lt;module-option name="password-stacking" value="useFirstPass"/&gt;
  &lt;/login-module&gt;
  </code></pre>
  
  <p>login.xhtml</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ui:composition xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui"&gt;
      &lt;div class="center"&gt;
          &lt;form method="POST" action="j_security_check" id=""&gt;
              &lt;h:panelGrid id="panel" columns="2" border="1" cellpadding="4" cellspacing="4"&gt;
                  &lt;h:outputLabel for="j_username" value="Username:" /&gt;
                  &lt;input type="text" name="j_username" /&gt;
                  &lt;h:outputLabel for="j_password" value="Password:" /&gt;
                  &lt;input type="password" name="j_password" /&gt;
                  &lt;h:panelGroup&gt;
                      &lt;input type="submit" value="Login" /&gt;
                  &lt;/h:panelGroup&gt;
              &lt;/h:panelGrid&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/ui:composition&gt;
  </code></pre>
  
  <p>So that worked fine.. now I want to use database authentication.. so I change the standalone.xml. </p>
  
  <pre><code>&lt;login-module code="Database" flag="sufficient"&gt;
      &lt;module-option name="dsJndiName" value="java:jboss/jsi/GarageXADataSource"/&gt;
      &lt;module-option name="principalsQuery" value="select encode(password, 'hex') from principal where username=?"/&gt;
      &lt;module-option name="rolesQuery" value="select r.role, r.role_group from role r inner join principal p on r.role = p.role where p.username=?"/&gt;
      &lt;module-option name="hashAlgorithm" value="SHA-512"/&gt;
      &lt;module-option name="hashEncoding" value="hex"/&gt;
  &lt;/login-module&gt;
  </code></pre>
  
  <p>I use this sql to insert a role and a user in the database ( PostgreSQL 9.3 )</p>
  
  <blockquote>
    <p>INSERT INTO role(role, role_group) VALUES ('admin', 'Roles'); <br> INSERT
    INTO principal(username, email, password, role) VALUES ('Kris',
    'xx@gmail.com', digest('pass', 'sha512'), 'admin');</p>
  </blockquote>
  
  <p>But the login does not work. 
  I see no errors in the log. 
  I have used this approach before on AS 7.1.1 where it worked.</p>
  
  <p>Thanks for you help. </p>