Commit d9e2a877 by Antonio Rueda

Creación de servicio de credenciales de usuario y de clase para

configuración de seguridad.
parent f6093cdb
......@@ -5,10 +5,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
/**
*
......@@ -17,17 +14,13 @@ import org.springframework.security.crypto.password.PasswordEncoder;
@SpringBootApplication(scanBasePackages={
"es.ujaen.dae.reservahoteles.servicios",
"es.ujaen.dae.reservahoteles.repositorios",
"es.ujaen.dae.reservahoteles.rest"
"es.ujaen.dae.reservahoteles.rest",
"es.ujaen.dae.reservahoteles.seguridad"
})
@EntityScan(basePackages="es.ujaen.dae.reservahoteles.entidades")
@EnableScheduling
@EnableCaching
public class ReservaHoteles {
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
public static void main(String[] args) {
SpringApplication.run(ReservaHoteles.class);
}
......
package es.ujaen.dae.reservahoteles.seguridad;
import es.ujaen.dae.reservahoteles.entidades.Usuario;
import es.ujaen.dae.reservahoteles.servicios.ServicioReservas;
import org.springframework.beans.factory.annotation.Autowired;
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;
/**
*
* @author ajrueda
*/
@Service
public class ServicioCredencialesUsuario implements UserDetailsService {
@Autowired
ServicioReservas servicioReservas;
@Override
public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
Usuario usuario = servicioReservas.buscarUsuario(userName).orElseThrow(()-> new UsernameNotFoundException(""));
return User.withUsername(usuario.email())
.password(usuario.clave())
.roles(usuario.nombre().equals("direccion") ? "DIRECCION": "CLIENTE")
.build();
}
}
package es.ujaen.dae.reservahoteles.seguridad;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
/**
*
* @author ajrueda
*/
@Configuration
public class ServicioSeguridad {
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
......@@ -71,7 +71,10 @@ public class ServicioReservas {
return repositorioClientes.buscar(email).filter(cliente -> cliente.clave().equals(clave));
}
public Optional<Usuario> buscarUsuario(@Email String email) {
public Optional<Usuario> buscarUsuario(String email) {
if (email.equals(direccion.email()))
return Optional.of(direccion);
return repositorioClientes.buscar(email);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment