amezenin / encyclopedia-kn

0 stars 0 forks source link

Use functional and remove `fromUser` method #10

Open vahid-forghani opened 2 years ago

vahid-forghani commented 2 years ago

https://github.com/amezenin/encyclopedia-kn/blob/7e864bcb59032941b1b5756c027f829ee9387d52/src/main/java/com/knits/product/security/UserDetailsServiceImpl.java#L22

here you created this nice class which is responsible for getting the userDetail to spring security so you can modify your method like this:

@Override
    public UserDetails loadUserByUsername(String login) throws UsernameNotFoundException {
        User user = userRepository.findOneByLogin(login).orElseThrow(() ->
                new UsernameNotFoundException("User does not exists"));

        return new SecurityUser(
                user.getLogin(), 
                user.getPassword(), 
                user.getRoleList().stream().map(role -> new SimpleGrantedAuthority(role.getName())).collect(Collectors.toList()), 
                user.getActive());
    }

and then you don't need SecurityUser.fromUser(user) anymore.

vahid-forghani commented 2 years ago

I also noticed one thing in your code which will be fixed after you apply this change.

you didn't set authorities for your user, because the authorities is empty and you were always returning this empty authorities but after you apply this change I mentioned I will be fixed.