title: SpringSecurity form login using UserDetailsService and Hibernate
body: <p>I have got stuck at creating usefull form login in SpringMVC using Spring Security. I'm quite newbie in this and also Hibernate. I would like to create simple form login which could provide access to my web application.</p>
  
  <p>I have created my project using SpringSource Tool Suite and selecting Spring Template Project. It uses Maven and I have also generated by Hibernate classes with annotations and <code>hibernate.cfg.xml</code>. In my database (HSQLDB) I have three tables: users, roles and users_roles. The third one contains user_id and role_id, so it stores information about users' roles. I have generated class by Hibernate successful.</p>
  
  <p>I have started writing my class implementing UserDetailsService. But I don't know how to do this properly. In <code>spring-security.xml</code> I have defined bean like this:</p>
  
  <pre><code>&lt;bean id="userDetailsService" class="hutter.pl.services.HutterUserDetailsService" /&gt;
  </code></pre>
  
  <p>I would like to use hashing by sha-256 with saltSource.</p>
  
  <pre><code>&lt;bean class="org.springframework.security.authentication.dao.ReflectionSaltSource" id="saltSource"&gt;
      &lt;property name="userPropertyToUse" value="username"/&gt;
  &lt;/bean&gt;
  
  &lt;security:authentication-manager&gt;   
      &lt;security:authentication-provider user-service-ref="userDetailsService"&gt;
          &lt;security:password-encoder hash="sha-256"&gt;
              &lt;security:salt-source ref="saltSource" /&gt;
          &lt;/security:password-encoder&gt; 
      &lt;/security:authentication-provider&gt;
  &lt;/security:authentication-manager&gt;
  </code></pre>
  
  <p>Should I have used this solution: <a href="https://stackoverflow.com/a/1654488/845220">https://stackoverflow.com/a/1654488/845220</a> ? Hibernate have generted classes like: RolesHome, Roles, Users, UsersHome, UsersRoles, UsersRolesHome. But i really don't know how to use these Hibernates classes to authorize users.</p>
  
  <pre><code>@Service("userDetailsService")
  public class MyUserDetailsService implements UserDetailsService {
  
      @Transactional(readOnly = true)
      public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {   
          UsersHome usersHome = new UsersHome();
         //Users user = ...       
         //...            
         return null;     
      }   
  }
  </code></pre>
  
  <p>Could you give me some hints?</p>
  
  <p><strong>EDIT:</strong>
  I have tried to add method <code>public Users findByLogin(String login)</code> to the <code>UsersHome</code> class.</p>
  
  <pre><code>   public Users findByLogin(String login) {
      log.debug("getting Users instance with login: " + login);
      try {
          Users instance = entityManager.find(Users.class, login);
          log.debug("get successful");
          return instance;
      } catch (RuntimeException re) {
          log.error("get failed", re);
          throw re;
      }
  }
  </code></pre>
  
  <p>And body of my UserDetailsService looks like:</p>
  
  <pre><code>UsersHome usersHome = new UsersHome();
  Users user = usersHome.findByLogin(username);
  </code></pre>
  
  <p>But I have got excpetion:</p>
  
  <pre><code> ERROR: my.package.dao.UsersHome - get failed
   java.lang.NullPointerException
  at my.package.dao.UsersHome.findByLogin(UsersHome.java:72)
  at my.package.services.HutterUserDetailsService.loadUserByUsername(MyUserDetailsService.java:19)
  </code></pre>