title: spring security with custom user details
body: <p>I am trying to apply security in my spring application using database tables.</p>
  
  <p>What I have so far in my applicationContext-Security is:</p>
  
  <pre><code>&lt;beans:bean id="userDetailsService" class="org.intan.pedigree.service.UserDetailsServiceImpl"&gt;&lt;/beans:bean&gt;
  
  &lt;http auto-config='true'&gt;
      &lt;intercept-url pattern="/**" access="ROLE_USER" /&gt;
  &lt;/http&gt;
  
   &lt;beans:bean id="daoAuthenticationProvider"
      class="org.springframework.security.authentication.dao.DaoAuthenticationProvider"&gt;
      &lt;beans:property name="userDetailsService" ref="userDetailsService" /&gt;
  &lt;/beans:bean&gt;
  
  &lt;beans:bean id="authenticationManager"
      class="org.springframework.security.authentication.ProviderManager"&gt;
      &lt;beans:property name="providers"&gt;
          &lt;beans:list&gt;
              &lt;beans:ref local="daoAuthenticationProvider" /&gt;
          &lt;/beans:list&gt;
      &lt;/beans:property&gt;
  &lt;/beans:bean&gt;
  
  
  &lt;authentication-manager&gt;
      &lt;authentication-provider user-service-ref="userDetailsService"&gt;
          &lt;password-encoder hash="plaintext" /&gt;
      &lt;/authentication-provider&gt;
  &lt;/authentication-manager&gt;
  </code></pre>
  
  <p>my implementation of userDetailsService looks as follows:</p>
  
  <pre><code>    package org.intan.pedigree.service;
  
  import org.intan.pedigree.dao.UserEntityDAO;
  import org.intan.pedigree.dao.UserEntityDAOImpl;
  import org.intan.pedigree.form.UserEntity;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.dao.DataAccessException;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  import org.springframework.security.core.userdetails.UserDetails;
  import org.springframework.security.core.userdetails.UserDetailsService;
  import org.springframework.security.core.userdetails.UsernameNotFoundException;
  
  @Service("userDetailsService")
  public class UserDetailsServiceImpl implements UserDetailsService {
  
      @Autowired
      private UserEntityDAO dao;
      @Autowired
      private Assembler assembler;
  
      @Transactional(readOnly = true)
      public UserDetails loadUserByUsername(String username)
              throws UsernameNotFoundException, DataAccessException {
  
          UserDetails userDetails = null;
          UserEntity userEntity = dao.findByName(username);
           if (userEntity == null)
                throw new UsernameNotFoundException("user not found");
  
          return  assembler.buildUserFromUserEntity(userEntity);
      }
  }
  </code></pre>
  
  <p>my assembler looks as follows:</p>
  
  <pre><code>package org.intan.pedigree.service;
  
  import java.util.ArrayList;
  import java.util.Collection;
  
  import org.intan.pedigree.form.SecurityRoleEntity;
  import org.intan.pedigree.form.UserEntity;
  //import org.springframework.security.core.GrantedAuthority;
  //import org.springframework.security.core.authority.GrantedAuthorityImpl;
  //import org.springframework.security.core.userdetails.User;
  import org.springframework.security.core.GrantedAuthority;
  import org.springframework.security.core.authority.GrantedAuthorityImpl;
  import org.springframework.security.core.userdetails.User;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  
  @Service("assembler")
  public class Assembler {
  
    @Transactional(readOnly = true)
    User buildUserFromUserEntity(UserEntity userEntity) {
  
      String username = userEntity.getUsername();
      String password = userEntity.getPassword();
      boolean enabled = userEntity.isActive();
      boolean accountNonExpired = userEntity.isActive();
      boolean credentialsNonExpired = userEntity.isActive();
      boolean accountNonLocked = userEntity.isActive();
      Collection&lt;GrantedAuthority&gt; authorities = new ArrayList&lt;GrantedAuthority&gt;();
      for (SecurityRoleEntity role : userEntity.getUserSecurityRoleEntity()) {
        authorities.add(new GrantedAuthorityImpl(role.getName()));
      }
  
      User user = new User(username, password, enabled,
        accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
      return user;
    }
  }
  </code></pre>
  
  <p>now the user entity is:</p>
  
  <pre><code>    package org.intan.pedigree.form;
  
  import java.util.Date;
  import java.util.HashSet;
  import java.util.Set;
  
  import javax.persistence.CascadeType;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.JoinTable;
  import javax.persistence.OneToMany;
  import javax.persistence.Table;
  import javax.validation.constraints.NotNull;
  @Entity
  @Table(name="user")
  public class UserEntity {
  
      @Id
      @GeneratedValue
      @Column(name="ID")
      private int id;
      @Column(name="first_name")
      private String first_name;
      @Column(name="family_name")
      private String last_name;
      @Column(name="dob")
      private Date dob;
      @Column(name="password")
      private String password;
      @Column(name="username")
      private String username;
      @Column(name="active")
          @NotNull
      private boolean isActive;
      @Column(name="user_types_id")
      private int user_types_id;
      @Column(name="confirm_password")
      public String confirmPassword;
      @OneToMany(cascade = CascadeType.ALL)
      @JoinTable(name = "user_address", joinColumns = { @JoinColumn(name = "user_id") },
              inverseJoinColumns = { @JoinColumn(name = "address_id") })
      private Set&lt;Address&gt; userAddress = new HashSet&lt;Address&gt;(0);
      /*******************************************************************************/
      @OneToMany(cascade = CascadeType.ALL)
      @JoinTable(name = "user_security_role", joinColumns = { @JoinColumn(name = "user_id") },
              inverseJoinColumns = { @JoinColumn(name = "security_role_id") })
      private Set&lt;SecurityRoleEntity&gt; userSecurityRoleEntity = new HashSet&lt;SecurityRoleEntity&gt;(0);
  
      public Set&lt;Address&gt; getUserAddress(){
          return this.userAddress;
      }
  
      public void setUserAddress(Set&lt;Address&gt; userAddress) {
          this.userAddress = userAddress;
      }
      /*****************************************************************************/
  
      public Set&lt;SecurityRoleEntity&gt; getUserSecurityRoleEntity(){
          return this.userSecurityRoleEntity;
      }
  
      public void setUserSecurityRoleEntity(Set&lt;SecurityRoleEntity&gt; userSecurityRoleEntity) {
          this.userSecurityRoleEntity = userSecurityRoleEntity;
      }
  
  
      public boolean isActive() {
          return isActive;
      }
  
      public void setActive(boolean isActive) {
          this.isActive = isActive;
      }
  
      public String getConfirmPassword() {
          return confirmPassword;
      }
      public void setConfirmPassword(String confirmPassword) {
          this.confirmPassword = confirmPassword;
      }
      public int getId() {
          return id;
      }
      public void setId(int id) {
          this.id = id;
      }
      public String getFirst_name() {
          return first_name;
      }
      public void setFirst_name(String first_name) {
          this.first_name = first_name;
      }
      public String getLast_name() {
          return last_name;
      }
      public void setLast_name(String last_name) {
          this.last_name = last_name;
      }
      public Date getDob() {
          return dob;
      }
      public void setDob(Date dob) {
          this.dob = dob;
      }
      public String getPassword() {
          return password;
      }
      public void setPassword(String password) {
          this.password = password;
      }
      public String getUsername() {
          return username;
      }
      public void setUsername(String username) {
          this.username = username;
      }
      public int getUser_types_id() {
          return user_types_id;
      }
      public void setUser_types_id(int user_types_id) {
          this.user_types_id = user_types_id;
      }
  
  
  }
  </code></pre>
  
  <p>my userentitydao interface is:</p>
  
  <pre><code>package org.intan.pedigree.dao;
  
  import java.util.List;
  
  import org.intan.pedigree.form.UserEntity;
  
  
  public interface UserEntityDAO {
      public void removeUserEntity(Integer id);
      public List&lt;UserEntity&gt; listUserEntity() ;
      public void addUserEntity(UserEntity user) ;
      public void updateUserEntity(UserEntity user) ;
      public UserEntity getUserEntityByID(Integer id);
      public UserEntity findByName(String username);
  }
  </code></pre>
  
  <p>and implementation is:</p>
  
  <pre><code>    @Repository
  public class UserEntityDAOImpl implements UserEntityDAO{
  
      @Autowired
      private SessionFactory sessionFactory;
  
      public void addUserEntity(UserEntity user) {
          try {
          sessionFactory.getCurrentSession().save(user);
          } catch (Exception e) {
              System.out.println(e);
          }
      }
  
      public UserEntity findByName(String username) {
          UserEntity user = (UserEntity) sessionFactory.getCurrentSession().createQuery(
                  "select u form user u where u.username = '" + username + "'");
          return user;
  
      }
  
      public UserEntity getUserEntityByID(Integer id) {
          UserEntity user = (UserEntity) sessionFactory.getCurrentSession().createQuery(
                  "select u form user u where id = '" + id + "'");
          return user;
      }
      public void updateUserEntity(UserEntity user) {
          try {
          sessionFactory.getCurrentSession().update(user);
          } catch (Exception e) {
              System.out.println(e);
          }
      }
  
      public List&lt;UserEntity&gt; listUserEntity() {
  
          return sessionFactory.getCurrentSession().createQuery("from User")
                  .list();
      }
  
      public void removeUserEntity(Integer id) {
          UserEntity user = (UserEntity) sessionFactory.getCurrentSession().load(
                  UserEntity.class, id);
          if (null != user) {
              sessionFactory.getCurrentSession().delete(user);
          }
  
      }
  }
  </code></pre>
  
  <p>now when i try to deploy on tomcat i get the following exception:</p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDetailsService': Injection of autowired dependencies failed; nested exception
  is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.intan.pedigree.dao.UserEntityDAO org.intan.pedigree.service.UserDetailsS
  erviceImpl.dao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.intan.pedigree.dao.UserEntityDAO] found
   for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotatio
  n.Autowired(required=true)}
  </code></pre>
  
  <p>and not mater what i do I can not figure out what is wrong with it. Any advice would be welcome.</p>
  
  <p>thanks a lot</p>