Skip to content

Instantly share code, notes, and snippets.

@srdelarosa
Created April 6, 2021 03:25
Show Gist options
  • Save srdelarosa/f1e8518e59e5781bc58593924d0dfd37 to your computer and use it in GitHub Desktop.
Save srdelarosa/f1e8518e59e5781bc58593924d0dfd37 to your computer and use it in GitHub Desktop.
package com.newhorizons.takeitnow.security.domain.service;
import com.newhorizons.takeitnow.security.domain.entity.UserApplication;
import com.newhorizons.takeitnow.security.domain.repository.IUserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class UserService implements UserDetailsService {
private Logger logger = LoggerFactory.getLogger(UserService.class);
@Autowired
private IUserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UserApplication userApplication = userRepository.authenticationByUsername(username);
if(userApplication == null){
logger.error("Error en el login, no existe el usuario " + username +" .");
throw new UsernameNotFoundException("Error en el login, no existe el usuario " + username +" .");
}
List<GrantedAuthority> authorities = userApplication.getRoles()
.stream()
.map(r -> new SimpleGrantedAuthority(r.getName()))
.peek(a -> logger.info("Role: " + a.getAuthority()))
.collect(Collectors.toList());
logger.info("Usuario autenticado correctamente " + username + " .");
return new User(userApplication.getUsername(), userApplication.getPassword(), userApplication.getActive(), true, true, true, authorities);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment