Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Alvaro Ordóñez Romero
/
DAE-aha00026-aor00039
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
457fc1d9
authored
Dec 21, 2023
by
Alvaro Ordóñez Romero
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Modificaciones Security
parent
7aff0238
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
63 additions
and
88 deletions
src/main/java/com/carpooling/carpoolingaoraha/entidades/Usuario.java
src/main/java/com/carpooling/carpoolingaoraha/seguridad/ServicioSeguridadCarpooling.java
src/main/java/com/carpooling/carpoolingaoraha/services/ServicioCarPooling.java
src/main/java/com/carpooling/carpoolingaoraha/utils/CachedBCryptPasswordEncoder.java
src/test/java/com/carpooling/carpoolingaoraha/services/ServicioCarPoolingTest.java
src/main/java/com/carpooling/carpoolingaoraha/entidades/Usuario.java
View file @
457fc1d9
...
...
@@ -4,6 +4,7 @@ import com.carpooling.carpoolingaoraha.excepciones.ReservaNoRegistrada;
import
com.carpooling.carpoolingaoraha.excepciones.SolicitudYaExistente
;
import
com.carpooling.carpoolingaoraha.excepciones.ViajeNoRegistrado
;
import
com.carpooling.carpoolingaoraha.services.ServicioCarPooling
;
import
com.carpooling.carpoolingaoraha.utils.CodificadorPassword
;
import
com.carpooling.carpoolingaoraha.utils.ExprReg
;
import
jakarta.persistence.*
;
import
org.antlr.v4.runtime.misc.NotNull
;
...
...
@@ -40,17 +41,17 @@ public class Usuario {
@Id
@NotNull
@Size
(
min
=
9
,
max
=
9
)
@Pattern
(
regexp
=
ExprReg
.
DNI
)
@Size
(
min
=
9
,
max
=
9
)
@Pattern
(
regexp
=
ExprReg
.
DNI
)
private
String
DNI
;
@Size
(
min
=
9
,
max
=
9
)
@Pattern
(
regexp
=
ExprReg
.
TELEFONO
)
@Size
(
min
=
9
,
max
=
9
)
@Pattern
(
regexp
=
ExprReg
.
TELEFONO
)
private
int
telefono
;
@NotNull
@Pattern
(
regexp
=
ExprReg
.
EMAIL
)
@Pattern
(
regexp
=
ExprReg
.
EMAIL
)
private
String
email
;
@NotNull
...
...
@@ -90,7 +91,7 @@ public class Usuario {
this
.
DNI
=
DNI
;
this
.
telefono
=
telefono
;
this
.
email
=
email
;
this
.
claveAcceso
=
claveAcceso
;
this
.
claveAcceso
=
CodificadorPassword
.
codificar
(
claveAcceso
)
;
this
.
puntuacion
=
new
ArrayList
<>();
this
.
rol
=
rol
;
this
.
viajesOfertados
=
new
ArrayList
<>();
...
...
@@ -175,7 +176,7 @@ public class Usuario {
for
(
int
i
=
0
;
i
<
getPuntuacion
().
size
();
i
++)
{
puntuacionMedia
+=
getPuntuacion
().
get
(
i
);
}
puntuacionMedia
=
puntuacionMedia
/
getPuntuacion
().
size
();
puntuacionMedia
=
puntuacionMedia
/
getPuntuacion
().
size
();
return
puntuacionMedia
;
}
...
...
@@ -183,14 +184,13 @@ public class Usuario {
this
.
puntuacionMedia
=
puntuacionMedia
;
}
public
void
cambiarRol
(){
public
void
cambiarRol
()
{
this
.
rol
=
Rol
.
AMBOS
;
}
public
boolean
ofrecerViaje
(
String
origen
,
String
destino
,
Date
fechaSalida
,
int
nAsientos
,
int
precioAsiento
){
if
(
this
.
rol
==
Rol
.
CONDUCTOR
||
this
.
rol
==
Rol
.
AMBOS
)
{
public
boolean
ofrecerViaje
(
String
origen
,
String
destino
,
Date
fechaSalida
,
int
nAsientos
,
int
precioAsiento
)
{
if
(
this
.
rol
==
Rol
.
CONDUCTOR
||
this
.
rol
==
Rol
.
AMBOS
)
{
//Crea una reserva
Reserva
reserva
=
new
Reserva
(
this
,
origen
,
destino
,
nAsientos
,
precioAsiento
,
fechaSalida
);
this
.
viajesOfertados
.
add
(
reserva
.
getIdReserva
());
...
...
@@ -198,8 +198,9 @@ public class Usuario {
}
throw
new
ReservaNoRegistrada
();
}
public
boolean
ofrecerViaje
(
Reserva
v
){
if
(
this
.
rol
==
Rol
.
CONDUCTOR
||
this
.
rol
==
Rol
.
AMBOS
)
{
public
boolean
ofrecerViaje
(
Reserva
v
)
{
if
(
this
.
rol
==
Rol
.
CONDUCTOR
||
this
.
rol
==
Rol
.
AMBOS
)
{
//Crea una reserva
this
.
viajesOfertados
.
add
(
v
.
getIdReserva
());
return
true
;
...
...
@@ -207,11 +208,11 @@ public class Usuario {
throw
new
ReservaNoRegistrada
();
}
public
boolean
aceptarPasajero
(
int
idReserva
,
String
DNI
,
Rol
rol
){
//Le pasamos el rol del conductor
if
(
rol
==
Rol
.
CONDUCTOR
||
rol
==
Rol
.
AMBOS
)
{
public
boolean
aceptarPasajero
(
int
idReserva
,
String
DNI
,
Rol
rol
)
{
//Le pasamos el rol del conductor
if
(
rol
==
Rol
.
CONDUCTOR
||
rol
==
Rol
.
AMBOS
)
{
List
<
Reserva
>
reservas
=
servicioCarPooling
.
listarReservas
();
for
(
int
i
=
0
;
i
<
reservas
.
size
();
i
++)
{
if
(
idReserva
==
reservas
.
get
(
i
).
getIdReserva
()
&&
reservas
.
get
(
i
).
haySitioDisponible
())
{
for
(
int
i
=
0
;
i
<
reservas
.
size
();
i
++)
{
if
(
idReserva
==
reservas
.
get
(
i
).
getIdReserva
()
&&
reservas
.
get
(
i
).
haySitioDisponible
())
{
reservas
.
get
(
i
).
anadirPasajeros
(
DNI
,
idReserva
);
}
}
...
...
@@ -220,8 +221,8 @@ public class Usuario {
return
false
;
}
public
boolean
asignarPuntuacion
(
int
idReserva
,
String
DNI
,
int
puntuacion
){
if
(
puntuacion
<=
5
)
{
public
boolean
asignarPuntuacion
(
int
idReserva
,
String
DNI
,
int
puntuacion
)
{
if
(
puntuacion
<=
5
)
{
for
(
int
i
=
0
;
i
<
servicioCarPooling
.
listarReservas
().
size
();
i
++)
{
if
(
idReserva
==
this
.
servicioCarPooling
.
listarReservas
().
get
(
i
).
getIdReserva
()
&&
this
.
servicioCarPooling
.
listarReservas
().
get
(
i
).
esViajero
(
DNI
))
{
this
.
servicioCarPooling
.
listarReservas
().
get
(
i
).
anadirPuntuacion
(
puntuacion
);
...
...
@@ -235,7 +236,7 @@ public class Usuario {
public
List
<
Viaje
>
buscarViaje
(
String
origen
,
String
destino
,
Date
fecha
,
Rol
rol
,
List
<
Viaje
>
todosLosViajes
)
{
List
<
Viaje
>
viajesDisponibles
=
new
ArrayList
<>();
if
(
todosLosViajes
.
size
()
==
0
)
{
if
(
todosLosViajes
.
size
()
==
0
)
{
throw
new
ViajeNoRegistrado
();
}
...
...
@@ -281,8 +282,8 @@ public class Usuario {
return
viajesDisponibles
;
}
public
void
finalizaViaje
(
Reserva
reserva
){
if
(
this
.
rol
==
Rol
.
CONDUCTOR
||
this
.
rol
==
Rol
.
AMBOS
)
{
public
void
finalizaViaje
(
Reserva
reserva
)
{
if
(
this
.
rol
==
Rol
.
CONDUCTOR
||
this
.
rol
==
Rol
.
AMBOS
)
{
reserva
.
setReservaConcluida
(
true
);
// Mueve la solicitud asociada al histórico después de que el viaje ha concluido
...
...
@@ -356,7 +357,6 @@ public class Usuario {
}
public
ArrayList
<
Integer
>
getViajesOfertados
()
{
return
viajesOfertados
;
}
...
...
@@ -385,4 +385,9 @@ public class Usuario {
public
void
setHistoricoSolicitudes
(
List
<
Solicitud
>
historicoSolicitudes
)
{
this
.
historicoSolicitudes
=
historicoSolicitudes
;
}
public
boolean
claveValida
(
String
clave
)
{
return
CodificadorPassword
.
igual
(
clave
,
this
.
claveAcceso
);
//return this.clave.equals(clave);
}
}
src/main/java/com/carpooling/carpoolingaoraha/seguridad/ServicioSeguridadCarpooling.java
View file @
457fc1d9
package
com
.
carpooling
.
carpoolingaoraha
.
seguridad
;
import
com.carpooling.carpoolingaoraha.utils.CachedBCryptPasswordEncoder
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.security.config.annotation.web.builders.HttpSecurity
;
import
org.springframework.security.crypto.password.PasswordEncoder
;
import
org.springframework.security.web.SecurityFilterChain
;
import
org.springframework.security.web.access.expression.WebExpressionAuthorizationManager
;
public
class
ServicioSeguridadCarpooling
{
@Bean
PasswordEncoder
passwordEncoder
()
{
//return new BCryptPasswordEncoder();
return
new
CachedBCryptPasswordEncoder
();
}
@Bean
public
SecurityFilterChain
filterChain
(
HttpSecurity
httpSecurity
)
throws
Exception
{
...
...
@@ -21,10 +14,9 @@ public class ServicioSeguridadCarpooling {
.
httpBasic
(
httpBasic
->
httpBasic
.
realmName
(
"carpooling"
))
.
csrf
(
csrf
->
csrf
.
disable
())
.
sessionManagement
(
session
->
session
.
disable
())
.
authorizeHttpRequests
(
request
->
request
.
requestMatchers
(
HttpMethod
.
POST
,
"http://localhost:8080/"
)
.
permitAll
())
.
authorizeHttpRequests
(
request
->
request
.
requestMatchers
(
"http://localhost:8080/**"
)
.
access
(
new
WebExpressionAuthorizationManager
(
"hasRole('USUARIO') and #dni == principal.username"
)));
.
authorizeHttpRequests
(
request
->
request
.
requestMatchers
(
HttpMethod
.
POST
,
"/carpooling/usuarios"
)
.
permitAll
());
return
httpSecurity
.
build
();
}
...
...
src/main/java/com/carpooling/carpoolingaoraha/services/ServicioCarPooling.java
View file @
457fc1d9
...
...
@@ -128,17 +128,10 @@ public class ServicioCarPooling {
@Transactional
public
Optional
<
Usuario
>
login
(
@NotBlank
String
dni
,
@NotBlank
String
password
)
{
Optional
<
Usuario
>
usuarioLogin
=
repositorioUsuario
.
buscar
(
dni
);
if
(
usuarioLogin
.
isPresent
())
{
Usuario
usuario
=
usuarioLogin
.
get
();
BCryptPasswordEncoder
passwordEncoder
=
new
BCryptPasswordEncoder
();
// Verificar la contraseña
if
(
passwordEncoder
.
matches
(
password
,
usuario
.
getClaveAcceso
()))
{
return
usuarioLogin
;
// La contraseña es correcta
}
}
return
usuarioLogin
;
Optional
<
Usuario
>
clienteLogin
=
repositorioUsuario
.
buscar
(
dni
)
.
filter
((
cliente
)->
cliente
.
claveValida
(
password
));
return
clienteLogin
;
}
@Transactional
public
Optional
<
Viaje
>
verViaje
(
@NotBlank
int
id
)
{
...
...
src/main/java/com/carpooling/carpoolingaoraha/utils/CachedBCryptPasswordEncoder.java
deleted
100644 → 0
View file @
7aff0238
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package
com
.
carpooling
.
carpoolingaoraha
.
utils
;
import
org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
*
* @author ajrueda
*/
public
class
CachedBCryptPasswordEncoder
extends
BCryptPasswordEncoder
{
static
Map
<
String
,
CharSequence
>
cache
=
new
HashMap
<>();
public
CachedBCryptPasswordEncoder
()
{
super
();
}
public
CachedBCryptPasswordEncoder
(
int
strength
)
{
super
(
strength
);
}
@Override
public
boolean
matches
(
CharSequence
rawPassword
,
String
encodedPassword
)
{
CharSequence
cachedMatch
=
cache
.
get
(
encodedPassword
);
if
(
cachedMatch
!=
null
&&
cachedMatch
.
equals
(
rawPassword
))
{
return
true
;
}
boolean
result
=
super
.
matches
(
rawPassword
,
encodedPassword
);
if
(
result
==
true
)
{
cache
.
put
(
encodedPassword
,
rawPassword
);
}
return
result
;
}
}
src/test/java/com/carpooling/carpoolingaoraha/services/ServicioCarPoolingTest.java
View file @
457fc1d9
...
...
@@ -2,12 +2,15 @@ package com.carpooling.carpoolingaoraha.services;
import
com.carpooling.carpoolingaoraha.entidades.*
;
import
com.carpooling.carpoolingaoraha.entidades.Usuario.*
;
import
org.assertj.core.api.Assertions
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.annotation.DirtiesContext
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.time.LocalDate
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Optional
;
...
...
@@ -318,4 +321,29 @@ public class ServicioCarPoolingTest
assertTrue
(
viajes
.
size
()
>
0
);
}
@Test
@DirtiesContext
(
methodMode
=
DirtiesContext
.
MethodMode
.
AFTER_METHOD
)
public
void
testAltaYLoginClienteCuenta
()
throws
ParseException
{
int
length
=
9
;
String
characters
=
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
;
// Inicializar el generador de números aleatorios
Random
random
=
new
Random
();
StringBuilder
sb
=
new
StringBuilder
(
length
);
// Generar la cadena aleatoria
for
(
int
i
=
0
;
i
<
length
;
i
++)
{
int
randomIndex
=
random
.
nextInt
(
characters
.
length
());
char
randomChar
=
characters
.
charAt
(
randomIndex
);
sb
.
append
(
randomChar
);
}
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd"
);
java
.
util
.
Date
yourDate
=
sdf
.
parse
(
"1992-07-26"
);
Usuario
usuario
=
new
Usuario
(
"Perico"
,
"Alonso"
,
yourDate
,
"78162640S"
,
670988953
,
"aor00039@red.ujaen.es"
,
"hola"
,
Rol
.
PASAJERO
);
Usuario
clienteLogin
=
servicio
.
registrarUsuario
(
usuario
);
Assertions
.
assertThat
(
clienteLogin
.
getDNI
()).
isEqualTo
(
usuario
.
getDNI
());
}
}
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