Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
BackEnd-V2
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Joel Andres Molina Velez
BackEnd-V2
Commits
b7e2a867
Commit
b7e2a867
authored
Aug 27, 2023
by
Joel Andres Molina Velez
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Se maneja la seguridad de apis
parent
a022f174
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
12 additions
and
5 deletions
+12
-5
JwtAuthenticationFilter.java
...vilidad/MovilidadWS/Security/JwtAuthenticationFilter.java
+10
-4
SecurityConfig.java
...u/espe/movilidad/MovilidadWS/Security/SecurityConfig.java
+2
-1
No files found.
src/main/java/ec/edu/espe/movilidad/MovilidadWS/Security/JwtAuthenticationFilter.java
View file @
b7e2a867
...
@@ -5,6 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired;
...
@@ -5,6 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.security.authentication.UsernamePasswordAuthenticationToken
;
import
org.springframework.security.authentication.UsernamePasswordAuthenticationToken
;
import
org.springframework.security.core.context.SecurityContextHolder
;
import
org.springframework.security.core.context.SecurityContextHolder
;
import
org.springframework.security.core.userdetails.UserDetails
;
import
org.springframework.security.core.userdetails.UserDetails
;
import
org.springframework.security.core.userdetails.UsernameNotFoundException
;
import
org.springframework.security.web.authentication.WebAuthenticationDetailsSource
;
import
org.springframework.security.web.authentication.WebAuthenticationDetailsSource
;
import
org.springframework.web.filter.OncePerRequestFilter
;
import
org.springframework.web.filter.OncePerRequestFilter
;
...
@@ -34,6 +35,7 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
...
@@ -34,6 +35,7 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
loggerAuth
.
info
(
"Token válido obtenido en el backend: {}"
,
Token
);
loggerAuth
.
info
(
"Token válido obtenido en el backend: {}"
,
Token
);
if
(
email
!=
null
&&
SecurityContextHolder
.
getContext
().
getAuthentication
()
==
null
)
{
if
(
email
!=
null
&&
SecurityContextHolder
.
getContext
().
getAuthentication
()
==
null
)
{
try
{
// Autenticar al usuario por correo electrónico
// Autenticar al usuario por correo electrónico
UserDetails
userDetails
=
jwtUserDetailsService
.
loadUserByUsername
(
email
);
UserDetails
userDetails
=
jwtUserDetailsService
.
loadUserByUsername
(
email
);
if
(
userDetails
!=
null
)
{
if
(
userDetails
!=
null
)
{
...
@@ -41,14 +43,19 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
...
@@ -41,14 +43,19 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
userDetails
,
null
,
userDetails
.
getAuthorities
());
userDetails
,
null
,
userDetails
.
getAuthorities
());
authenticationToken
.
setDetails
(
new
WebAuthenticationDetailsSource
().
buildDetails
(
request
));
authenticationToken
.
setDetails
(
new
WebAuthenticationDetailsSource
().
buildDetails
(
request
));
SecurityContextHolder
.
getContext
().
setAuthentication
(
authenticationToken
);
SecurityContextHolder
.
getContext
().
setAuthentication
(
authenticationToken
);
}
}
catch
(
UsernameNotFoundException
ex
)
{
// Manejar la excepción y enviar una respuesta adecuada
response
.
setStatus
(
HttpServletResponse
.
SC_UNAUTHORIZED
);
response
.
getWriter
().
write
(
"El usuario no es correcto"
);
response
.
getWriter
().
flush
();
return
;
}
}
}
}
if
(
request
.
getRequestURI
().
equals
(
"/api/v1.0/usuarioLogeado"
))
{
if
(
request
.
getRequestURI
().
equals
(
"/api/v1.0/usuarioLogeado"
))
{
String
username
=
SecurityContextHolder
.
getContext
().
getAuthentication
().
getName
();
//Se obtiene el nombre del uuario autenticado
String
username
=
SecurityContextHolder
.
getContext
().
getAuthentication
().
getName
();
//Se obtiene el nombre del usuario autenticado
DtoUserLogeado
userInfo
=
jwtUserDetailsService
.
getUserInfoByUsername
(
username
);
DtoUserLogeado
userInfo
=
jwtUserDetailsService
.
getUserInfoByUsername
(
username
);
//Se devuelve los datos en formato JSON
//Se devuelve los datos en formato JSON
ObjectMapper
objectMapper
=
new
ObjectMapper
();
ObjectMapper
objectMapper
=
new
ObjectMapper
();
...
@@ -61,4 +68,3 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
...
@@ -61,4 +68,3 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
filterChain
.
doFilter
(
request
,
response
);
filterChain
.
doFilter
(
request
,
response
);
}
}
}
}
src/main/java/ec/edu/espe/movilidad/MovilidadWS/Security/SecurityConfig.java
View file @
b7e2a867
...
@@ -3,6 +3,7 @@ package ec.edu.espe.movilidad.MovilidadWS.Security;
...
@@ -3,6 +3,7 @@ package ec.edu.espe.movilidad.MovilidadWS.Security;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
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.annotation.web.builders.HttpSecurity
;
import
org.springframework.security.config.http.SessionCreationPolicy
;
import
org.springframework.security.config.http.SessionCreationPolicy
;
...
@@ -34,9 +35,9 @@ public class SecurityConfig {
...
@@ -34,9 +35,9 @@ public class SecurityConfig {
.
httpBasic
().
authenticationEntryPoint
(
jwtEntryPoint
)
.
httpBasic
().
authenticationEntryPoint
(
jwtEntryPoint
)
.
and
()
.
and
()
.
authorizeRequests
()
.
authorizeRequests
()
.
antMatchers
(
HttpMethod
.
DELETE
,
"/api/v1.0/**"
).
hasRole
(
"Administrador del Sistema"
)
.
antMatchers
(
"/api/v1.0/**"
).
authenticated
()
.
antMatchers
(
"/api/v1.0/**"
).
authenticated
()
//.antMatchers("/api/v1.0/**").permitAll()
//.antMatchers("/api/v1.0/**").permitAll()
// .antMatchers("/api/v1.0/**").hasRole("Administrador del Sistema") // Asignar API a rol ADMIN
.
and
()
.
and
()
.
sessionManagement
()
.
sessionManagement
()
.
sessionCreationPolicy
(
SessionCreationPolicy
.
STATELESS
);
.
sessionCreationPolicy
(
SessionCreationPolicy
.
STATELESS
);
...
...
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