R
R
Romanov19812020-10-09 16:48:53
Spring
Romanov1981, 2020-10-09 16:48:53

Spring Security don't understand how it works?

Greetings dear forum users.

I have a question of this kind:

Please tell me, here in Spring Security there is a method loadUserByUsername

loadUserByUsername

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        
        // Получаем User из БД для проверки
        User user = repositories.getUserByUsername(username);

        return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getUsername(),
                true, true, true, true, getAuthorities());

    }
    

    private Collection<? extends GrantedAuthority> getAuthorities(){
        return Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER"));
    }


as we can see, we are passing parameters to userdetails.User for authentication. Based on these parameters, Spring Security authorizes us. What if the requested user does not exist? Then what to do? how to tell Spring that there is no such user?

Here is something I can't understand. Or am I doing something wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan Hasanli, 2020-10-09
@Romanov1981

What if the requested user does not exist? Then what to do? how to tell Spring that there is no such user?

Why tell Spring-y something? If there is no user, then an exception will be thrown throws UsernameNotFoundException
Or are you interested in something else?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question