Saturday, January 10, 2015

org.springframework.security.authentication.InternalAuthenticationServiceException: UserDetailsService returned null, which is an interface contract violation

You can encounter the following exception:

org.springframework.security.authentication.InternalAuthenticationServiceException: UserDetailsService returned null, which is an interface contract violation

UserDetailsService.loadUserByUsername() shouldn't return a null value as described in Javadoc.

When the value is null, it should throw a UsernameNotFoundException as follows:

  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    User user = userRepository.findByUsername(username);
    if (user == null) {
      throw new UsernameNotFoundException(username);
    }
    return user;
  }

No comments:

Post a Comment