Search in sources :

Example 1 with User

use of br.fatecsp.engsoft.entities.User in project Projeto_Detetive_ES3 by VitorAndrioli.

the class UserDetailsServiceImpl method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    Optional<User> userOp = Optional.ofNullable(UserRepository.findByUsername(username));
    User user = userOp.orElseThrow(() -> new UsernameNotFoundException("Usuario não encontrado"));
    Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
    user.getRoles().stream().forEach(role -> {
        grantedAuthorities.add(new SimpleGrantedAuthority(role.getName()));
    });
    return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), grantedAuthorities);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) User(br.fatecsp.engsoft.entities.User) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) HashSet(java.util.HashSet)

Aggregations

User (br.fatecsp.engsoft.entities.User)1 HashSet (java.util.HashSet)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1