title: spring security authentication not working
body: <p>I am trying out spring security for the first time but for some reason the authentication mechanism I have wrote is not firing  - </p>
  
  <p>Tech used - Spring 4.2.1, spring-security, jetty container, jersey and couchbase DB</p>
  
  <p>SecurityConfig.java - </p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  @ComponentScan(basePackageClasses={UserRepository.class, MyUserDetailService.class})
  public class SecurityConfig extends WebSecurityConfigurerAdapter{
  
  @Autowired
  @Qualifier("userDetailsService")
  UserDetailsService userDetailsService;
  
  @Autowired
  public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
   auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
  }
  
  @Override
  protected void configure(HttpSecurity http) throws Exception {
      http
              .authorizeRequests()
              .anyRequest().authenticated()
              .and()
              .formLogin()
              .and()
              .httpBasic();
  //    For later
  // 
  //        http.authorizeRequests().antMatchers("/*")
  //                .access("hasRole('ROLE_ADMIN')");
  }
  
  @Bean
  public PasswordEncoder passwordEncoder(){
      PasswordEncoder encoder = new BCryptPasswordEncoder();
      return encoder;
  }
  </code></pre>
  
  <p>SecurityWebAppInitializer.java - </p>
  
  <pre><code>public class SecurityWebApplicationInitializer
      extends AbstractSecurityWebApplicationInitializer {
  
  public SecurityWebApplicationInitializer() {
      super(SecurityConfig.class);
  }
  }
  </code></pre>
  
  <p>UserDetailService.java -</p>
  
  <pre><code>@Service("userDetailsService")
  public class MyUserDetailService extends BaseServiceImpl&lt;com.scoolboard.rest.entity.User, String&gt; implements UserDetailsService {
  
  @Autowired
  private UserRepository userRepository;
  
  
  protected UserRepository getRepository() {
      return userRepository;
  }
  
  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  
      com.scoolboard.rest.entity.User user = getRepository().findByUserEmail(username);
      List&lt;GrantedAuthority&gt; authorities = buildUserAuthority(new HashSet&lt;UserRole&gt;(user.getUserRole()));
  
      return buildUserForAuthentication(user, authorities);
  }
  
  // Converts com.mkyong.users.model.User user to
  // org.springframework.security.core.userdetails.User
  private User buildUserForAuthentication(com.scoolboard.rest.entity.User user,
                                          List&lt;GrantedAuthority&gt; authorities) {
      return new User(user.getEmail(), user.getPassword(),
              user.isEnabled(), true, true, true, authorities);
  }
  
  private List&lt;GrantedAuthority&gt; buildUserAuthority(Set&lt;UserRole&gt; userRoles) {
  
      Set&lt;GrantedAuthority&gt; setAuths = new HashSet&lt;GrantedAuthority&gt;();
  
      // Build user's authorities
      for (UserRole userRole : userRoles) {
          setAuths.add(new SimpleGrantedAuthority(userRole.getRole()));
      }
  
      List&lt;GrantedAuthority&gt; result = new ArrayList&lt;GrantedAuthority&gt;(setAuths);
  
      return result;
  }
  }
  </code></pre>
  
  <p>What am I missing over here.</p>