Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Antonio Rueda
/
reserva-hoteles
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
fbc62b19
authored
Dec 11, 2024
by
Antonio Rueda
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Configuración provisional de seguridad
parent
3d85f13e
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
27 additions
and
12 deletions
src/main/java/es/ujaen/dae/reservahoteles/rest/ControladorReservas.java
src/main/java/es/ujaen/dae/reservahoteles/seguridad/ServicioCredencialesUsuario.java
src/main/java/es/ujaen/dae/reservahoteles/seguridad/ServicioSeguridad.java
src/main/java/es/ujaen/dae/reservahoteles/servicios/ServicioReservas.java
src/test/java/es/ujaen/dae/reservahoteles/rest/TestControladorReservas.java
src/main/java/es/ujaen/dae/reservahoteles/rest/ControladorReservas.java
View file @
fbc62b19
...
...
@@ -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
)
...
...
src/main/java/es/ujaen/dae/reservahoteles/seguridad/ServicioCredencialesUsuario.java
View file @
fbc62b19
...
...
@@ -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
())
...
...
src/main/java/es/ujaen/dae/reservahoteles/seguridad/ServicioSeguridad.java
View file @
fbc62b19
...
...
@@ -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
();
}
}
src/main/java/es/ujaen/dae/reservahoteles/servicios/ServicioReservas.java
View file @
fbc62b19
...
...
@@ -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
()
{
}
...
...
src/test/java/es/ujaen/dae/reservahoteles/rest/TestControladorReservas.java
View file @
fbc62b19
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
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment