Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Antonio Rueda
/
UJACoin
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
284ba900
authored
Nov 25, 2023
by
Antonio Rueda
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Resueltos problemas con caché
parent
a88d62b9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
49 additions
and
43 deletions
pom.xml
src/main/java/es/ujaen/dae/ujacoin/entidades/Cliente.java
src/main/java/es/ujaen/dae/ujacoin/rest/ControladorREST.java
src/main/java/es/ujaen/dae/ujacoin/seguridad/ServicioSeguridadUjaCoin.java
src/main/java/es/ujaen/dae/ujacoin/servicios/ServicioUjaCoin.java
src/main/java/es/ujaen/dae/ujacoin/util/CachedBCryptPasswordEncoder.java
src/main/resources/application-test.yml
src/main/resources/application.yml
src/main/resources/ehcache.xml
pom.xml
View file @
284ba900
...
...
@@ -69,14 +69,13 @@
<dependency>
<groupId>
com.mysql
</groupId>
<artifactId>
mysql-connector-j
</artifactId>
<version>
8.0.33
</version>
<scope>
runtime
</scope>
</dependency>
<dependency>
<groupId>
com.h2database
</groupId>
<artifactId>
h2
</artifactId>
<
!-- <scope>test</scope> --
>
<
scope>
test
</scope
>
</dependency>
<dependency>
...
...
@@ -85,9 +84,9 @@
</dependency>
<dependency>
<groupId>
org
.ehcache
</groupId>
<groupId>
net.sf
.ehcache
</groupId>
<artifactId>
ehcache
</artifactId>
<version>
3.10.8
</version>
<version>
2.10.9.2
</version>
</dependency>
</dependencies>
...
...
src/main/java/es/ujaen/dae/ujacoin/entidades/Cliente.java
View file @
284ba900
...
...
@@ -60,7 +60,6 @@ public class Cliente implements Serializable {
String
clave
;
/** Tarjetas asociadas al cliente (no tiene por qué ser el titular */
@OneToMany
(
fetch
=
FetchType
.
EAGER
,
cascade
=
CascadeType
.
ALL
)
@JoinColumn
(
name
=
"cliente_dni"
)
List
<
Tarjeta
>
tarjetas
;
...
...
src/main/java/es/ujaen/dae/ujacoin/rest/ControladorREST.java
View file @
284ba900
...
...
@@ -21,7 +21,6 @@ import es.ujaen.dae.ujacoin.servicios.ServicioUjaCoin;
import
jakarta.validation.ConstraintViolationException
;
import
jakarta.validation.constraints.Positive
;
import
java.time.LocalDateTime
;
import
java.time.LocalTime
;
import
java.time.Month
;
import
java.time.format.DateTimeParseException
;
import
java.util.List
;
...
...
@@ -74,7 +73,7 @@ public class ControladorREST {
@PostMapping
(
"/clientes"
)
ResponseEntity
<
DTOCuenta
>
altaCliente
(
@RequestBody
DTOCliente
cliente
)
{
try
{
Cuenta
cuenta
=
servicios
.
altaCliente
(
cliente
.
aCliente
());
Cuenta
cuenta
=
servicios
.
altaCliente
(
cliente
.
aCliente
());
return
ResponseEntity
.
status
(
HttpStatus
.
CREATED
).
body
(
new
DTOCuenta
(
cuenta
));
}
catch
(
ClienteYaRegistrado
e
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
CONFLICT
).
build
();
...
...
@@ -151,7 +150,7 @@ public class ControladorREST {
List
<
DTOCuenta
>
verCuentas
(
@PathVariable
String
dni
)
{
return
servicios
.
verCliente
(
dni
)
.
orElseThrow
(
ClienteNoRegistrado:
:
new
)
.
verCuentas
()
.
verCuentas
()
.
stream
()
.
map
(
c
->
new
DTOCuenta
(
c
)).
toList
();
}
...
...
src/main/java/es/ujaen/dae/ujacoin/seguridad/ServicioSeguridadUjaCoin.java
View file @
284ba900
...
...
@@ -5,12 +5,11 @@
*/
package
es
.
ujaen
.
dae
.
ujacoin
.
seguridad
;
import
es.ujaen.dae.ujacoin.util.CachedBCryptPasswordEncoder
;
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.config.http.SessionCreationPolicy
;
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
;
...
...
@@ -24,7 +23,8 @@ import org.springframework.security.web.access.expression.WebExpressionAuthoriza
public
class
ServicioSeguridadUjaCoin
{
@Bean
PasswordEncoder
passwordEncoder
()
{
return
new
BCryptPasswordEncoder
();
//return new BCryptPasswordEncoder();
return
new
CachedBCryptPasswordEncoder
();
}
@Bean
...
...
src/main/java/es/ujaen/dae/ujacoin/servicios/ServicioUjaCoin.java
View file @
284ba900
...
...
@@ -28,6 +28,8 @@ import java.util.Optional;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cache.CacheManager
;
import
org.springframework.cache.annotation.CacheEvict
;
import
org.springframework.cache.annotation.Cacheable
;
import
org.springframework.cache.annotation.Caching
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
import
org.springframework.validation.annotation.Validated
;
...
...
@@ -94,11 +96,15 @@ public class ServicioUjaCoin {
* @param clave la clave de acceso
* @return el objeto de la clase Cliente asociado
*/
@Cacheable
(
"clientes"
)
public
Optional
<
Cliente
>
verCliente
(
@NotBlank
String
dni
)
{
Optional
<
Cliente
>
clienteLogin
=
repositorioClientes
.
buscar
(
dni
);
return
clienteLogin
;
}
return
repositorioClientes
.
buscar
(
dni
);
}
@Cacheable
(
"cuentas"
)
public
Optional
<
Cuenta
>
verCuenta
(
@NotBlank
String
num
)
{
return
repositorioCuentas
.
buscar
(
num
);
}
/**
* Crear una cuenta adicional para el cliente
...
...
@@ -110,6 +116,8 @@ public class ServicioUjaCoin {
Cliente
cliente
=
repositorioClientes
.
buscar
(
dni
).
orElseThrow
(
ClienteNoRegistrado:
:
new
);
Cuenta
cuenta
=
new
Cuenta
(
generarNumCuenta
(),
cliente
);
repositorioCuentas
.
guardar
(
cuenta
);
cliente
.
nuevaCuenta
(
cuenta
);
repositorioClientes
.
actualizar
(
cliente
);
...
...
@@ -138,7 +146,8 @@ public class ServicioUjaCoin {
* @param importe el importe a ingresar en euros (se tiene en cuenta el cambio euro-UJACoin actual)
*/
@Transactional
public
void
ingreso
(
@NotBlank
String
numCuenta
,
@NotBlank
String
numTarjeta
,
@Positive
float
importe
)
{
@CacheEvict
(
value
=
"cuentas"
,
key
=
"#numCuenta"
)
public
void
ingreso
(
@NotBlank
String
numCuenta
,
@NotBlank
String
numTarjeta
,
@Positive
float
importe
)
{
Cuenta
cuenta
=
repositorioCuentas
.
buscarYBloquear
(
numCuenta
)
.
orElseThrow
(
CuentaNoRegistrada:
:
new
);
...
...
@@ -146,6 +155,8 @@ public class ServicioUjaCoin {
.
orElseThrow
(
TarjetaNoRegistrada:
:
new
);
cuenta
.
nuevoMovimiento
(
new
Ingreso
(
tarjeta
,
importe
));
cacheManager
.
getCache
(
"clientes"
).
evict
(
cuenta
.
getTitular
().
getDni
());
}
/**
...
...
@@ -155,6 +166,7 @@ public class ServicioUjaCoin {
* @param importe el importe a retirar
*/
@Transactional
@CacheEvict
(
value
=
"cuentas"
,
key
=
"#numCuenta"
)
public
void
reintegro
(
@NotBlank
String
numCuenta
,
@NotBlank
String
numTarjeta
,
@Positive
float
importe
)
{
Cuenta
cuenta
=
repositorioCuentas
.
buscarYBloquear
(
numCuenta
)
.
orElseThrow
(
CuentaNoRegistrada:
:
new
);
...
...
@@ -163,6 +175,8 @@ public class ServicioUjaCoin {
.
orElseThrow
(
TarjetaNoRegistrada:
:
new
);
cuenta
.
nuevoMovimiento
(
new
Reintegro
(
tarjeta
,
importe
));
cacheManager
.
getCache
(
"clientes"
).
evict
(
cuenta
.
getTitular
().
getDni
());
}
/**
...
...
@@ -172,6 +186,10 @@ public class ServicioUjaCoin {
* @param importe el importe a transferir
*/
@Transactional
@Caching
(
evict
=
{
@CacheEvict
(
value
=
"cuentas"
,
key
=
"#numCuentaOrigen"
),
@CacheEvict
(
value
=
"cuentas"
,
key
=
"#numCuentaDestino"
)
})
public
void
transferencia
(
@NotBlank
String
numCuentaOrigen
,
@NotBlank
String
numCuentaDestino
,
@Positive
float
importe
)
{
Cuenta
cuentaOrigen
=
repositorioCuentas
.
buscarYBloquear
(
numCuentaOrigen
)
.
orElseThrow
(
CuentaNoRegistrada:
:
new
);
...
...
@@ -181,6 +199,9 @@ public class ServicioUjaCoin {
cuentaOrigen
.
nuevoMovimiento
(
new
TransferenciaEmitida
(
cuentaDestino
,
importe
));
cuentaDestino
.
nuevoMovimiento
(
new
TransferenciaRecibida
(
cuentaOrigen
,
importe
));
cacheManager
.
getCache
(
"clientes"
).
evict
(
cuentaOrigen
.
getTitular
().
getDni
());
cacheManager
.
getCache
(
"clientes"
).
evict
(
cuentaDestino
.
getTitular
().
getDni
());
}
/**
...
...
src/main/java/es/ujaen/dae/ujacoin/util/CachedBCryptPasswordEncoder.java
View file @
284ba900
...
...
@@ -4,7 +4,8 @@
*/
package
es
.
ujaen
.
dae
.
ujacoin
.
util
;
import
org.springframework.cache.Cache
;
import
java.util.HashMap
;
import
java.util.Map
;
import
org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
;
/**
...
...
@@ -12,21 +13,19 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
* @author ajrueda
*/
public
class
CachedBCryptPasswordEncoder
extends
BCryptPasswordEncoder
{
Cache
cache
;
static
Map
<
String
,
CharSequence
>
cache
=
new
HashMap
<>()
;
public
CachedBCryptPasswordEncoder
(
Cache
cache
)
{
public
CachedBCryptPasswordEncoder
()
{
super
();
this
.
cache
=
cache
;
}
public
CachedBCryptPasswordEncoder
(
Cache
cache
,
int
strength
)
{
public
CachedBCryptPasswordEncoder
(
int
strength
)
{
super
(
strength
);
this
.
cache
=
cache
;
}
@Override
public
boolean
matches
(
CharSequence
rawPassword
,
String
encodedPassword
)
{
CharSequence
cachedMatch
=
cache
.
get
(
encodedPassword
,
CharSequence
.
class
);
CharSequence
cachedMatch
=
cache
.
get
(
encodedPassword
);
if
(
cachedMatch
!=
null
&&
cachedMatch
.
equals
(
rawPassword
))
{
return
true
;
...
...
src/main/resources/application-test.yml
View file @
284ba900
...
...
@@ -3,7 +3,5 @@
# spring.datasource.url: jdbc:mysql://localhost:33060/ujacoin_test
spring.datasource.url
:
jdbc:h2:mem:ujacoin_test;MODE=MYSQL;DATABASE_TO_LOWER=TRUE;DB_CLOSE_DELAY=-1
spring.jpa.properties.javax.persistence.schema-generation.database.action
:
drop-and-create
spring.cache.type
:
NONE
jakarta.persistence.schema-generation.database.action
:
drop-and-create
src/main/resources/application.yml
View file @
284ba900
## Fichero de configuración para UjaCoin
## MySQL local
spring.datasource.url
:
jdbc:mysql://localhost:3306
0
/ujacoin
spring.datasource.url
:
jdbc:mysql://localhost:3306/ujacoin
spring.datasource.username
:
ujacoin
spring.datasource.password
:
secret
## H2 file
#spring.datasource.url: jdbc:h2:file:~/data/ujacoin;MODE=MYSQL;DATABASE_TO_LOWER=TRUE;DB_CLOSE_DELAY=-1
spring.jpa.properties.javax.persistence.schema-generation.database.action
:
none
spring.data.jpa.repositories.bootstrap-mode
:
default
spring.jpa.open-in-view
:
false
spring.cache.type
:
NONE
jakarta.persistence.schema-generation.database.action
:
none
#spring.cache.type: NONE
src/main/resources/ehcache.xml
View file @
284ba900
...
...
@@ -3,15 +3,13 @@
xsi:noNamespaceSchemaLocation=
"http://ehcache.org/ehcache.xsd"
>
<cache
name=
"clientes"
maxElementsInMemory=
"100"
maxElementsInMemory=
"100
0
"
eternal=
"false"
overflowToDisk=
"false"
/>
<cache
name=
"c
lave
s"
maxElementsInMemory=
"100"
<cache
name=
"c
uenta
s"
maxElementsInMemory=
"100
0
"
eternal=
"false"
timeToIdleSeconds=
"600"
timeToLiveSeconds=
"600"
overflowToDisk=
"false"
/>
</ehcache>
\ No newline at end of file
</ehcache>
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