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
6b7e0d57
authored
Jun 20, 2025
by
Alba María Álvarez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
bugfix(user): cambio de username a email para el token
parent
b6a3670e
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
24 additions
and
17 deletions
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/dto/LoginRequest.java
src/main/java/com/example/apprecetas/security/jwt/JwtTokenProvider.java
View file @
6b7e0d57
...
@@ -23,10 +23,10 @@ public class JwtTokenProvider {
...
@@ -23,10 +23,10 @@ public class JwtTokenProvider {
private
final
SecretKey
secretKey
=
Keys
.
secretKeyFor
(
SignatureAlgorithm
.
HS256
);
private
final
SecretKey
secretKey
=
Keys
.
secretKeyFor
(
SignatureAlgorithm
.
HS256
);
public
String
generateToken
(
String
username
)
{
public
String
generateToken
(
String
email
)
{
long
jwtExpirationMs
=
3600000
;
// 1 hora en milisegundos
long
jwtExpirationMs
=
3600000
;
// 1 hora en milisegundos
return
Jwts
.
builder
()
return
Jwts
.
builder
()
.
subject
(
username
)
.
subject
(
email
)
.
issuedAt
(
new
Date
())
.
issuedAt
(
new
Date
())
.
expiration
(
Date
.
from
(
Instant
.
now
().
plus
(
jwtExpirationMs
,
ChronoUnit
.
MILLIS
)))
.
expiration
(
Date
.
from
(
Instant
.
now
().
plus
(
jwtExpirationMs
,
ChronoUnit
.
MILLIS
)))
.
signWith
(
secretKey
)
.
signWith
(
secretKey
)
...
@@ -52,8 +52,8 @@ public class JwtTokenProvider {
...
@@ -52,8 +52,8 @@ public class JwtTokenProvider {
}
}
public
Authentication
getAuthentication
(
String
token
)
{
public
Authentication
getAuthentication
(
String
token
)
{
String
username
=
getUsernameFromToken
(
token
);
String
email
=
getUsernameFromToken
(
token
);
UserDetails
userDetails
=
userDetailsService
.
loadUserByUsername
(
username
);
UserDetails
userDetails
=
userDetailsService
.
loadUserByUsername
(
email
);
return
new
UsernamePasswordAuthenticationToken
(
userDetails
,
null
,
userDetails
.
getAuthorities
());
return
new
UsernamePasswordAuthenticationToken
(
userDetails
,
null
,
userDetails
.
getAuthorities
());
}
}
...
...
src/main/java/com/example/apprecetas/security/service/CustomUserDetailsServiceImpl.java
View file @
6b7e0d57
...
@@ -15,12 +15,12 @@ public class CustomUserDetailsServiceImpl implements UserDetailsService {
...
@@ -15,12 +15,12 @@ public class CustomUserDetailsServiceImpl implements UserDetailsService {
private
final
ReadUserRepository
userRepository
;
private
final
ReadUserRepository
userRepository
;
@Override
@Override
public
UserDetails
loadUserByUsername
(
String
username
)
throws
UsernameNotFoundException
{
public
UserDetails
loadUserByUsername
(
String
email
)
throws
UsernameNotFoundException
{
return
userRepository
.
readBy
Username
(
username
)
return
userRepository
.
readBy
Email
(
email
)
.
map
(
user
->
User
.
withUsername
(
user
.
get
Username
())
.
map
(
user
->
User
.
withUsername
(
user
.
get
Email
())
.
password
(
user
.
getPassword
())
.
password
(
user
.
getPassword
())
.
roles
(
user
.
getRole
().
name
())
.
roles
(
user
.
getRole
().
name
())
.
build
())
.
build
())
.
orElseThrow
(()
->
new
UsernameNotFoundException
(
"Us
er con username "
+
username
+
" no encontrado"
));
.
orElseThrow
(()
->
new
UsernameNotFoundException
(
"Us
uario con email "
+
email
+
" no encontrado"
));
}
}
}
}
src/main/java/com/example/apprecetas/user/infrastructure/controller/AuthController.java
View file @
6b7e0d57
package
com
.
example
.
apprecetas
.
user
.
infrastructure
.
controller
;
package
com
.
example
.
apprecetas
.
user
.
infrastructure
.
controller
;
import
com.example.apprecetas.exception.EntityNotFoundException
;
import
com.example.apprecetas.exception.UnprocessableEntityException
;
import
com.example.apprecetas.exception.UnprocessableEntityException
;
import
com.example.apprecetas.security.jwt.JwtTokenProvider
;
import
com.example.apprecetas.security.jwt.JwtTokenProvider
;
import
com.example.apprecetas.user.application.CreateUserUseCase
;
import
com.example.apprecetas.user.application.CreateUserUseCase
;
...
@@ -17,7 +18,6 @@ import org.springframework.http.ResponseEntity;
...
@@ -17,7 +18,6 @@ import org.springframework.http.ResponseEntity;
import
org.springframework.security.authentication.AuthenticationManager
;
import
org.springframework.security.authentication.AuthenticationManager
;
import
org.springframework.security.authentication.UsernamePasswordAuthenticationToken
;
import
org.springframework.security.authentication.UsernamePasswordAuthenticationToken
;
import
org.springframework.security.core.Authentication
;
import
org.springframework.security.core.Authentication
;
import
org.springframework.security.core.userdetails.UsernameNotFoundException
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestBody
;
...
@@ -40,23 +40,30 @@ public class AuthController {
...
@@ -40,23 +40,30 @@ public class AuthController {
private
final
UserMapper
mapper
=
Mappers
.
getMapper
(
UserMapper
.
class
);
private
final
UserMapper
mapper
=
Mappers
.
getMapper
(
UserMapper
.
class
);
@PostMapping
(
"/login"
)
@PostMapping
(
"/login"
)
public
ResponseEntity
<?>
login
(
@RequestBody
LoginRequest
loginRequest
)
{
public
ResponseEntity
<?>
login
(
@RequestBody
@Valid
LoginRequest
loginRequest
,
BindingResult
result
)
{
if
(
result
.
hasErrors
())
{
String
errorMsg
=
result
.
getFieldErrors
().
stream
()
.
map
(
fieldError
->
fieldError
.
getField
()
+
": "
+
fieldError
.
getDefaultMessage
())
.
collect
(
Collectors
.
joining
(
"; "
));
throw
new
UnprocessableEntityException
(
errorMsg
);
}
try
{
try
{
UserOutputDto
userOutputDto
=
mapper
.
map
(
readUserService
.
readByUsername
(
loginRequest
.
getUsername
()
));
readUserService
.
readByEmail
(
loginRequest
.
getEmail
(
));
Authentication
authentication
=
authenticationManager
.
authenticate
(
Authentication
authentication
=
authenticationManager
.
authenticate
(
new
UsernamePasswordAuthenticationToken
(
new
UsernamePasswordAuthenticationToken
(
loginRequest
.
get
Username
(),
loginRequest
.
get
Email
(),
loginRequest
.
getPassword
()
loginRequest
.
getPassword
()
)
)
);
);
String
token
=
jwtTokenProvider
.
generateToken
(
authentication
.
getName
());
String
token
=
jwtTokenProvider
.
generateToken
(
authentication
.
getName
());
return
ResponseEntity
.
ok
(
new
AuthResponse
(
token
));
return
ResponseEntity
.
ok
(
new
AuthResponse
(
token
));
}
catch
(
Username
NotFoundException
e
)
{
}
catch
(
Entity
NotFoundException
e
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
UNAUTHORIZED
).
body
(
"Usuario no encontrado"
);
return
ResponseEntity
.
status
(
HttpStatus
.
UNAUTHORIZED
).
body
(
"Usuario no encontrado"
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
UNAUTHORIZED
).
body
(
"C
redenciales no válidas
"
);
return
ResponseEntity
.
status
(
HttpStatus
.
UNAUTHORIZED
).
body
(
"C
ontraseña incorrecta
"
);
}
}
}
}
...
@@ -73,7 +80,7 @@ public class AuthController {
...
@@ -73,7 +80,7 @@ public class AuthController {
URI
location
=
URI
.
create
(
"/user"
);
URI
location
=
URI
.
create
(
"/user"
);
UserOutputDto
userOutputDto
=
mapper
.
map
(
createUserService
.
create
(
mapper
.
map
(
userInputDto
)));
UserOutputDto
userOutputDto
=
mapper
.
map
(
createUserService
.
create
(
mapper
.
map
(
userInputDto
)));
String
token
=
jwtTokenProvider
.
generateToken
(
userOutputDto
.
get
Username
());
String
token
=
jwtTokenProvider
.
generateToken
(
userOutputDto
.
get
Email
());
return
ResponseEntity
.
created
(
location
).
body
(
new
AuthResponse
(
token
));
return
ResponseEntity
.
created
(
location
).
body
(
new
AuthResponse
(
token
));
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
...
...
src/main/java/com/example/apprecetas/user/infrastructure/controller/dto/LoginRequest.java
View file @
6b7e0d57
...
@@ -5,8 +5,8 @@ import lombok.Data;
...
@@ -5,8 +5,8 @@ import lombok.Data;
@Data
@Data
public
class
LoginRequest
{
public
class
LoginRequest
{
@NotEmpty
(
message
=
"
Nombre de usuario
obligatorio"
)
@NotEmpty
(
message
=
"
Email
obligatorio"
)
private
String
username
;
private
String
email
;
@NotEmpty
(
message
=
"Contraseña obligatoria"
)
@NotEmpty
(
message
=
"Contraseña obligatoria"
)
private
String
password
;
private
String
password
;
...
...
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