| 1 | package com.tecmilenio.mapsconect.security; |
| 2 | |
| 3 | import com.tecmilenio.mapsconect.entity.Usuario; |
| 4 | import com.tecmilenio.mapsconect.repository.UsuarioRepository; |
| 5 | import org.springframework.beans.factory.annotation.Autowired; |
| 6 | import org.springframework.security.core.GrantedAuthority; |
| 7 | import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| 8 | import org.springframework.security.core.userdetails.User; |
| 9 | import org.springframework.security.core.userdetails.UserDetails; |
| 10 | import org.springframework.security.core.userdetails.UserDetailsService; |
| 11 | import org.springframework.security.core.userdetails.UsernameNotFoundException; |
| 12 | import org.springframework.stereotype.Service; |
| 13 | |
| 14 | import java.util.Collections; |
| 15 | import java.util.List; |
| 16 | |
| 17 | @Service |
| 18 | public class CustomUserDetailsService implements UserDetailsService { |
| 19 | |
| 20 | @Autowired |
| 21 | private UsuarioRepository usuarioRepository; |
| 22 | |
| 23 | @Override |
| 24 | public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException { |
| 25 | Usuario usuario = usuarioRepository.findByEmail(email) |
| 26 | .orElseThrow(() -> new UsernameNotFoundException("Usuario no encontrado: " + email)); |
| 27 | |
| 28 | List<GrantedAuthority> authorities = Collections.singletonList( |
| 29 | new SimpleGrantedAuthority("ROLE_" + usuario.getRol().name())); |
| 30 | |
| 31 | return new User( |
| 32 | usuario.getEmail(), |
| 33 | usuario.getContrasena(), |
| 34 | usuario.getActivo(), |
| 35 | true, true, true, |
| 36 | authorities); |
| 37 | } |
| 38 | } |