Q:

Spring Boot Configuration + RepositoryRestResource + Authentication

I am trying to create a Spring Boot application which stores user/password combination in a users document with MongoDB.  I was able to successfully set up a repository extending MongoRepository and all works fine.  Now, I would like to set up authentication based on the data source my repository is connecting to.  Is there a quick way to do this just using the default connection, or do I need to specifically define a DataSource to do this?
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.jdbcAuthentication().dataSource(dataSource);
}

Here we should assume a dataSource is defined, but if I already have a REST repository set up, I don't have that yet.   Do I need an intermediary step here?

A:

I think that in this case UserDetailsService interface will be your best friend
You have to implement the UserDetailsService interface and make sure that your Mongo DB Repository is injected into the UserDetailsService implementation that you create.  After that let's implement the loadUserByUsername method in order to return a org.springframework.security.core.userdetails.User object and fill it with the Mongo DB user information.
import org.springframework.security.core.userdetails.User;
@Service
public class MyUserService implements UserDetailsService {

    @Autowired
    MyMongoRepo myMongoRepo;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        List<SimpleGrantedAuthority> dummyAuthorityForExample = Arrays.asList(new  SimpleGrantedAuthority("ROLE_ADMIN"));
        MyMongoUser mongoUser= myMongoRepo.findByUsername(s);
        User user = new User(mongoUser.getUsername(), mongoUser.getPassword(),dummyAuthorityForExample);
        return user;
    }
}

And finally inject the UserDetailsService to your AuthenticationManagerBuilder
@Autowired
UserDetailsService userDetailsService;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailsService);
}

