Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Implementing a UserDetailsService

Sergei Makarov
Sergei Makarov
13,839 Points

Changing method implementation by loadUserByUsername(String str)

Is any difference to using THIS implementation in loadUserByUsername method:

User user = findByUsername(username);

instead of :

User user = userDao.findByUsername(username);

cause we already have this implementation in our class. THe full code is here:

@Override
    public User findByUsername(String username) {
        return userDao.findByUsername(username);
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            User user = findByUsername(username); 
//OR MAYBE SO:
           User user = userDao.findByUsername(username)
        if(user == null) {
            throw new UsernameNotFoundException("User not found");
        }

        return user;
    }

1 Answer

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

I think there it depends whether you want to call userDao.findByUsername or userService.findByUsername. If they are the same like in your case. It does not matter, but they can be different. For example: userService.findByUsername can check if user is admin, but userDao.findByUsername just retireves values. In this case you have to decide whether in the method you want to use userDao method, without checking admin rights, or userService method with additional security check