Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Alba María Álvarez
/
AppRecetas
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
9a83b275
authored
Jun 21, 2025
by
Alba María Álvarez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
perf(security): cambio subject token de email a id de usuario
parent
60898b64
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
33 additions
and
44 deletions
src/main/java/com/example/apprecetas/recipe/application/impl/CreateRecipeUseCaseImpl.java
src/main/java/com/example/apprecetas/recipe/infrastructure/controller/ReadRecipeController.java
src/main/java/com/example/apprecetas/security/jwt/JwtTokenProvider.java
src/main/java/com/example/apprecetas/security/service/CustomUserDetailsServiceImpl.java
src/main/java/com/example/apprecetas/user/infrastructure/controller/AuthController.java
src/main/java/com/example/apprecetas/user/infrastructure/controller/ReadUserController.java
src/main/java/com/example/apprecetas/user/infrastructure/controller/UpdateUserController.java
src/main/java/com/example/apprecetas/recipe/application/impl/CreateRecipeUseCaseImpl.java
View file @
9a83b275
package
com
.
example
.
apprecetas
.
recipe
.
application
.
impl
;
import
com.example.apprecetas.exception.EntityNotFoundException
;
import
com.example.apprecetas.recipe.application.CreateRecipeUseCase
;
import
com.example.apprecetas.recipe.domain.entity.Recipe
;
import
com.example.apprecetas.recipe.domain.repository.CreateRecipeRepository
;
import
com.example.apprecetas.recipe.infrastructure.mapper.RecipeMapper
;
import
com.example.apprecetas.recipe.infrastructure.repository.mongodb.RecipeDocument
;
import
com.example.apprecetas.user.domain.repository.ReadUserRepository
;
import
com.example.apprecetas.user.infrastructure.repository.mongodb.UserDocument
;
import
lombok.RequiredArgsConstructor
;
import
org.mapstruct.factory.Mappers
;
import
org.springframework.security.core.context.SecurityContextHolder
;
...
...
@@ -27,15 +25,11 @@ public class CreateRecipeUseCaseImpl implements CreateRecipeUseCase {
public
Recipe
create
(
Recipe
recipe
)
{
RecipeDocument
recipeDocument
=
mapper
.
mapDocument
(
recipe
);
// Get email from token
String
email
=
SecurityContextHolder
.
getContext
().
getAuthentication
().
getName
();
// Search user
UserDocument
user
=
readUserRepository
.
readByEmail
(
email
)
.
orElseThrow
(()
->
new
EntityNotFoundException
(
"Usuario con email "
+
email
+
" no encontrado"
));
// Get idUser from token
String
userId
=
SecurityContextHolder
.
getContext
().
getAuthentication
().
getName
();
// Put userId to recipe
recipeDocument
.
setUserId
(
user
.
getId
()
);
recipeDocument
.
setUserId
(
user
Id
);
RecipeDocument
savedRecipe
=
createRecipeRepository
.
create
(
recipeDocument
);
return
mapper
.
mapDocument
(
savedRecipe
);
...
...
src/main/java/com/example/apprecetas/recipe/infrastructure/controller/ReadRecipeController.java
View file @
9a83b275
...
...
@@ -4,7 +4,6 @@ import com.example.apprecetas.recipe.application.ReadRecipeUseCase;
import
com.example.apprecetas.recipe.infrastructure.controller.dto.output.RecipeOutputDto
;
import
com.example.apprecetas.recipe.infrastructure.mapper.RecipeMapper
;
import
com.example.apprecetas.user.application.ReadUserUseCase
;
import
com.example.apprecetas.user.domain.entity.User
;
import
lombok.RequiredArgsConstructor
;
import
org.mapstruct.factory.Mappers
;
import
org.springframework.data.domain.Page
;
...
...
@@ -34,14 +33,13 @@ public class ReadRecipeController {
public
ResponseEntity
<
Page
<
RecipeOutputDto
>>
readAll
(
@RequestParam
(
required
=
false
,
defaultValue
=
"0"
)
int
page
,
@RequestParam
(
required
=
false
,
defaultValue
=
"6"
)
int
size
)
{
Pageable
pageable
=
PageRequest
.
of
(
page
,
size
);
String
email
=
SecurityContextHolder
.
getContext
().
getAuthentication
().
getName
();
User
user
=
readUserUseCase
.
readByEmail
(
email
);
String
userId
=
SecurityContextHolder
.
getContext
().
getAuthentication
().
getName
();
if
(
service
.
readAllByUser
(
user
.
getId
()
,
pageable
).
isEmpty
())
{
if
(
service
.
readAllByUser
(
user
Id
,
pageable
).
isEmpty
())
{
return
ResponseEntity
.
noContent
().
build
();
}
return
ResponseEntity
.
ok
().
body
(
service
.
readAllByUser
(
user
.
getId
()
,
pageable
).
map
(
mapper:
:
map
));
return
ResponseEntity
.
ok
().
body
(
service
.
readAllByUser
(
user
Id
,
pageable
).
map
(
mapper:
:
map
));
}
}
src/main/java/com/example/apprecetas/security/jwt/JwtTokenProvider.java
View file @
9a83b275
...
...
@@ -23,10 +23,10 @@ public class JwtTokenProvider {
private
final
SecretKey
secretKey
=
Keys
.
secretKeyFor
(
SignatureAlgorithm
.
HS256
);
public
String
generateToken
(
String
email
)
{
public
String
generateToken
(
String
id
)
{
long
jwtExpirationMs
=
3600000
;
// 1 hora en milisegundos
return
Jwts
.
builder
()
.
subject
(
email
)
.
subject
(
id
)
.
issuedAt
(
new
Date
())
.
expiration
(
Date
.
from
(
Instant
.
now
().
plus
(
jwtExpirationMs
,
ChronoUnit
.
MILLIS
)))
.
signWith
(
secretKey
)
...
...
@@ -42,7 +42,7 @@ public class JwtTokenProvider {
}
}
public
String
getUser
name
FromToken
(
String
token
)
{
public
String
getUser
Id
FromToken
(
String
token
)
{
return
Jwts
.
parser
()
.
verifyWith
(
secretKey
)
.
build
()
...
...
@@ -52,8 +52,8 @@ public class JwtTokenProvider {
}
public
Authentication
getAuthentication
(
String
token
)
{
String
email
=
getUsername
FromToken
(
token
);
UserDetails
userDetails
=
userDetailsService
.
loadUserByUsername
(
email
);
String
userId
=
getUserId
FromToken
(
token
);
UserDetails
userDetails
=
userDetailsService
.
loadUserByUsername
(
userId
);
return
new
UsernamePasswordAuthenticationToken
(
userDetails
,
null
,
userDetails
.
getAuthorities
());
}
...
...
src/main/java/com/example/apprecetas/security/service/CustomUserDetailsServiceImpl.java
View file @
9a83b275
...
...
@@ -15,12 +15,12 @@ public class CustomUserDetailsServiceImpl implements UserDetailsService {
private
final
ReadUserRepository
userRepository
;
@Override
public
UserDetails
loadUserByUsername
(
String
email
)
throws
UsernameNotFoundException
{
return
userRepository
.
readBy
Email
(
email
)
.
map
(
user
->
User
.
withUsername
(
user
.
get
Email
())
public
UserDetails
loadUserByUsername
(
String
id
)
throws
UsernameNotFoundException
{
return
userRepository
.
readBy
Id
(
id
)
.
map
(
user
->
User
.
withUsername
(
user
.
get
Id
())
.
password
(
user
.
getPassword
())
.
roles
(
user
.
getRole
().
name
())
.
build
())
.
orElseThrow
(()
->
new
UsernameNotFoundException
(
"Usuario con
email "
+
email
+
" no encontrado"
));
.
orElseThrow
(()
->
new
UsernameNotFoundException
(
"Usuario con
id "
+
id
+
" no encontrado"
));
}
}
src/main/java/com/example/apprecetas/user/infrastructure/controller/AuthController.java
View file @
9a83b275
...
...
@@ -5,6 +5,7 @@ import com.example.apprecetas.exception.UnprocessableEntityException;
import
com.example.apprecetas.security.jwt.JwtTokenProvider
;
import
com.example.apprecetas.user.application.CreateUserUseCase
;
import
com.example.apprecetas.user.application.ReadUserUseCase
;
import
com.example.apprecetas.user.domain.entity.User
;
import
com.example.apprecetas.user.infrastructure.controller.dto.AuthResponse
;
import
com.example.apprecetas.user.infrastructure.controller.dto.LoginRequest
;
import
com.example.apprecetas.user.infrastructure.controller.dto.UserInputDto
;
...
...
@@ -18,6 +19,7 @@ import org.springframework.http.ResponseEntity;
import
org.springframework.security.authentication.AuthenticationManager
;
import
org.springframework.security.authentication.UsernamePasswordAuthenticationToken
;
import
org.springframework.security.core.Authentication
;
import
org.springframework.security.crypto.password.PasswordEncoder
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
...
...
@@ -33,6 +35,7 @@ import java.util.stream.Collectors;
public
class
AuthController
{
private
final
AuthenticationManager
authenticationManager
;
private
final
PasswordEncoder
passwordEncoder
;
private
final
JwtTokenProvider
jwtTokenProvider
;
private
final
ReadUserUseCase
readUserService
;
private
final
CreateUserUseCase
createUserService
;
...
...
@@ -48,22 +51,18 @@ public class AuthController {
.
collect
(
Collectors
.
joining
(
"; "
));
throw
new
UnprocessableEntityException
(
errorMsg
);
}
try
{
readUserService
.
readByEmail
(
loginRequest
.
getEmail
());
User
user
=
readUserService
.
readByEmail
(
loginRequest
.
getEmail
());
Authentication
authentication
=
authenticationManager
.
authenticate
(
new
UsernamePasswordAuthenticationToken
(
loginRequest
.
getEmail
(),
loginRequest
.
getPassword
()
)
);
if
(!
passwordEncoder
.
matches
(
loginRequest
.
getPassword
(),
user
.
getPassword
()))
return
ResponseEntity
.
status
(
HttpStatus
.
UNAUTHORIZED
).
body
(
"Contraseña incorrecta"
);
String
token
=
jwtTokenProvider
.
generateToken
(
authentication
.
getName
());
String
token
=
jwtTokenProvider
.
generateToken
(
user
.
getId
());
return
ResponseEntity
.
ok
(
new
AuthResponse
(
token
));
}
catch
(
EntityNotFoundException
e
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
UNAUTHORIZED
).
body
(
"Usuario no encontrado"
);
}
catch
(
Exception
e
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
UNAUTHORIZED
).
body
(
"Contraseña incorrecta"
);
}
}
...
...
@@ -76,15 +75,17 @@ public class AuthController {
.
collect
(
Collectors
.
joining
(
"; "
));
throw
new
UnprocessableEntityException
(
errorMsg
);
}
try
{
URI
location
=
URI
.
create
(
"/user"
);
User
OutputDto
userOutputDto
=
mapper
.
map
(
createUserService
.
create
(
mapper
.
map
(
userInputDto
)
));
User
createdUser
=
createUserService
.
create
(
mapper
.
map
(
userInputDto
));
String
token
=
jwtTokenProvider
.
generateToken
(
userOutputDto
.
getEmail
());
String
token
=
jwtTokenProvider
.
generateToken
(
createdUser
.
getId
());
return
ResponseEntity
.
created
(
location
).
body
(
new
AuthResponse
(
token
));
}
catch
(
Exception
e
)
{
throw
new
UnprocessableEntityException
(
"Usuario ya registrado. Cambie
sus credenciales
."
);
throw
new
UnprocessableEntityException
(
"Usuario ya registrado. Cambie
el email
."
);
}
}
...
...
src/main/java/com/example/apprecetas/user/infrastructure/controller/ReadUserController.java
View file @
9a83b275
package
com
.
example
.
apprecetas
.
user
.
infrastructure
.
controller
;
import
com.example.apprecetas.user.application.ReadUserUseCase
;
import
com.example.apprecetas.user.domain.entity.User
;
import
com.example.apprecetas.user.infrastructure.controller.dto.UserOutputDto
;
import
com.example.apprecetas.user.infrastructure.mapper.UserMapper
;
import
lombok.RequiredArgsConstructor
;
...
...
@@ -25,9 +24,8 @@ public class ReadUserController {
@GetMapping
(
"/me"
)
public
ResponseEntity
<
UserOutputDto
>
readMe
()
{
String
email
=
SecurityContextHolder
.
getContext
().
getAuthentication
().
getName
();
User
user
=
service
.
readByEmail
(
email
);
return
ResponseEntity
.
ok
().
body
(
mapper
.
map
(
service
.
readById
(
user
.
getId
())));
String
userId
=
SecurityContextHolder
.
getContext
().
getAuthentication
().
getName
();
return
ResponseEntity
.
ok
().
body
(
mapper
.
map
(
service
.
readById
(
userId
)));
}
@GetMapping
...
...
src/main/java/com/example/apprecetas/user/infrastructure/controller/UpdateUserController.java
View file @
9a83b275
...
...
@@ -3,7 +3,6 @@ package com.example.apprecetas.user.infrastructure.controller;
import
com.example.apprecetas.exception.UnprocessableEntityException
;
import
com.example.apprecetas.user.application.ReadUserUseCase
;
import
com.example.apprecetas.user.application.UpdateUserUseCase
;
import
com.example.apprecetas.user.domain.entity.User
;
import
com.example.apprecetas.user.infrastructure.controller.dto.UserInputDto
;
import
com.example.apprecetas.user.infrastructure.controller.dto.UserOutputDto
;
import
com.example.apprecetas.user.infrastructure.mapper.UserMapper
;
...
...
@@ -39,9 +38,8 @@ public class UpdateUserController {
.
collect
(
Collectors
.
joining
(
"; "
));
throw
new
UnprocessableEntityException
(
errorMsg
);
}
String
email
=
SecurityContextHolder
.
getContext
().
getAuthentication
().
getName
();
User
user
=
readUserUseCase
.
readByEmail
(
email
);
return
ResponseEntity
.
ok
().
body
(
mapper
.
map
(
service
.
updateById
(
user
.
getId
(),
mapper
.
map
(
userInputDto
))));
String
userId
=
SecurityContextHolder
.
getContext
().
getAuthentication
().
getName
();
return
ResponseEntity
.
ok
().
body
(
mapper
.
map
(
service
.
updateById
(
userId
,
mapper
.
map
(
userInputDto
))));
}
}
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