title: Setting up CAS Jasig Server with database authentication
body: <p>I am trying to setup Single Sign-On via a  CAS server. I want to use database authentication as I have existing applications using spring security that authenticate through MySQL. </p>
  
  <p>I am following the instructions on the <a href="http://jasig.github.io/cas/4.0.x/installation/Configuring-Authentication-Components.html" rel="nofollow">official site</a>, using their base maven overlay project as my starting point. Adding a database authentication handler seems straightforward from the instructions on the site - define the beans( db handler, password encoder and datasource) and add the handler to the list of handlers on the authenticationManager bean. </p>
  
  <p>When I try to login on /cas/login, nothing happens. No query hits the database, as if the handler is not considered at all. The web page does not spit out an error about wrong credentials. The tomcat log does not give any errors or any signs of whats wrong and I am at a loss. What else do I need to setup?</p>
  
  <p>My deployerConfigContext.xml is below. I have only edited/added stuff up to passwordEncoder. Everything else is from the default config that comes with the maven overlay project. </p>
  
  <p>Thank you for your help and Happy New Year!</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:p="http://www.springframework.org/schema/p"
     xmlns:c="http://www.springframework.org/schema/c"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:util="http://www.springframework.org/schema/util"
     xmlns:sec="http://www.springframework.org/schema/security"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
     http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"&gt;
  
  &lt;!--
     | The authentication manager defines security policy for authentication by specifying at a minimum
     | the authentication handlers that will be used to authenticate credential. While the AuthenticationManager
     | interface supports plugging in another implementation, the default PolicyBasedAuthenticationManager should
     | be sufficient in most cases.
     +--&gt;
  &lt;bean id="authenticationManager" class="org.jasig.cas.authentication.PolicyBasedAuthenticationManager"&gt;
      &lt;constructor-arg&gt;
          &lt;list&gt;
          &lt;ref bean="dbAuthHandler" /&gt;
       &lt;/list&gt;
      &lt;/constructor-arg&gt;
  
      &lt;!-- Uncomment the metadata populator to allow clearpass to capture and cache the password
           This switch effectively will turn on clearpass.
      &lt;property name="authenticationMetaDataPopulators"&gt;
         &lt;util:list&gt;
            &lt;bean class="org.jasig.cas.extension.clearpass.CacheCredentialsMetaDataPopulator"
                  c:credentialCache-ref="encryptedMap" /&gt;
         &lt;/util:list&gt;
      &lt;/property&gt;
      --&gt;
  
      &lt;!--
         | Defines the security policy around authentication. Some alternative policies that ship with CAS:
         |
         | * NotPreventedAuthenticationPolicy - all credential must either pass or fail authentication
         | * AllAuthenticationPolicy - all presented credential must be authenticated successfully
         | * RequiredHandlerAuthenticationPolicy - specifies a handler that must authenticate its credential to pass
         --&gt;
      &lt;property name="authenticationPolicy"&gt;
          &lt;bean class="org.jasig.cas.authentication.AnyAuthenticationPolicy" /&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;!--
     | TODO: Replace this component with one suitable for your enviroment.
     |
     | This component provides authentication for the kind of credential used in your environment. In most cases
     | credential is a username/password pair that lives in a system of record like an LDAP directory.
     | The most common authentication handler beans:
     |
     | * org.jasig.cas.authentication.LdapAuthenticationHandler
     | * org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler
     | * org.jasig.cas.adaptors.x509.authentication.handler.support.X509CredentialsAuthenticationHandler
     | * org.jasig.cas.support.spnego.authentication.handler.support.JCIFSSpnegoAuthenticationHandler
     --&gt;
  &lt;bean id="dbAuthHandler"    class="org.jasig.cas.adaptors.jdbc.SearchModeSearchDatabaseAuthenticationHandler"&gt;
    &lt;property name="dataSource" ref="secureDataSource"&gt;&lt;/property&gt;
    &lt;property name="passwordEncoder" ref="passwordEncoder"&gt;&lt;/property&gt;
    &lt;property name="tableUsers" value="sec_user"&gt;&lt;/property&gt;
    &lt;property name="fieldUser" value="username"&gt;&lt;/property&gt;
    &lt;property name="fieldPassword" value="password"&gt;&lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="secureDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;
     &lt;property name="driverClassName" value="${secure.db.driver}"/&gt;
     &lt;property name="url" value="${secure.db.url}"/&gt;
     &lt;property name="username" value="${secure.db.username}"/&gt;
     &lt;property name="password" value="${secure.db.password}"/&gt;
     &lt;property name="maxActive" value="${secure.db.max.active}"/&gt;
     &lt;property name="maxIdle" value="${secure.db.max.idle}"/&gt;
     &lt;property name="minIdle" value="${secure.db.min.idle}"/&gt;
     &lt;property name="testOnBorrow" value="${secure.db.testOnBorrow}"&gt;&lt;/property&gt;
     &lt;property name="validationQuery" value="select 1"&gt;&lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="passwordEncoder" class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder"&gt;
     &lt;constructor-arg value="SHA256"&gt;&lt;/constructor-arg&gt;
     &lt;property name="characterEncoding" value="UTF-8"&gt;&lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;!-- Required for proxy ticket mechanism --&gt;
  &lt;bean id="proxyPrincipalResolver"
        class="org.jasig.cas.authentication.principal.BasicPrincipalResolver" /&gt;
  
  &lt;!--
     | Resolves a principal from a credential using an attribute repository that is configured to resolve
     | against a deployer-specific store (e.g. LDAP).
     --&gt;
  &lt;bean id="primaryPrincipalResolver"
        class="org.jasig.cas.authentication.principal.PersonDirectoryPrincipalResolver" &gt;
      &lt;property name="attributeRepository" ref="attributeRepository" /&gt;
  &lt;/bean&gt;
  
  &lt;!--
  Bean that defines the attributes that a service may return.  This example uses the Stub/Mock version.  A real implementation
  may go against a database or LDAP server.  The id should remain "attributeRepository" though.
  +--&gt;
  &lt;bean id="attributeRepository" class="org.jasig.services.persondir.support.StubPersonAttributeDao"
          p:backingMap-ref="attrRepoBackingMap" /&gt;
  
  &lt;util:map id="attrRepoBackingMap"&gt;
      &lt;entry key="uid" value="uid" /&gt;
      &lt;entry key="eduPersonAffiliation" value="eduPersonAffiliation" /&gt; 
      &lt;entry key="groupMembership" value="groupMembership" /&gt;
  &lt;/util:map&gt;
  
  &lt;!-- 
  Sample, in-memory data store for the ServiceRegistry. A real implementation
  would probably want to replace this with the JPA-backed ServiceRegistry DAO
  The name of this bean should remain "serviceRegistryDao".
  +--&gt;
  &lt;bean id="serviceRegistryDao" class="org.jasig.cas.services.InMemoryServiceRegistryDaoImpl"
          p:registeredServices-ref="registeredServicesList" /&gt;
  
  &lt;util:list id="registeredServicesList"&gt;
      &lt;bean class="org.jasig.cas.services.RegexRegisteredService"
            p:id="0" p:name="HTTP and IMAP" p:description="Allows HTTP(S) and IMAP(S) protocols"
            p:serviceId="^(https?|imaps?)://.*" p:evaluationOrder="10000001" /&gt;
      &lt;!--
      Use the following definition instead of the above to further restrict access
      to services within your domain (including sub domains).
      Note that example.com must be replaced with the domain you wish to permit.
      This example also demonstrates the configuration of an attribute filter
      that only allows for attributes whose length is 3.
      --&gt;
      &lt;!--
      &lt;bean class="org.jasig.cas.services.RegexRegisteredService"&gt;
          &lt;property name="id" value="1" /&gt;
          &lt;property name="name" value="HTTP and IMAP on example.com" /&gt;
          &lt;property name="description" value="Allows HTTP(S) and IMAP(S) protocols on example.com" /&gt;
          &lt;property name="serviceId" value="^(https?|imaps?)://([A-Za-z0-9_-]+\.)*example\.com/.*" /&gt;
          &lt;property name="evaluationOrder" value="0" /&gt;
          &lt;property name="attributeFilter"&gt;
            &lt;bean class="org.jasig.cas.services.support.RegisteredServiceRegexAttributeFilter" c:regex="^\w{3}$" /&gt; 
          &lt;/property&gt;
      &lt;/bean&gt;
      --&gt;
  &lt;/util:list&gt;
  
  &lt;bean id="auditTrailManager" class="com.github.inspektr.audit.support.Slf4jLoggingAuditTrailManager" /&gt;
  
  &lt;bean id="healthCheckMonitor" class="org.jasig.cas.monitor.HealthCheckMonitor" p:monitors-ref="monitorsList" /&gt;
  
  &lt;util:list id="monitorsList"&gt;
    &lt;bean class="org.jasig.cas.monitor.MemoryMonitor" p:freeMemoryWarnThreshold="10" /&gt;
    &lt;!--
      NOTE
      The following ticket registries support SessionMonitor:
        * DefaultTicketRegistry
        * JpaTicketRegistry
      Remove this monitor if you use an unsupported registry.
    --&gt;
    &lt;bean class="org.jasig.cas.monitor.SessionMonitor"
        p:ticketRegistry-ref="ticketRegistry"
        p:serviceTicketCountWarnThreshold="5000"
        p:sessionCountWarnThreshold="100000" /&gt;
  &lt;/util:list&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>UPDATE:</strong>
  For debugging purposes, here are two log files. One with the deployerConfig you see here, and one with the default one. </p>
  
  <p>Mine: <a href="http://pastebin.com/9tRLPDq8" rel="nofollow">http://pastebin.com/9tRLPDq8</a></p>
  
  <p>Default: <a href="http://pastebin.com/pxrUJMQG" rel="nofollow">http://pastebin.com/pxrUJMQG</a></p>
  
  <p>In the default case I use wrong credentials on purpose. The most notable differences that I see are right before the audit(?) message:</p>
  
  <p>Mine:</p>
  
  <pre><code>2015-01-02 09:58:15,515 DEBUG [org.springframework.webflow.execution.ActionExecutor] - &lt;Executing [EvaluateAction@cdb588 expression = authenticationViaFormAction.submit(flowRequestContext, flowScope.credential, messageContext), resultExpression = [null]]&gt;
  2015-01-02 09:58:15,515 DEBUG [org.springframework.webflow.execution.AnnotatedAction] - &lt;Putting action execution attributes map[[empty]]&gt;
  2015-01-02 09:58:15,515 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - &lt;Returning cached instance of singleton bean 'authenticationViaFormAction'&gt;
  2015-01-02 09:58:15,517 INFO [org.perf4j.TimingLogger] - &lt;start[1420221495517] time[0] tag[AUTHENTICATE]&gt;
  2015-01-02 09:58:15,518 INFO [com.github.inspektr.audit.support.Slf4jLoggingAuditTrailManager] - &lt;Audit trail record BEGIN
  =============================================================
  WHO: audit:unknown
  ...
  </code></pre>
  
  <p>Default:</p>
  
  <pre><code>2015-01-02 10:07:53,246 DEBUG [org.springframework.webflow.execution.AnnotatedAction] - &lt;Putting action execution attributes map[[empty]]&gt;
  2015-01-02 10:07:53,246 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - &lt;Returning cached instance of singleton bean 'authenticationViaFormAction'&gt;
  2015-01-02 10:07:53,266 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - &lt;Returning cached instance of singleton bean 'auditTrailManagementAspect'&gt;
  2015-01-02 10:07:53,267 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - &lt;Returning cached instance of singleton bean 'timingAspect'&gt;
  2015-01-02 10:07:53,272 DEBUG [org.jasig.cas.authentication.AcceptUsersAuthenticationHandler] - &lt;admin was not found in the map.&gt;
  2015-01-02 10:07:53,272 INFO [org.jasig.cas.authentication.PolicyBasedAuthenticationManager] - &lt;AcceptUsersAuthenticationHandler failed authenticating admin+password&gt;
  2015-01-02 10:07:53,272 INFO [org.perf4j.TimingLogger] - &lt;start[1420222073269] time[2] tag[AUTHENTICATE]&gt;
  2015-01-02 10:07:53,277 INFO [com.github.inspektr.audit.support.Slf4jLoggingAuditTrailManager] - &lt;Audit trail record BEGIN
  =============================================================
  WHO: audit:unknown
  ...
  </code></pre>
  
  <p><strong>UPDATE 2:</strong>
  I digged into the source a bit and compared the log messages to the expected ones. Looking at <a href="http://grepcode.com/file/repo1.maven.org/maven2/org.jasig.cas/cas-server-core/4.0.0/org/jasig/cas/authentication/PolicyBasedAuthenticationManager.java#PolicyBasedAuthenticationManager.authenticateInternal%28org.jasig.cas.authentication.Credential[]%29" rel="nofollow">the authenticateInternal method</a>, I do not see <em>any</em> of the log messages there when using the db handler, and I see them when I use the default handler. Assuming I actually reach this method, the only place where it can fail without a message appears to be <code>for (final Credential credential : credentials) {...}</code>. </p>
  
  <p>Adding system info:
  CAS 4.0.0
  Tomcat 7
  JDK 1.7
  MySQL 5.5.22</p>
  
  <p>UPDATE 3:
  Well, it was looking at me right there all along...</p>
  
  <pre><code>WHO: audit:unknown
  WHAT: java.security.NoSuchAlgorithmException: SHA256 MessageDigest not available**
  ACTION: TICKET_GRANTING_TICKET_NOT_CREATED
  APPLICATION: CAS
  WHEN: Fri Jan 02 09:58:15 PST 2015
  CLIENT IP ADDRESS: 0:0:0:0:0:0:0:1
  SERVER IP ADDRESS: 0:0:0:0:0:0:0:1
  </code></pre>