dimanche 29 mars 2015

Spring Security authentication using UserDetailsService

I have some issues with Spring security authentication. Everywhere in my application everything works great (CRUD operations work well), but login attempt fails.


Here's my code (I marked below with comments where userDAO is null which is cause of failed authentication):



@Service
public class UserServiceImpl implements UserService, UserDetailsService {

@Autowired
UserDAO userDAO;

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userDAO.getUserByUsername(username); //userDAO == null Causing NPE
if (user == null)
throw new UsernameNotFoundException("Oops!");

List<SimpleGrantedAuthority> authorities = Arrays.asList(new SimpleGrantedAuthority(user.getRole()));

return new org.springframework.security.core.userdetails
.User(user.getLogin(), user.getPassword(), authorities);
}

@Override
public List<User> getUsers() {
return userDAO.getUsers();//userDAO !=null
}
//rest of code skiped


My SecurityConfig looks like this



@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

UserServiceImpl userService = new UserServiceImpl();

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userService);
}
//rest of code skipped


I marked where i get NPE and i have no idea how to solve this. Whole Configuration is JavaBased and you can check it out out here for more details HERE


Aucun commentaire:

Enregistrer un commentaire