Commit fbc62b19 by Antonio Rueda

Configuración provisional de seguridad

parent 3d85f13e
......@@ -21,6 +21,7 @@ import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
......@@ -45,11 +46,6 @@ public class ControladorReservas {
ServicioReservas servicioReservas;
Usuario direccion;
@PostConstruct
void loginDireccion() {
direccion = servicioReservas.login("direccion@hotelxyz.es", "SeCrEtO").get();
}
// Definir un mapeado global para cualquier excepción de validación de beans
@ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY)
......
......@@ -21,7 +21,7 @@ public class ServicioCredencialesUsuario implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
Usuario usuario = servicioReservas.buscarUsuario(userName).orElseThrow(()-> new UsernameNotFoundException(""));
Usuario usuario = servicioReservas.buscarUsuario(userName).orElseThrow(() -> new UsernameNotFoundException(""));
return User.withUsername(usuario.email())
.password(usuario.clave())
......
......@@ -3,8 +3,12 @@ package es.ujaen.dae.reservahoteles.seguridad;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.access.expression.WebExpressionAuthorizationManager;
/**
*
......@@ -17,4 +21,20 @@ public class ServicioSeguridad {
return new BCryptPasswordEncoder();
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http
.csrf(csrf -> csrf.disable())
.sessionManagement(session -> session.disable())
.httpBasic(httpBasic -> httpBasic.realmName("reservas"))
.authorizeHttpRequests(request -> request
.requestMatchers(HttpMethod.GET, "/reservas/usuarios/{email}")
.access(new WebExpressionAuthorizationManager("hasRole('DIRECCION') or (hasRole('USUARIO') and #email == principal.username)"))
.requestMatchers(HttpMethod.POST, "/reservas/hoteles").hasRole("DIRECCION")
.requestMatchers(HttpMethod.POST, "/reservas/hoteles/{id}/reservas").hasAnyRole("DIRECCION", "USUARIO")
.requestMatchers(HttpMethod.POST, "/reservas/**").permitAll()
)
.build();
}
}
......@@ -43,7 +43,8 @@ public class ServicioReservas {
int mesesHistorico;
// Cliente especial de dirección
private static final Usuario direccion = new Usuario("direccion", "-", "670343332", "direccion@hotelxyz.es", "SeCrEtO");
private static final Usuario direccion = new Usuario("direccion", "-", "670343332", "direccion@hotelxyz.es",
"$2a$10$ZUSGA7jwZxSufzxZ1A2JRuIekaJrJbuwV6g6H5hZ7WPMk9nV9h/re");
public ServicioReservas() {
}
......
package es.ujaen.dae.reservahoteles.rest;
import es.ujaen.dae.reservahoteles.entidades.Usuario;
import es.ujaen.dae.reservahoteles.rest.dto.DHotel;
import es.ujaen.dae.reservahoteles.rest.dto.DReserva;
import es.ujaen.dae.reservahoteles.rest.dto.DUsuario;
......@@ -81,11 +80,10 @@ public class TestControladorReservas {
);
assertThat(respuesta.getStatusCode()).isEqualTo(HttpStatus.CREATED);
var respuestaLogin = restTemplate.getForEntity(
"/usuarios/{email}?clave={clave}",
var respuestaLogin = restTemplate.withBasicAuth("ppp@gmail.com", "miClAvE").getForEntity(
"/usuarios/{email}",
DUsuario.class,
"ppp@gmail.com",
"miClAvE"
"ppp@gmail.com"
);
assertThat(respuestaLogin.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
......
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