title: Spring Security Custom Authentication and Password Encoding
body: <p>Is there a tutorial out there or does anyone have pointers on how to do the following with Spring-Security?</p>
  
  <p><strong>Task:</strong></p>
  
  <p>I need to get the salt from my database for the authenticating username and use it to encrypt the provided password (from the login page) to compare it to the stored encrypted password (a.k.a. authenticate the user).</p>
  
  <p><strong>additional information:</strong></p>
  
  <p>I use a custom database structure. A <code>UserDetails</code> object is created via a custom <code>UserDetailsService</code> which in turn uses a custom DAOProvider to get the information from the database.</p>
  
  <p>my <code>security.xml</code> file so far:</p>
  
  <pre><code>&lt;authentication-manager&gt;
      &lt;authentication-provider user-service-ref="userDetailsService"&gt;
      &lt;/authentication-provider&gt;
  &lt;/authentication-manager&gt;
  </code></pre>
  
  <p>now I guess I'll need </p>
  
  <pre><code>        &lt;password-encoder hash="sha" /&gt;
  </code></pre>
  
  <p>but what else? How do I tell spring security to use the databaseprovided salt in order to encode the password? </p>
  
  <hr>
  
  <p><strong>edit</strong>:</p>
  
  <p>I found <a href="https://stackoverflow.com/questions/4968704/spring-security-3-salting-password-issue">This SO</a> post to be informatative but not sufficient: If I define a salt source in my xml to be used by the password encoder, like so:</p>
  
  <pre><code>        &lt;password-encoder ref="passwordEncoder"&gt;                
              &lt;salt-source ref="saltSource"/&gt;
          &lt;/password-encoder&gt;
  </code></pre>
  
  <p>I'll have to write a custom SaltSource to use my custom salt. But that's not to be found inside the <code>UserDetails</code> object. So...</p>
  
  <p><strong>Alternative 1:</strong></p>
  
  <p>Can I use a custom Implementation of UserDetails which might then have the salt property?</p>
  
  <pre><code>&lt;beans:bean id="saltSource" class="path.to.MySaltSource"
      p:userPropertyToUse="salt"/&gt;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>@Service("userDetailsService") 
  public class UserDetailsServiceImpl implements UserDetailsService {
      public UserDetails loadUserByUsername(String username)
              throws UsernameNotFoundException, DataAccessException {
  
          // ...
          return buildUserFromAccount(account);
      }
  
      @Transactional(readOnly = true)
  
      UserDetailsImpl buildUserFromAccount(Account account){
  
          // ... build User object that contains salt property
  }
  </code></pre>
  
  <p>custom User Class:</p>
  
  <pre><code>public class UserDetailsImpl extends User{
  
      // ...
  
      private String salt;
  
      public String getSalt() { return salt; }
  
      public void setSalt(String salt) { this.salt = salt; }
  }
  </code></pre>
  
  <p>security.xml:</p>
  
  <pre><code>&lt;authentication-manager&gt;
      &lt;authentication-provider user-service-ref="userDetailsService"&gt;
          &lt;password-encoder hash="sha"&gt;                
          &lt;salt-source ref="saltSource"/&gt;
      &lt;/password-encoder&gt;
      &lt;/authentication-provider&gt;
  &lt;/authentication-manager&gt;
  
  &lt;beans:bean id="saltSource" class="org.springframework.security.authentication.dao.ReflectionSaltSource" p:userPropertyToUse="salt"/&gt;
  </code></pre>
  
  <p><br /></p>
  
  <p><strong>Alternative 2:</strong></p>
  
  <p>Otherwise I'd have to inject my accountDAO into the <code>SaltSource</code> to extract the salt for a given <code>userName</code> from the database. </p>
  
  <p>BUT: How does Spring Security call the <code>SaltSource</code>? Always with <code>saltSource.getSalt(userDetails)</code>? </p>
  
  <p>Then I'd just have to make sure my <code>SaltSource</code> uses <code>userDetails.accountName</code> on my <code>accountDAO</code> to retrieve the salt. </p>
  
  <hr>
  
  <p><strong>Edit2:</strong></p>
  
  <p>Just learned that my approach is.. legacy.. :( So I guess I'll just use the <code>StandardPasswordEncoder</code> (which I still have to figure out how to use exactly).</p>
  
  <p>BTW: I implemented the first option with a custom UserDetails class extending the User class and just adding a salt property which an then be passed to the SaltSource as a userPropertyToUse just like it has been proposed in the SO post mentioned in Edit 1...</p>
  
  <hr>
  
  <p><strong>EDIT 3:</strong></p>
  
  <p>Just got the StandardPasswordEncoder working, so I'll leave some pointers here:</p>
  
  <p>Use the StandardPasswordEncoder for Authentication:</p>
  
  <pre><code>&lt;beans:bean id="encoder" 
      class="org.springframework.security.crypto.password.StandardPasswordEncoder"&gt;
  &lt;/beans:bean&gt;
  
  
  &lt;authentication-manager&gt;
      &lt;authentication-provider user-service-ref="userDetailsService"&gt;
          &lt;password-encoder ref="encoder" /&gt;         
      &lt;/authentication-provider&gt;
  &lt;/authentication-manager&gt;
  </code></pre>
  
  <p>This requires the spring-security-crypto module in version 3.1.0.RC? as far as I know. Couldn't find any repository that has a 3.0. version (even though somewhere it had the versions listed that included 3.0.6 and so on). Also the documentations talks about spring security 3.1 so I figured, I'll just go with that. </p>
  
  <p>When creating a user (for me only an admin can do that), I just use </p>
  
  <pre><code>        StandardPasswordEncoder encoder = new StandardPasswordEncoder();
          String result = encoder.encode(password);
  </code></pre>
  
  <p>and I'm done.</p>
  
  <p>Spring security will randomly create a salt and add it to the password string before storing it in the database so <strong>no salt column is needed anymore</strong>.</p>
  
  <p>One can however also provide a global salt as a constructor argument (<code>new StandardPasswordEncoder("12345");</code>), but I didn't know how to set up my security configuration to retrieve that value from a bean instead of supplying a static string with <code>&lt;constructor-arg name="secret" value "12345" /&gt;</code>. But I don't know how much that is needed anyway.</p>