title: Spring Security - UserDetailsService implementation - Login Fails
body: <p>I'm quite new to spring and i'having this issue with spring security.
  Actually it only works without my custom UserDetailsService implementation.</p>
  
  <p>Account and Role Objects</p>
  
  <pre><code>@Entity
  @Table(name="ACCOUNT", uniqueConstraints = {@UniqueConstraint (columnNames = "USERNAME"),      @UniqueConstraint (columnNames = "EMAIL")})
  public class Account implements Serializable {
  private static final long serialVersionUID = 2872791921224905344L;
  
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name="ID")
  private Integer id;
  
  @Column(name="USERNAME")
  @NotNull
  private String username;
  
  @Column(name="PASSWORD")
  @NotNull
  private String password;
  
  @Column(name="EMAIL")
  @NotNull
  @Email
  private String email;
  
  @Column(name="ENABLED")
  private boolean enabled;
  
  @ManyToMany(cascade= CascadeType.ALL)
  @JoinTable(name="ACCOUNT_ROLE", joinColumns = {@JoinColumn (name="ID_ACCOUNT")}, inverseJoinColumns ={ @JoinColumn (name="ID_ROLE")})
  private Set&lt;Role&gt; roles = new HashSet&lt;Role&gt;(0);
  </code></pre>
  
  <p>Role</p>
  
  <pre><code>@Entity
  @Table(name="ROLE", uniqueConstraints={@UniqueConstraint (columnNames="NAME")})
  public class Role implements Serializable{
  
  private static final long serialVersionUID = -9162292216387180496L;
  
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Integer id;
  
  @Column(name = "NAME")
  @NotNull
  private String name;
  
  @ManyToMany(mappedBy = "roles")
  private Set&lt;Account&gt; accounts = new HashSet&lt;Account&gt;(0);
  </code></pre>
  
  <p>The adapter for the UserDetails</p>
  
  <pre><code>@SuppressWarnings({ "serial", "deprecation" })
  public class UserDetailsAdapter implements UserDetails {
  
  private Account account;
  
  public UserDetailsAdapter(Account account) {this.account = account;}
  
  public Account getAccount() {return account;}
  
  public Integer getId(){return account.getId();}
  
  public String getEmail () {return account.getEmail();}
  
  @Override
  public Collection&lt;? extends GrantedAuthority&gt; getAuthorities() {
      Set&lt;GrantedAuthority&gt; authorities = new HashSet&lt;GrantedAuthority&gt;();
      for (Role r :account.getRoles()) {
          authorities.add(new GrantedAuthorityImpl(r.getName()));
      }
      return authorities;
  }
  </code></pre>
  
  <p>The custom UserDetailsService</p>
  
  <pre><code>@Service("customUserDetailsService")
  public class CustomUserDetailsService implements UserDetailsService {
  @Inject AccountDao accountDao;
  @Inject RoleDao roleDao;
  
  
  @Override
  public UserDetails loadUserByUsername(String username)
          throws UsernameNotFoundException {
      Account account= accountDao.findByUsername(username);
  
  
  
      if(account==null) {throw new UsernameNotFoundException("No such user: " + username);
      } else if (account.getRoles().isEmpty()) {
          throw new UsernameNotFoundException("User " + username + " has no authorities");
                  }
      UserDetailsAdapter user = new UserDetailsAdapter(account);
      return user;
      }
  </code></pre>
  
  <p>The web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
  
  &lt;filter&gt;
          &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
          &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
  &lt;/filter&gt;
  
  &lt;filter-mapping&gt;
          &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  
  &lt;!-- The definition of the Root Spring Container shared by all Servlets and Filters --&gt;
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/spring/root-context.xml
                      /WEB-INF/spring/appServlet/security-  context.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;!-- Creates the Spring Container shared by all Servlets and Filters --&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;!-- Creates the Filters to handle hibernate lazyload exception --&gt;
  
  &lt;filter&gt;
      &lt;filter-name&gt;OpenSessionInViewFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.orm.hibernate3.support.OpenSessionInViewFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;OpenSessionInViewFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  
  
  &lt;!-- Processes application requests --&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/spring/appServlet/servlet-context.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>the root-context</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:mvc="http://www.springframework.org/schema/mvc"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"&gt;
  
  &lt;!-- Root Context: defines shared resources visible to all other web components --&gt;
  &lt;context:property-placeholder properties-ref="deployProperties"/&gt; 
  &lt;!-- Remember to correctly locate the right file for properties configuration(example DB connection parameters) --&gt;
  &lt;bean id="deployProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"
      p:location="/WEB-INF/spring/appServlet/spring.properties" /&gt;
  
      &lt;context:annotation-config/&gt;
      &lt;context:component-scan base-package="org.treci"&gt;
      &lt;context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/&gt;
      &lt;/context:component-scan&gt;
  
      &lt;import resource="/appServlet/data-context.xml"/&gt;
  </code></pre>
  
  <p></p>
  
  <p>The security context</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans:beans xmlns="http://www.springframework.org/schema/security"
  xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"&gt;
  
  &lt;http pattern="/resources" security="none" /&gt;
  
  
  &lt;http auto-config="true" use-expressions="true"&gt;
      &lt;intercept-url pattern="/" access="permitAll"/&gt;
      &lt;intercept-url pattern="/login" access="permitAll"/&gt;
      &lt;intercept-url pattern="/login-success" access="hasRole('ROLE_ADMIN')"/&gt;
  
      &lt;form-login login-page="/login" 
      default-target-url="/login-success"
      authentication-failure-url="/login-failed"/&gt;
      &lt;logout logout-success-url="/logout"/&gt;
  &lt;/http&gt;
  
  &lt;beans:bean id="customUserDetailsService" class="org.treci.app.service.CustomUserDetailsService"&gt;&lt;/beans:bean&gt;
  
  
  &lt;authentication-manager&gt;
      &lt;authentication-provider user-service-ref="customUserDetailsService"&gt;
      &lt;/authentication-provider&gt;
  &lt;/authentication-manager&gt;
  
  &lt;/beans:beans&gt;
  </code></pre>
  
  <p>I hope some of you can help, save me :)</p>