Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
Equipo 3
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
migudel
Equipo 3
Merge requests
!17
Compila, se construye y corre en principio
Code
Review changes
Check out branch
Open in Workspace
Download
Patches
Plain diff
Expand sidebar
Merged
Compila, se construye y corre en principio
dev/microservicio-authentication
into
develop
Overview
0
Commits
1
Pipelines
0
Changes
27
Merged
Compila, se construye y corre en principio
hugcubi
requested to merge
dev/microservicio-authentication
into
develop
9 months ago
Overview
0
Commits
1
Pipelines
0
Changes
27
0
0
Merge request reports
Compare
develop
develop (base)
and
latest version
latest version
43ea7d12
1 commit,
9 months ago
27 files
+
815
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
27
java/services/authentication/src/main/java/com/uva/authentication/Controllers/AuthenticationController.java
0 → 100644
+
94
−
0
View file @ 43ea7d12
Edit in single-file editor
Open in Web IDE
package
com.uva.authentication.Controllers
;
import
com.uva.authentication.Models.User
;
import
io.jsonwebtoken.Jwts
;
import
io.jsonwebtoken.SignatureAlgorithm
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.client.RestTemplate
;
import
java.util.Date
;
@RestController
@RequestMapping
(
"/auth"
)
public
class
AuthenticationController
{
@Autowired
private
RestTemplate
restTemplate
;
// Usamos RestTemplate para acceder al microservicio de usuarios
private
final
String
USER_SERVICE_URL
=
"http://user-service/users"
;
// URL del microservicio de usuarios
// Clave secreta para firmar el token JWT
private
final
String
SECRET_KEY
=
"your-secret-key"
;
// Cambia esto por una clave secreta segura
@PostMapping
(
"/login"
)
public
ResponseEntity
<?>
authenticateUser
(
@RequestBody
LoginRequest
loginRequest
)
{
// Hacemos una llamada al microservicio de usuarios para obtener el usuario por correo
ResponseEntity
<
User
>
userResponse
=
restTemplate
.
getForEntity
(
USER_SERVICE_URL
+
"/{email}"
,
User
.
class
,
loginRequest
.
getEmail
()
);
// Verificamos si el usuario existe
if
(
userResponse
.
getStatusCode
().
is2xxSuccessful
())
{
User
user
=
userResponse
.
getBody
();
// Verificamos que la contraseña coincida (esto debería hacerse de forma segura en un servicio real)
if
(
user
!=
null
&&
user
.
getPassword
().
equals
(
loginRequest
.
getPassword
()))
{
// Generamos el token JWT si la autenticación es correcta
String
token
=
generateToken
(
user
.
getName
(),
user
.
getEmail
());
return
ResponseEntity
.
ok
(
new
AuthResponse
(
token
));
}
}
// Si el usuario no existe o la contraseña es incorrecta
return
ResponseEntity
.
status
(
401
).
body
(
"Invalid email or password"
);
}
// Método para generar el token JWT
private
String
generateToken
(
String
name
,
String
email
)
{
return
Jwts
.
builder
()
.
setSubject
(
name
)
.
claim
(
"email"
,
email
)
// Añadimos el correo como parte del payload
.
setIssuedAt
(
new
Date
())
// Fecha de emisión
.
setExpiration
(
new
Date
(
System
.
currentTimeMillis
()
+
3600000
))
// Expira en 1 hora
.
signWith
(
SignatureAlgorithm
.
HS512
,
SECRET_KEY
)
// Firma con la clave secreta
.
compact
();
}
public
static
class
LoginRequest
{
private
String
email
;
private
String
password
;
// Getters and setters
public
String
getEmail
()
{
return
email
;
}
public
void
setEmail
(
String
email
)
{
this
.
email
=
email
;
}
public
String
getPassword
()
{
return
password
;
}
public
void
setPassword
(
String
password
)
{
this
.
password
=
password
;
}
}
public
static
class
AuthResponse
{
private
String
token
;
public
AuthResponse
(
String
token
)
{
this
.
token
=
token
;
}
// Getter
public
String
getToken
()
{
return
token
;
}
}
}
Loading